将Kyber.Point转换为字节或从Kyber.Point中提取字节。

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

Convert Kyber.Point to Byte or Extract Byte from Using Kyber.Point

问题

如何将Kyber.Point类型转换为字节或从中提取字节(在Golang中)?

我想要使用HMAC的密钥(ECDH)作为哈希密钥。

代码:

  1. package main
  2. import (
  3. "crypto/hmac"
  4. "crypto/rand"
  5. "crypto/sha256"
  6. "encoding/hex"
  7. "fmt"
  8. "math/big"
  9. "go.dedis.ch/kyber/v3/group/edwards25519"
  10. "go.dedis.ch/kyber/v3/util/random"
  11. )
  12. var rng = random.New()
  13. func GenerateRandomASCIIString(length int) (string, error) {
  14. result := ""
  15. for {
  16. if len(result) >= length {
  17. return result, nil
  18. }
  19. num, err := rand.Int(rand.Reader, big.NewInt(int64(127)))
  20. if err != nil {
  21. return "", err
  22. }
  23. n := num.Int64()
  24. if n > 32 && n < 127 {
  25. result += string(n)
  26. }
  27. }
  28. }
  29. func main() {
  30. suite := edwards25519.NewBlakeSHA256Ed25519()
  31. X := suite.Point().Pick(rng)
  32. Y := suite.Point().Pick(rng)
  33. a := suite.Scalar().Pick(suite.RandomStream())
  34. b := suite.Scalar().Pick(suite.RandomStream())
  35. fmt.Printf("Titik yang disepakati Alice dan Bob :\n X:\t%s\n Y:\t%s\n\n", X, Y)
  36. fmt.Printf("Kunci Private Alice:\n %s\n\n", a)
  37. fmt.Printf("Kunci Private Bob:\n %s\n\n", b)
  38. aX := suite.Point().Mul(a, X)
  39. aY := suite.Point().Mul(a, Y)
  40. bX := suite.Point().Mul(b, X)
  41. bY := suite.Point().Mul(b, Y)
  42. //Punya Alice
  43. abX := suite.Point().Mul(a, bX)
  44. abY := suite.Point().Mul(a, bY)
  45. //Punya Bob
  46. baX := suite.Point().Mul(b, aX)
  47. baY := suite.Point().Mul(b, aY)
  48. fmt.Printf("Kunci Rahasia Alice:\n abX : %s\n abY : %s\n\n", abX, abY)
  49. fmt.Printf("Kunci Rahasia Bob:\n baX : %s\n baY : %s\n", baX, baY)
  50. data := "data"
  51. h := hmac.New(sha256.New, abX)
  52. h.Write([]byte(data))
  53. sha := hex.EncodeToString(h.Sum(nil))
  54. fmt.Println("Hasil Hash Alice: " + sha)
  55. g := hmac.New(sha256.New, baY)
  56. g.Write([]byte(data))
  57. sha2 := hex.EncodeToString(g.Sum(nil))
  58. fmt.Println("Hasil Hash Bob: " + sha2)
  59. }

运行代码时,我遇到以下错误:

  1. ./prog.go:77:15: cannot use abX (type kyber.Point) as type []byte in argument to hmac.New
  2. ./prog.go:90:15: cannot use baY (type kyber.Point) as type []byte in argument to hmac.New

我想要将abX/abY和baX/baY转换为字节类型。

英文:

How to convert Kyber.Point type to Byte or Extract bytes from in Golang ?

I want the secret key (ECDH) to use with HMAC has hash key.

