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 (
"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)
}
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.
CodePudding user response:
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 (
"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)
}