英文:
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 (
"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.
答案1
得分: 0
如果你指的是kyber,这个库的Point接口有一个Data
方法,用于返回嵌入的字节数据。
方法签名:
Data() ([]byte, error)
。
由于abX
和baY
都是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 (
"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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论