Code:

  1. package main
  2. import (
  3. &quot;crypto/hmac&quot;
  4. &quot;crypto/rand&quot;
  5. &quot;crypto/sha256&quot;
  6. &quot;encoding/hex&quot;
  7. &quot;fmt&quot;
  8. &quot;math/big&quot;
  9. &quot;go.dedis.ch/kyber/v3/group/edwards25519&quot;
  10. &quot;go.dedis.ch/kyber/v3/util/random&quot;
  11. )
  12. var rng = random.New()
  13. func GenerateRandomASCIIString(length int) (string, error) {
  14. result := &quot;&quot;
  15. for {
  16. if len(result) &gt;= length {
  17. return result, nil
  18. }
  19. num, err := rand.Int(rand.Reader, big.NewInt(int64(127)))
  20. if err != nil {
  21. return &quot;&quot;, err
  22. }
  23. n := num.Int64()
  24. if n &gt; 32 &amp;&amp; n &lt; 127 {
  25. result += string(n)
  26. }
  27. }
  28. }
  29. func main() {
  30. suite := edwards25519.NewBlakeSHA256Ed25519()
  31. X := suite.Point().Pick(rng)
  32. Y := suite.Point().Pick(rng)
  33. a := suite.Scalar().Pick(suite.RandomStream())
  34. b := suite.Scalar().Pick(suite.RandomStream())
  35. fmt.Printf(&quot;Titik yang disepakati Alice dan Bob :\n X:\t%s\n Y:\t%s\n\n&quot;, X, Y)
  36. fmt.Printf(&quot;Kunci Private Alice:\n %s\n\n&quot;, a)
  37. fmt.Printf(&quot;Kunci Private Bob:\n %s\n\n&quot;, b)
  38. aX := suite.Point().Mul(a, X)
  39. aY := suite.Point().Mul(a, Y)
  40. bX := suite.Point().Mul(b, X)
  41. bY := suite.Point().Mul(b, Y)
  42. //Punya Alice
  43. abX := suite.Point().Mul(a, bX)
  44. abY := suite.Point().Mul(a, bY)
  45. //Punya Bob
  46. baX := suite.Point().Mul(b, aX)
  47. baY := suite.Point().Mul(b, aY)
  48. fmt.Printf(&quot;Kunci Rahasia Alice:\n abX : %s\n abY : %s\n\n&quot;, abX, abY)
  49. fmt.Printf(&quot;Kunci Rahasia Bob:\n baX : %s\n baY : %s\n&quot;, baX, baY)
  50. data := &quot;data&quot;
  51. h := hmac.New(sha256.New, abX)
  52. h.Write([]byte(data))
  53. sha := hex.EncodeToString(h.Sum(nil))
  54. fmt.Println(&quot;Hasil Hash Alice: &quot; + sha)
  55. g := hmac.New(sha256.New, baY)
  56. g.Write([]byte(data))
  57. sha2 := hex.EncodeToString(g.Sum(nil))
  58. fmt.Println(&quot;Hasil Hash Bob: &quot; + sha2)
  59. }

I am getting below errors when running the code:

  1. ./prog.go:77:15: cannot use abX (type kyber.Point) as type []byte in argument to hmac.New
  2. ./prog.go:90:15: cannot use baY (type kyber.Point) as type []byte in argument to hmac.New

I want abX/abY and baX/baY to Byte type.

答案1

得分: 0

如果你指的是kyber,这个库的Point接口有一个Data方法,用于返回嵌入的字节数据。

方法签名:Data() ([]byte, error)

由于abXbaY都是Point类型,你可以直接调用它们的Data方法来获取字节数据。

  1. package main
  2. import (
  3. "crypto/hmac"
  4. "crypto/rand"
  5. "crypto/sha256"
  6. "encoding/hex"
  7. "fmt"
  8. "math/big"
  9. "go.dedis.ch/kyber/v3/group/edwards25519"
  10. "go.dedis.ch/kyber/v3/util/random"
  11. )
  12. var rng = random.New()
  13. func GenerateRandomASCIIString(length int) (string, error) {
  14. result := ""
  15. for {
  16. if len(result) >= length {
  17. return result, nil
  18. }
  19. num, err := rand.Int(rand.Reader, big.NewInt(int64(127)))
  20. if err != nil {
  21. return "", err
  22. }
  23. n := num.Int64()
  24. if n > 32 && n < 127 {
  25. result += string(n)
  26. }
  27. }
  28. }
  29. func main() {
  30. suite := edwards25519.NewBlakeSHA256Ed25519()
  31. X := suite.Point().Pick(rng)
  32. Y := suite.Point().Pick(rng)
  33. a := suite.Scalar().Pick(suite.RandomStream())
  34. b := suite.Scalar().Pick(suite.RandomStream())
  35. fmt.Printf("Titik yang disepakati Alice dan Bob :\n X:\t%s\n Y:\t%s\n\n", X, Y)
  36. fmt.Printf("Kunci Private Alice:\n %s\n\n", a)
  37. fmt.Printf("Kunci Private Bob:\n %s\n\n", b)
  38. aX := suite.Point().Mul(a, X)
  39. aY := suite.Point().Mul(a, Y)
  40. bX := suite.Point().Mul(b, X)
  41. bY := suite.Point().Mul(b, Y)
  42. //Punya Alice
  43. abX := suite.Point().Mul(a, bX)
  44. abY := suite.Point().Mul(a, bY)
  45. //Punya Bob
  46. baX := suite.Point().Mul(b, aX)
  47. baY := suite.Point().Mul(b, aY)
  48. fmt.Printf("Kunci Rahasia Alice:\n abX : %s\n abY : %s\n\n", abX, abY)
  49. fmt.Printf("Kunci Rahasia Bob:\n baX : %s\n baY : %s\n", baX, baY)
  50. data, err := abX.Data()
  51. if err != nil {
  52. fmt.Println("提取字节时出错")
  53. }
  54. fmt.Printf("数据: %+v", data)
  55. h := hmac.New(sha256.New, data)
  56. h.Write([]byte(data))
  57. sha := hex.EncodeToString(h.Sum(nil))
  58. fmt.Println("Alice的哈希结果: " + sha)
  59. data, err = baY.Data()
  60. if err != nil {
  61. fmt.Println("提取字节时出错")
  62. }
  63. fmt.Printf("数据: %+v", data)
  64. g := hmac.New(sha256.New, data)
  65. g.Write([]byte(data))
  66. sha2 := hex.EncodeToString(g.Sum(nil))
  67. fmt.Println("Bob的哈希结果: " + sha2)
  68. }
