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 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100