简体   繁体   English

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

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

I'm using crypto package to signing the data using below code.我正在使用crypto package 使用以下代码对数据进行签名。 The DSA in the package is ed25519 . package 中的 DSA 是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在这段代码中,我想使用由其他服务(节点 js)生成的 static 私钥到代码中提到的符号 function 中。我已经尝试过了,但显示了错误

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 图书馆链接

Your key is (hex decoded) only 32 bytes and is only the seed, you have to append the 32 bytes public key, s.您的密钥(十六进制解码)只有 32 个字节并且只是种子,您必须 append 32 个字节的公钥,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.附有消息68656c6c6f的位置。


You can eg 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.由于 Ed25519 是确定性的,因此始终为相同的输入数据生成相同的签名。 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.请注意,该工具不会 append 消息,它使用 Base64 编码数据作为密钥和签名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM