duanbiao4035 2014-02-09 11:26
浏览 89
已采纳

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

I need to sign a message for submission to a remote service (over a 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. I'm using golang, and crypto/ecdsa for this with accompanying packages for byte encoding etc.

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. Private keys are generated hashing your user ID and your password with SHA224.

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.

  2. Reads that into an int64 which is then used as the private key

  3. Constructs an instance of ecdsa.PrivateKey and corresponding ecdsa.PublicKey correctly

  4. Returns said key for use in ecdsa.Sign() function calls

I then sign another message which consists of a userId (integer), and two nonces.

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?

  2. The r and s components are very large - presumably because I'm using int64. Could this be an issue?

  3. Is the line sha256.New224().Sum([]byte(userId + pass)) "correct" for hasing those two items?

  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.

  • 写回答

1条回答 默认 最新

  • douhuanqiao5290 2014-03-12 12:18
    关注

    To answer my own questions:

    Is my NewKey func correct?

    No.

    The r and s components are very large - presumably because I'm using 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?

    It's correct insofar as I'm passing it a []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:

    key := new(big.Int).SetBytes(sum)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口