英文:

If you are referring to kyber

Point interface of this library has a Data method which returns embedded byte data.

> Method Signature: Data() ([]byte, error).

Since abX and baY are both Point type you can directly call Data method on it to retrieve bytes.

  1. package main
  2. import (
  3. &quot;crypto/hmac&quot;
  4. &quot;crypto/rand&quot;
  5. &quot;crypto/sha256&quot;
  6. &quot;encoding/hex&quot;
  7. &quot;fmt&quot;
  8. &quot;math/big&quot;
  9. &quot;go.dedis.ch/kyber/v3/group/edwards25519&quot;
  10. &quot;go.dedis.ch/kyber/v3/util/random&quot;
  11. )
  12. var rng = random.New()
  13. func GenerateRandomASCIIString(length int) (string, error) {
  14. result := &quot;&quot;
  15. for {
  16. if len(result) &gt;= length {
  17. return result, nil
  18. }
  19. num, err := rand.Int(rand.Reader, big.NewInt(int64(127)))
  20. if err != nil {
  21. return &quot;&quot;, err
  22. }
  23. n := num.Int64()
  24. if n &gt; 32 &amp;&amp; n &lt; 127 {
  25. result += string(n)
  26. }
  27. }
  28. }
  29. func main() {
  30. suite := edwards25519.NewBlakeSHA256Ed25519()
  31. X := suite.Point().Pick(rng)
  32. Y := suite.Point().Pick(rng)
  33. a := suite.Scalar().Pick(suite.RandomStream())
  34. b := suite.Scalar().Pick(suite.RandomStream())
  35. fmt.Printf(&quot;Titik yang disepakati Alice dan Bob :\n X:\t%s\n Y:\t%s\n\n&quot;, X, Y)
  36. fmt.Printf(&quot;Kunci Private Alice:\n %s\n\n&quot;, a)
  37. fmt.Printf(&quot;Kunci Private Bob:\n %s\n\n&quot;, b)
  38. aX := suite.Point().Mul(a, X)
  39. aY := suite.Point().Mul(a, Y)
  40. bX := suite.Point().Mul(b, X)
  41. bY := suite.Point().Mul(b, Y)
  42. //Punya Alice
  43. abX := suite.Point().Mul(a, bX)
  44. abY := suite.Point().Mul(a, bY)
  45. //Punya Bob
  46. baX := suite.Point().Mul(b, aX)
  47. baY := suite.Point().Mul(b, aY)
  48. fmt.Printf(&quot;Kunci Rahasia Alice:\n abX : %s\n abY : %s\n\n&quot;, abX, abY)
  49. fmt.Printf(&quot;Kunci Rahasia Bob:\n baX : %s\n baY : %s\n&quot;, baX, baY)
  50. data, err := abX.Data()
  51. if err != nil {
  52. fmt.Println(&quot;Someting went wrong while extracting bytes&quot;)
  53. }
  54. fmt.Printf(&quot;DATA: %+v&quot;, data);
  55. h := hmac.New(sha256.New, data)
  56. h.Write([]byte(data))
  57. sha := hex.EncodeToString(h.Sum(nil))
  58. fmt.Println(&quot;Hasil Hash Alice: &quot; + sha)
  59. data, err = baY.Data()
  60. if err != nil {
  61. fmt.Println(&quot;Someting went wrong while extracting bytes&quot;)
  62. }
  63. fmt.Printf(&quot;DATA: %+v&quot;, data);
  64. g := hmac.New(sha256.New, data)
  65. g.Write([]byte(data))
  66. sha2 := hex.EncodeToString(g.Sum(nil))
  67. fmt.Println(&quot;Hasil Hash Bob: &quot; + sha2)
  68. }

huangapple
  • 本文由 发表于 2022年2月25日 00:49:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/71255452.html
匿名

发表评论

匿名网友

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

确定