在近协议中使用ed25519进行签名和验证 [英] Sign and verify using ed25519 in near protocol

查看:619
本文介绍了在近协议中使用ed25519进行签名和验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 jwt身份验证.

I am trying to sign the message in javascript using signMessage and verify it with python for implementing jwt authentication.

这是使用 near-api-js 在javascript中签名消息的代码.

This is the code for signing message in javascript using near-api-js

window.signer = await new nearlib.InMemorySigner(window.walletAccount._keyStore)
const mysign = await window.signer.signMessage("Amiya", 
window.walletAccount._authData.accountId, window.walletAccount._networkId)
let mypubdata = ""
mysign.publicKey.data.forEach( function (item,index){
  if(item < 16) {
    mypubdata = mypubdata + '0' + item.toString(16)
  }
  else {
    mypubdata = mypubdata + item.toString(16)
  }

})
console.log("public key", mypubdata)
let mysignature = ""
mysign.signature.forEach( function (item,index){
  if(item < 16) {
    mysignature = mysignature + '0' + item.toString(16)
  }
  else {
    mysignature = mysignature + item.toString(16)
  }

})
console.log("signature", mysignature)

输出给出:
公钥fe20d3e271876c8329c74dcdbe95e32586ee5cf67def1c0cc9e0b8d0e4285813
签名61d864f40667075da6f920f811def3b83330a6cce49b7bd24eb4711f29abcf55d6d2eaf6f67bf74f20a2f79598f7fd42b4f70db41446d73d596b58d31825710c

Output gives:
public key fe20d3e271876c8329c74dcdbe95e32586ee5cf67def1c0cc9e0b8d0e4285813
signature 61d864f40667075da6f920f811def3b83330a6cce49b7bd24eb4711f29abcf55d6d2eaf6f67bf74f20a2f79598f7fd42b4f70db41446d73d596b58d31825710c

这是我的后端的python 代码:

import ed25519
import hashlib
pubKey = ed25519.VerifyingKey(b"fe20d3e271876c8329c74dcdbe95e32586ee5cf67def1c0cc9e0b8d0e4285813", encoding="hex")

print("Public key (32 bytes): ", pubKey.to_ascii(encoding='hex'))
signature = "61d864f40667075da6f920f811def3b83330a6cce49b7bd24eb4711f29abcf55d6d2eaf6f67bf74f20a2f79598f7fd42b4f70db41446d73d596b58d31825710c"
msg = hashlib.sha256(b"Amiya").hexdigest()
print(msg)

try:
    pubKey.verify(signature, msg, encoding='hex')
    print("The signature is valid.")
except:
    print("Invalid signature!")

但是我无法使其正常工作,它给出了无效的签名.

But I am unable to make it work, it gives an invalid signature.

推荐答案

形式化@topaco在上面的评论中给出的答案:

Formalizing the answer given by @topaco in the comment above:

尝试用digest()代替hexdigest() – Topaco 5月27日18:13

Try digest() instead of hexdigest() – Topaco May 27 at 18:13

这篇关于在近协议中使用ed25519进行签名和验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