Golang curve25519公钥生成器

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

Golang curve25519 pubkey generator

问题

我正在尝试实现curve25519算法用于公钥生成。但是我在如何从Go中的sha256编码字符串生成公钥方面遇到了困难。

我在Python中使用sha256编码字符串生成公钥没有问题:

import hashlib
import x25519

example_string = 'kerielle'

sha = hashlib.sha256(example_string.encode()).hexdigest()
hash_bytes = bytes.fromhex(sha)
public_key = x25519.scalar_base_mult(hash_bytes)
hex_res = public_key.hex()

print(hex_res)
>>> 0fa065fcaedecef9aebb5c79ea1c613e82bc5534c4b139d71f3a1cb0cb956652

但是在Go中如何做到这一点呢?以下是一个基本示例:

var publicKey [32]byte
privateKey := (*[32]byte)(bytes.Repeat([]byte("1"), 32))
curve25519.ScalarBaseMult(&publicKey, privateKey)

fmt.Printf("%x\n", publicKey)
>>> 04f5f29162c31a8defa18e6e742224ee806fc1718a278be859ba5620402b8f3a
英文:

I'm trying to implement the curve25519 algo for pubkey generation. But I got stuck on how to generate pubkey from sha256 encoded string in Go?

I have no problems with generation pubkey from sha256 encoded string via Python:

import hashlib
import x25519

example_string = 'kerielle'

sha = hashlib.sha256(example_string.encode()).hexdigest()
hash_bytes = bytes.fromhex(sha)
public_key = x25519.scalar_base_mult(hash_bytes)
hex_res = public_key.hex()

print(hex_res)
>>> 0fa065fcaedecef9aebb5c79ea1c613e82bc5534c4b139d71f3a1cb0cb956652

But how to do the same in Go? Here basic example:

var publicKey [32]byte
privateKey := (*[32]byte)(bytes.Repeat([]byte("1"), 32))
curve25519.ScalarBaseMult(&publicKey, privateKey)

fmt.Printf("%x\n", publicKey)
>>> 04f5f29162c31a8defa18e6e742224ee806fc1718a278be859ba5620402b8f3a

答案1

得分: 3

这只是复制了Python代码示例的结果,并考虑了curve25519.ScalarBaseMult()文档中的建议:

> 建议使用X25519函数与Basepoint一起使用,因为将数据复制到固定大小的数组中可能会导致意外的错误。

package main

import (
	"crypto/sha256"
	"fmt"
	"log"

	"golang.org/x/crypto/curve25519"
)

func main() {
	example_string := "kerielle"
	hash := sha256.Sum256([]byte(example_string))
	publicKey, err := curve25519.X25519(hash[:], curve25519.Basepoint)
	if err != nil {
		log.Fatalf("curve25519.X25519() 失败: %v", err)
	}
	fmt.Printf("%x\n", publicKey)
}

输出:

0fa065fcaedecef9aebb5c79ea1c613e82bc5534c4b139d71f3a1cb0cb956652

Go Playground

英文:

This simply replicates the result of the Python code sample while also taking into account guidance from the curve25519.ScalarBaseMult() documentation:

> It is recommended to use the X25519 function with Basepoint instead,
> as copying into fixed size arrays can lead to unexpected bugs.

package main

import (
	"crypto/sha256"
	"fmt"
	"log"

	"golang.org/x/crypto/curve25519"
)

func main() {
	example_string := "kerielle"
	hash := sha256.Sum256([]byte(example_string))
	publicKey, err := curve25519.X25519(hash[:], curve25519.Basepoint)
	if err != nil {
		log.Fatalf("curve25519.X25519() failed: %v", err)
	}
	fmt.Printf("%x\n", publicKey)
}

Output:

0fa065fcaedecef9aebb5c79ea1c613e82bc5534c4b139d71f3a1cb0cb956652

Go Playground

huangapple
  • 本文由 发表于 2022年1月16日 11:37:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/70727192.html
匿名

发表评论

匿名网友

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

确定