如何使用静态字符串作为私钥来签署消息?

huangapple go评论88阅读模式
英文:

How to use static string as of private key to sign the message?

问题

我正在使用crypto包来使用以下代码对数据进行签名。该包中的DSA是ed25519。在这段代码中,我想使用由其他服务(Node.js)生成的静态私钥来进行签名。我已经尝试过,但是显示错误信息:

cannot convert "3ef8cc42c5c5ed0f7a11ef2045da509ed175074223c6ee1a6acc71c15b2a8a8a" (untyped string constant) to [64]byte

代码

package main

import (
	"log"

	crypto "golang.org/x/crypto/nacl/sign"
)

func main() {

	signedData := crypto.Sign(nil, []byte("hello"), &[64]byte("3ef8cc42c5c5ed0f7a11ef2045da509ed175074223c6ee1a6acc71c15b2a8a8a"))
	log.Println(string(signedData))
	log.Println(signedData)

}

有人可以帮我解决这个问题吗?库链接

英文:

I'm using crypto package to signing the data using below code. The DSA in the package is ed25519 . In this code I want to using a static private key which is generated by other service(node js) into sign function which mention in the code.I have tried it but showing me the error

cannot convert "3ef8cc42c5c5ed0f7a11ef2045da509ed175074223c6ee1a6acc71c15b2a8a8a" (untyped string constant) to [64]byte

CODE

package main

import (
	"log"

	crypto "golang.org/x/crypto/nacl/sign"
)

func main() {

	signedData := crypto.Sign(nil, []byte("hello"), *[64]byte("3ef8cc42c5c5ed0f7a11ef2045da509ed175074223c6ee1a6acc71c15b2a8a8a"))
	log.Println(string(signedData))
	log.Println(signedData)

}

Can anyone help me in this? library link

答案1

得分: 2

你的密钥是(十六进制解码后)只有32个字节,只是种子,你需要附加32个字节的公钥s。这里

3ef8cc42c5c5ed0f7a11ef2045da509ed175074223c6ee1a6acc71c15b2a8a8ae2f5b5f7c272360a8292b796c1dc28e8b15f415c9c3680bcae927dda3458261b

并对完整密钥进行十六进制解码。总体而言:

import (
	"encoding/hex"
	"log"
	crypto "golang.org/x/crypto/nacl/sign"
)

func main() {

	decoded, _ := hex.DecodeString("3ef8cc42c5c5ed0f7a11ef2045da509ed175074223c6ee1a6acc71c15b2a8a8ae2f5b5f7c272360a8292b796c1dc28e8b15f415c9c3680bcae927dda3458261b")
	var key [64]byte
	copy(key[:], decoded)
	signedData := crypto.Sign(nil, []byte("hello"), &key)
	log.Println(string(signedData))
	log.Println(hex.EncodeToString(signedData))
}

该代码给出了十六进制编码的签名:

560f119fd68a647eb9ff10fa83fff71713e377d041c7b076873bed767ed15bae74ff7822764636bfadc2afc4f430e93f32df6c7ba8b904f6055fc9b32fe85f0268656c6c6f

其中附加了消息68656c6c6f


你可以使用这个在线工具生成相同密钥和相同消息的签名。由于Ed25519是确定性的,相同的输入数据总是生成相同的签名。测试显示两个签名匹配,因此上述代码经过了成功测试。
请注意,该工具不附加消息,并且使用Base64编码的数据作为密钥和签名。

英文:

Your key is (hex decoded) only 32 bytes and is only the seed, you have to append the 32 bytes public key, s. here:

3ef8cc42c5c5ed0f7a11ef2045da509ed175074223c6ee1a6acc71c15b2a8a8ae2f5b5f7c272360a8292b796c1dc28e8b15f415c9c3680bcae927dda3458261b

and hex decode the full key. Overall:

import (
	"encoding/hex"
	"log"
	crypto "golang.org/x/crypto/nacl/sign"
)

func main() {

	decoded, _ := hex.DecodeString("3ef8cc42c5c5ed0f7a11ef2045da509ed175074223c6ee1a6acc71c15b2a8a8ae2f5b5f7c272360a8292b796c1dc28e8b15f415c9c3680bcae927dda3458261b")
	var key [64]byte
	copy(key[:], decoded)
	signedData := crypto.Sign(nil, []byte("hello"), &key)
	log.Println(string(signedData))
	log.Println(hex.EncodeToString(signedData))
}

The code gives for the hex encoded signature:

560f119fd68a647eb9ff10fa83fff71713e377d041c7b076873bed767ed15bae74ff7822764636bfadc2afc4f430e93f32df6c7ba8b904f6055fc9b32fe85f0268656c6c6f

where the message 68656c6c6f is attached.


You can e.g. use this online tool and generate a signature for the same key and the same message. Since Ed25519 is deterministic, the same signature is always generated for the same input data. The test shows that both signatures match, so the above code is successfully tested.
Note that the tool does not append the message and that it uses Base64 encoded data for key and signature.

huangapple
  • 本文由 发表于 2022年9月13日 15:42:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/73699210.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定