登录
首页 >  Golang >  Go问答

验证由 Golang ECDSA 库生成的签名,使用 JavaScript 的椭圆曲线库

来源:stackoverflow

时间:2024-02-25 10:36:27 495浏览 收藏

目前golang学习网上已经有很多关于Golang的文章了,自己在初次阅读这些文章中,也见识到了很多学习思路;那么本文《验证由 Golang ECDSA 库生成的签名,使用 JavaScript 的椭圆曲线库》,也希望能帮助到大家,如果阅读完后真的对你学习Golang有帮助,欢迎动动手指,评论留言并分享~

问题内容

此线程是此处找到的旧线程的延续

在上一个线程中,使用 golang 的椭圆库成功实现了成功验证从 javascript 的椭圆库生成的签名的目标。这个帖子的问题是如何实现相反的效果?即成功验证了在 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 代码没有错误。

客户端(javascript)上的演示代码使用 golang ecdsa 库生成的值:

// 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 椭圆库

golang edcsa 库


解决方案


我已经自己解决了这个问题。解决方案是在生成签名之前将哈希转换为字节数组。执行此操作后,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
}

到这里,我们也就讲完了《验证由 Golang ECDSA 库生成的签名,使用 JavaScript 的椭圆曲线库》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>