gpt4 book ai didi

javascript - 使用JavaScript的椭圆库验证从golang ecdsa库生成的签名

转载 作者:行者123 更新时间:2023-12-01 22:20:13 25 4
gpt4 key购买 nike

该线程是here找到的旧线程的延续
在上一个线程中,成功验证从javascript椭圆库生成的签名的目标是使用golang椭圆库成功实现的。该线程的问题是如何实现反向转换?可以成功验证使用javascript中的golang生成的有效数字签名。使用的椭圆曲线是secp256k1。
Golang实用程序功能:

package utils

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

"github.com/secp256k1"
)

//GeneratePrivateKey : ecdsa.PrivateKey
func GeneratePrivateKey() (*big.Int, error) {
var privateKey *ecdsa.PrivateKey
var privateKeyGenerationError error
privateKey, privateKeyGenerationError = ecdsa.GenerateKey(secp256k1.S256(), rand.Reader)
if privateKeyGenerationError != nil {
return privateKey.D, privateKeyGenerationError
}
return privateKey.D, nil
}

//GeneratePublicKey :
func GeneratePublicKey(privateKey *big.Int) ecdsa.PublicKey {
var pri ecdsa.PrivateKey
pri.D, _ = new(big.Int).SetString(fmt.Sprintf("%x", privateKey), 16)
pri.PublicKey.Curve = secp256k1.S256()
pri.PublicKey.X, pri.PublicKey.Y = pri.PublicKey.Curve.ScalarBaseMult(pri.D.Bytes())

publicKey := ecdsa.PublicKey{
Curve: secp256k1.S256(),
X: pri.PublicKey.X,
Y: pri.PublicKey.Y,
}

return publicKey
}

//Signature :
type Signature struct {
R *big.Int
S *big.Int
}

//SignMessage : Generates a valid digital signature for golang's ecdsa library
func SignMessage(message string, privateKey *big.Int) (Signature, error) {
var result Signature
msgHash := fmt.Sprintf(
"%x",
sha256.Sum256([]byte(message)),
)
privateKeyStruct, privateKeyGenerationError := ecdsa.GenerateKey(secp256k1.S256(), rand.Reader)
if privateKeyGenerationError != nil {
return result, privateKeyGenerationError
}

privateKeyStruct.D = privateKey

signatureR, signatureS, signatureGenerationError := ecdsa.Sign(rand.Reader, privateKeyStruct, []byte(msgHash))
if signatureGenerationError != nil {
return result, signatureGenerationError
}
result.R = signatureR
result.S = signatureS
return result, nil
}

//SignExternalMessage : Generates a valid digital signature for javascript's elliptic library https://github.com/indutny/elliptic
func SignExternalMessage(message string, privateKey *big.Int) (Signature, error) {
var result Signature
msgHash := fmt.Sprintf(
"%x",
sha256.Sum256([]byte(message)),
)
privateKeyStruct, privateKeyGenerationError := ecdsa.GenerateKey(secp256k1.S256(), rand.Reader)
if privateKeyGenerationError != nil {
return result, privateKeyGenerationError
}

privateKeyStruct.D = privateKey
hash, hashDecodeError := hex.DecodeString(msgHash)

if hashDecodeError != nil {
return result, hashDecodeError
}

signatureR, signatureS, signatureGenerationError := ecdsa.Sign(rand.Reader, privateKeyStruct, hash)
if signatureGenerationError != nil {
return result, signatureGenerationError
}
result.R = signatureR
result.S = signatureS
return result, nil
}

//VerifyMessage : Verifies signatures generated using golang's ecdsa function
func VerifyMessage(message string, publicKey *ecdsa.PublicKey, signature Signature) (bool, error) {
msgHash := fmt.Sprintf(
"%x",
sha256.Sum256([]byte(message)),
)
return ecdsa.Verify(publicKey, []byte(msgHash), signature.R, signature.S), nil
}

//VerifyExternalMessage : Verifies signatures generated using the javascript elliptic library
// https://github.com/indutny/elliptic
func VerifyExternalMessage(message string, publicKey *ecdsa.PublicKey, signature Signature) (bool, error) {
msgHash := fmt.Sprintf(
"%x",
sha256.Sum256([]byte(message)),
)
hash, hashDecodeError := hex.DecodeString(msgHash)

if hashDecodeError != nil {
return false, hashDecodeError
}
return ecdsa.Verify(publicKey, hash, signature.R, signature.S), nil
}
上面的代码示例中的SignExternalMessage函数中存在此问题。
负责验证签名的Typescript函数:
declare const require: any;
var EC = require('elliptic').ec;
var ec = new EC('secp256k1');
const SHA256 = require("crypto-js/sha256");

public static verifySignature(message: string, publicKey: PublicKey, signature: Signature): boolean {
message = SHA256(message).toString();
const key = ec.keyFromPublic(publicKey, 'hex');
return key.verify(message, signature);
}

class PublicKey {
constructor(
public x: string,
public y: string
) { }
}

class Signature {
constructor(
public r: string,
public s: string,
public recoveryParam: number
) { }
}
上面的Typescript代码没有错误。
使用从golang ecdsa库生成的值在客户端(Javascript)上的演示代码:
// Public key generated using golang
const publicKey = {
x:'6847E5B259E624E3A6E04160CAE5837DE19699F4120BFA3E1FA5511B31E014DF',
y:'1F88E0AFB82D94DB71D99BD749ADE9865BCAE4696EF16709D832C97C4FE4A00F'
}

const message = "hello world"

// Signature generated using golang
const signature = {
r:'9B5D1059C54A60A2C885FD645E07F3066A38E2BB7435B2919877D193AC73F7DB',
s:'CB819507AE4A88522029C2DCF82290010E340243751FFC8AFE3F12A083713173'
}

console.log(`SIG VERIFICATION: ${verifySignature(message, publicKey, signature)}`)
上面的代码在应为true时计算为false。
如果您有任何问题或建议,请在“讨论”部分进行讨论。
Javascript Elliptic Library
Golang edcsa library

最佳答案

我设法自己解决了这个问题。解决方案是在生成签名之前将哈希转换为字节数组。完成此操作后,Javascript签名验证将评估为true。下面是更正的功能:

//SignExternalMessage : Generates a valid digital signature for javascript's elliptic library https://github.com/indutny/elliptic
func SignExternalMessage(message string, privateKey *big.Int) (Signature, error) {
var result Signature
msgHash := fmt.Sprintf(
"%x",
sha256.Sum256([]byte(message)),
)
privateKeyStruct, privateKeyGenerationError := ecdsa.GenerateKey(secp256k1.S256(), rand.Reader)
if privateKeyGenerationError != nil {
return result, privateKeyGenerationError
}

privateKeyStruct.D = privateKey
hash, hashDecodeError := hex.DecodeString(msgHash)

if hashDecodeError != nil {
return result, hashDecodeError
}

signatureR, signatureS, signatureGenerationError := ecdsa.Sign(rand.Reader, privateKeyStruct, []byte(hash))
if signatureGenerationError != nil {
return result, signatureGenerationError
}
result.R = signatureR
result.S = signatureS
return result, nil
}

关于javascript - 使用JavaScript的椭圆库验证从golang ecdsa库生成的签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63983356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com