简体   繁体   English

通过SHA224摘要使用ecdsa和私钥进行Golang签名

[英]Golang signing with ecdsa and Private Key from a SHA224 digest

I need to sign a message for submission to a remote service (over a websocket). 我需要签名一条消息以提交到远程服务(通过Websocket)。 To do this, I need to structure a private key based on an integer (my user id) and a passphrase (a base64 encoded string)., hashed using SHA224. 为此,我需要基于整数(我的用户ID)和密码短语(base64编码的字符串)来构造私钥,并使用SHA224对其进行哈希处理。 I'm using golang, and crypto/ecdsa for this with accompanying packages for byte encoding etc. 我正在使用golang和crypto / ecdsa以及用于字节编码等的随附软件包。

Here's the documentation I have: 这是我的文档:

Signatures use an Elliptic Curve Digital Signature Algorithm (ECDSA) encoded message containing: user ID, Server Nonce, Client Node and Private key. 签名使用椭圆曲线数字签名算法(ECDSA)编码的消息,其中包含:用户ID,服务器Nonce,客户端节点和私钥。 Private keys are generated hashing your user ID and your password with SHA224. 私钥是通过SHA224哈希您的用户ID和密码而生成的。

Here's my func: 这是我的功能:

func NewKey(userId int64, pass string) (prKey ecdsa.PrivateKey) {
    buf := new(bytes.Buffer)
    binary.Write(buf, binary.BigEndian, userId)
    passArr := []byte(pass)

    sha := sha256.New224()
    sha.Write(buf.Bytes())
    sha.Write(passArr)
    sum := sha.Sum(nil)

    var in int64
    reader := bytes.NewReader(sum)
    err := binary.Read(reader, binary.BigEndian, &in)

    if err != nil {
        log.Fatal(err)
    }

    prKey.D = big.NewInt(in)
    prKey.PublicKey.Curve = elliptic.P224()
    return prKey
}

My intent with this func is that it: 我使用此功能的目的是:

  1. Hashes the userId and pass correctly in a []byte using SHA224. 散列userId并使用SHA224正确传递[]byte

  2. Reads that into an int64 which is then used as the private key 将其读取到int64 ,然后将其用作私钥

  3. Constructs an instance of ecdsa.PrivateKey and corresponding ecdsa.PublicKey correctly 正确构造ecdsa.PrivateKey和相应的ecdsa.PublicKey的实例

  4. Returns said key for use in ecdsa.Sign() function calls 返回用于ecdsa.Sign()函数调用的密钥

I then sign another message which consists of a userId (integer), and two nonces. 然后,我签署另一条由userId(整数)和两个随机数组成的消息。

Here's how I sign my message: 这是我对消息进行签名的方式:

key := NewKey(userId, pass) // the above func
msg := sha256.New224().Sum([]byte(userId + srNonce + clNonce))
r, s, err := ecdsa.Sign(rand.Reader, &key, msg)
sig := []string{enc(r.String()), enc(s.String())}

Questions: 问题:

  1. Is my NewKey func correct? 我的NewKey函数正确吗?

  2. The r and s components are very large - presumably because I'm using int64 . rs组件非常大-大概是因为我使用的是int64 Could this be an issue? 这可能是个问题吗?

  3. Is the line sha256.New224().Sum([]byte(userId + pass)) "correct" for hasing those two items? 拥有这sha256.New224().Sum([]byte(userId + pass)) “正确”?

  4. How can I create my private key correctly (assuming it's wrong) and subsequently sign the message? 如何正确创建我的私钥(假设它是错误的)并随后对消息签名?

I'm very new to ECDSA and have basic crypto knowledge in general. 我是ECDSA的新手,并且总体上具有基本的加密知识。

To answer my own questions: 要回答我自己的问题:

Is my NewKey func correct? 我的NewKey函数正确吗?

No. 没有。

The r and s components are very large - presumably because I'm using int64. r和s组件非常大-大概是因为我使用的是int64。 Could this be an issue? 这可能是个问题吗?

They should be large. 它们应该很大。

Is the line sha256.New224().Sum([]byte(userId + pass)) "correct" for hashing those two items? sha256.New224()。Sum([] byte(userId + pass))行对于散列这两个项目是否“正确”?

It's correct insofar as I'm passing it a []byte . 就我向它传递[]byte ,它是正确的。

How can I create my private key correctly (assuming it's wrong) and subsequently sign the message? 如何正确创建我的私钥(假设它是错误的)并随后对消息签名?

The key requires a big.Int , so using the following should suffice assuming the hash is correct: 密钥需要一个big.Int ,因此使用以下代码就可以满足假设哈希正确的情况:

key := new(big.Int).SetBytes(sum)

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

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