Golang curve25519公钥生成器

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

Golang curve25519 pubkey generator

问题

我正在尝试实现curve25519算法用于公钥生成。但是我在如何从Go中的sha256编码字符串生成公钥方面遇到了困难。

我在Python中使用sha256编码字符串生成公钥没有问题:

  1. import hashlib
  2. import x25519
  3. example_string = 'kerielle'
  4. sha = hashlib.sha256(example_string.encode()).hexdigest()
  5. hash_bytes = bytes.fromhex(sha)
  6. public_key = x25519.scalar_base_mult(hash_bytes)
  7. hex_res = public_key.hex()
  8. print(hex_res)
  9. >>> 0fa065fcaedecef9aebb5c79ea1c613e82bc5534c4b139d71f3a1cb0cb956652

但是在Go中如何做到这一点呢?以下是一个基本示例:

  1. var publicKey [32]byte
  2. privateKey := (*[32]byte)(bytes.Repeat([]byte("1"), 32))
  3. curve25519.ScalarBaseMult(&publicKey, privateKey)
  4. fmt.Printf("%x\n", publicKey)
  5. >>> 04f5f29162c31a8defa18e6e742224ee806fc1718a278be859ba5620402b8f3a
英文:

I'm trying to implement the curve25519 algo for pubkey generation. But I got stuck on how to generate pubkey from sha256 encoded string in Go?

I have no problems with generation pubkey from sha256 encoded string via Python:

  1. import hashlib
  2. import x25519
  3. example_string = 'kerielle'
  4. sha = hashlib.sha256(example_string.encode()).hexdigest()
  5. hash_bytes = bytes.fromhex(sha)
  6. public_key = x25519.scalar_base_mult(hash_bytes)
  7. hex_res = public_key.hex()
  8. print(hex_res)
  9. >>> 0fa065fcaedecef9aebb5c79ea1c613e82bc5534c4b139d71f3a1cb0cb956652

But how to do the same in Go? Here basic example:

  1. var publicKey [32]byte
  2. privateKey := (*[32]byte)(bytes.Repeat([]byte("1"), 32))
  3. curve25519.ScalarBaseMult(&publicKey, privateKey)
  4. fmt.Printf("%x\n", publicKey)
  5. >>> 04f5f29162c31a8defa18e6e742224ee806fc1718a278be859ba5620402b8f3a

答案1

得分: 3

这只是复制了Python代码示例的结果,并考虑了curve25519.ScalarBaseMult()文档中的建议:

> 建议使用X25519函数与Basepoint一起使用,因为将数据复制到固定大小的数组中可能会导致意外的错误。

  1. package main
  2. import (
  3. "crypto/sha256"
  4. "fmt"
  5. "log"
  6. "golang.org/x/crypto/curve25519"
  7. )
  8. func main() {
  9. example_string := "kerielle"
  10. hash := sha256.Sum256([]byte(example_string))
  11. publicKey, err := curve25519.X25519(hash[:], curve25519.Basepoint)
  12. if err != nil {
  13. log.Fatalf("curve25519.X25519() 失败: %v", err)
  14. }
  15. fmt.Printf("%x\n", publicKey)
  16. }

输出:

  1. 0fa065fcaedecef9aebb5c79ea1c613e82bc5534c4b139d71f3a1cb0cb956652

Go Playground

英文:

This simply replicates the result of the Python code sample while also taking into account guidance from the curve25519.ScalarBaseMult() documentation:

> It is recommended to use the X25519 function with Basepoint instead,
> as copying into fixed size arrays can lead to unexpected bugs.

  1. package main
  2. import (
  3. "crypto/sha256"
  4. "fmt"
  5. "log"
  6. "golang.org/x/crypto/curve25519"
  7. )
  8. func main() {
  9. example_string := "kerielle"
  10. hash := sha256.Sum256([]byte(example_string))
  11. publicKey, err := curve25519.X25519(hash[:], curve25519.Basepoint)
  12. if err != nil {
  13. log.Fatalf("curve25519.X25519() failed: %v", err)
  14. }
  15. fmt.Printf("%x\n", publicKey)
  16. }

Output:

  1. 0fa065fcaedecef9aebb5c79ea1c613e82bc5534c4b139d71f3a1cb0cb956652

Go Playground

huangapple
  • 本文由 发表于 2022年1月16日 11:37:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/70727192.html
匿名

发表评论

匿名网友

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

确定