将 Kyber.Point 转换为字节或使用 Kyber.Point 提取字节

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

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

我希望与 HMAC 一起使用的密钥 (ECDH) 具有散列密钥。

代码:

package main

import (
    "crypto/hmac"
    "crypto/rand"
    "crypto/sha256"
    "encoding/hex"
    "fmt"
    "math/big"

    "go.dedis.ch/kyber/v3/group/edwards25519"
    "go.dedis.ch/kyber/v3/util/random"
)

var rng = random.New()

func GenerateRandomASCIIString(length int) (string, error) {
    result := ""
    for {
        if len(result) >= length {
            return result, nil
        }
        num, err := rand.Int(rand.Reader, big.NewInt(int64(127)))
        if err != nil {
            return "", err
        }
        n := num.Int64()
        if n > 32 && n < 127 {
            result += string(n)
        }
    }
}
func main() {

    suite := edwards25519.NewBlakeSHA256Ed25519()

    X := suite.Point().Pick(rng)
    Y := suite.Point().Pick(rng)
    a := suite.Scalar().Pick(suite.RandomStream())
    b := suite.Scalar().Pick(suite.RandomStream())

    fmt.Printf("Titik yang disepakati Alice dan Bob :\n X:\t%s\n Y:\t%s\n\n", X, Y)
    fmt.Printf("Kunci Private Alice:\n %s\n\n", a)
    fmt.Printf("Kunci Private Bob:\n %s\n\n", b)

    aX := suite.Point().Mul(a, X)
    aY := suite.Point().Mul(a, Y)

    bX := suite.Point().Mul(b, X)
    bY := suite.Point().Mul(b, Y)

    //Punya Alice
    abX := suite.Point().Mul(a, bX)
    abY := suite.Point().Mul(a, bY)

    //Punya Bob
    baX := suite.Point().Mul(b, aX)
    baY := suite.Point().Mul(b, aY)

    fmt.Printf("Kunci Rahasia Alice:\n abX : %s\n abY : %s\n\n", abX, abY)
    fmt.Printf("Kunci Rahasia Bob:\n baX : %s\n baY : %s\n", baX, baY)

    data := "data"

    h := hmac.New(sha256.New, abX)

    h.Write([]byte(data))

    sha := hex.EncodeToString(h.Sum(nil))

    fmt.Println("Hasil Hash Alice: " + sha)

    g := hmac.New(sha256.New, baY)

    g.Write([]byte(data))

    sha2 := hex.EncodeToString(g.Sum(nil))

    fmt.Println("Hasil Hash Bob: " + sha2)
}

当 运行 代码:

时,我遇到以下错误
./prog.go:77:15: cannot use abX (type kyber.Point) as type []byte in argument to hmac.New
./prog.go:90:15: cannot use baY (type kyber.Point) as type []byte in argument to hmac.New

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

如果你指的是kyber

Point 这个库的接口有一个 Data 方法,它 returns 嵌入字节数据。

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

由于 abXbaY 都是 Point 类型,您可以直接调用 Data 方法来检索字节。

package main

import (
    "crypto/hmac"
    "crypto/rand"
    "crypto/sha256"
    "encoding/hex"
    "fmt"
    "math/big"

    "go.dedis.ch/kyber/v3/group/edwards25519"
    "go.dedis.ch/kyber/v3/util/random"
)

var rng = random.New()

func GenerateRandomASCIIString(length int) (string, error) {
    result := ""
    for {
        if len(result) >= length {
            return result, nil
        }
        num, err := rand.Int(rand.Reader, big.NewInt(int64(127)))
        if err != nil {
            return "", err
        }
        n := num.Int64()
        if n > 32 && n < 127 {
            result += string(n)
        }
    }
}
func main() {

    suite := edwards25519.NewBlakeSHA256Ed25519()

    X := suite.Point().Pick(rng)
    Y := suite.Point().Pick(rng)
    a := suite.Scalar().Pick(suite.RandomStream())
    b := suite.Scalar().Pick(suite.RandomStream())

    fmt.Printf("Titik yang disepakati Alice dan Bob :\n X:\t%s\n Y:\t%s\n\n", X, Y)
    fmt.Printf("Kunci Private Alice:\n %s\n\n", a)
    fmt.Printf("Kunci Private Bob:\n %s\n\n", b)

    aX := suite.Point().Mul(a, X)
    aY := suite.Point().Mul(a, Y)

    bX := suite.Point().Mul(b, X)
    bY := suite.Point().Mul(b, Y)

    //Punya Alice
    abX := suite.Point().Mul(a, bX)
    abY := suite.Point().Mul(a, bY)

    //Punya Bob
    baX := suite.Point().Mul(b, aX)
    baY := suite.Point().Mul(b, aY)

    fmt.Printf("Kunci Rahasia Alice:\n abX : %s\n abY : %s\n\n", abX, abY)
    fmt.Printf("Kunci Rahasia Bob:\n baX : %s\n baY : %s\n", baX, baY)

    data, err := abX.Data()
    if err != nil {
      fmt.Println("Someting went wrong while extracting bytes")
    }
    fmt.Printf("DATA: %+v", data);

    h := hmac.New(sha256.New, data)

    h.Write([]byte(data))

    sha := hex.EncodeToString(h.Sum(nil))

    fmt.Println("Hasil Hash Alice: " + sha)

    data, err = baY.Data()
    if err != nil {
      fmt.Println("Someting went wrong while extracting bytes")
    }
    fmt.Printf("DATA: %+v", data);

    g := hmac.New(sha256.New, data)

    g.Write([]byte(data))

    sha2 := hex.EncodeToString(g.Sum(nil))

    fmt.Println("Hasil Hash Bob: " + sha2)
}