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

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

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

问题

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

我想要使用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转换为字节类型。

英文:

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:

package main

import (
	&quot;crypto/hmac&quot;
	&quot;crypto/rand&quot;
	&quot;crypto/sha256&quot;
	&quot;encoding/hex&quot;
	&quot;fmt&quot;
	&quot;math/big&quot;

	&quot;go.dedis.ch/kyber/v3/group/edwards25519&quot;
	&quot;go.dedis.ch/kyber/v3/util/random&quot;
)

var rng = random.New()

func GenerateRandomASCIIString(length int) (string, error) {
	result := &quot;&quot;
	for {
		if len(result) &gt;= length {
			return result, nil
		}
		num, err := rand.Int(rand.Reader, big.NewInt(int64(127)))
		if err != nil {
			return &quot;&quot;, err
		}
		n := num.Int64()
		if n &gt; 32 &amp;&amp; n &lt; 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(&quot;Titik yang disepakati Alice dan Bob :\n X:\t%s\n Y:\t%s\n\n&quot;, X, Y)
	fmt.Printf(&quot;Kunci Private Alice:\n %s\n\n&quot;, a)
	fmt.Printf(&quot;Kunci Private Bob:\n %s\n\n&quot;, 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(&quot;Kunci Rahasia Alice:\n abX : %s\n abY : %s\n\n&quot;, abX, abY)
	fmt.Printf(&quot;Kunci Rahasia Bob:\n baX : %s\n baY : %s\n&quot;, baX, baY)

	data := &quot;data&quot;

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

	h.Write([]byte(data))

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

	fmt.Println(&quot;Hasil Hash Alice: &quot; + sha)

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

	g.Write([]byte(data))

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

	fmt.Println(&quot;Hasil Hash Bob: &quot; + sha2)
}

I am getting below errors when running the code:

./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

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

答案1

得分: 0

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

方法签名: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("提取字节时出错")
	}
	fmt.Printf("数据: %+v", data)

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

	h.Write([]byte(data))

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

	fmt.Println("Alice的哈希结果: " + sha)

	data, err = baY.Data()
	if err != nil {
		fmt.Println("提取字节时出错")
	}
	fmt.Printf("数据: %+v", data)

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

	g.Write([]byte(data))

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

	fmt.Println("Bob的哈希结果: " + sha2)
}
英文:

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.

package main

import (
	&quot;crypto/hmac&quot;
	&quot;crypto/rand&quot;
	&quot;crypto/sha256&quot;
	&quot;encoding/hex&quot;
	&quot;fmt&quot;
	&quot;math/big&quot;

	&quot;go.dedis.ch/kyber/v3/group/edwards25519&quot;
	&quot;go.dedis.ch/kyber/v3/util/random&quot;
)

var rng = random.New()

func GenerateRandomASCIIString(length int) (string, error) {
	result := &quot;&quot;
	for {
		if len(result) &gt;= length {
			return result, nil
		}
		num, err := rand.Int(rand.Reader, big.NewInt(int64(127)))
		if err != nil {
			return &quot;&quot;, err
		}
		n := num.Int64()
		if n &gt; 32 &amp;&amp; n &lt; 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(&quot;Titik yang disepakati Alice dan Bob :\n X:\t%s\n Y:\t%s\n\n&quot;, X, Y)
	fmt.Printf(&quot;Kunci Private Alice:\n %s\n\n&quot;, a)
	fmt.Printf(&quot;Kunci Private Bob:\n %s\n\n&quot;, 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(&quot;Kunci Rahasia Alice:\n abX : %s\n abY : %s\n\n&quot;, abX, abY)
	fmt.Printf(&quot;Kunci Rahasia Bob:\n baX : %s\n baY : %s\n&quot;, baX, baY)

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

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

	h.Write([]byte(data))

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

	fmt.Println(&quot;Hasil Hash Alice: &quot; + sha)

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

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

	g.Write([]byte(data))

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

	fmt.Println(&quot;Hasil Hash Bob: &quot; + sha2)
}

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:

确定