使用JavaScript的椭圆库验证从golang ecdsa库生成的签名 [英] Verifying signature generated from golang ecdsa library using javascript's elliptic library

查看:200
本文介绍了使用JavaScript的椭圆库验证从golang ecdsa库生成的签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此线程是在此处

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

In the previous thread the objective of successfully verifying a signature generated from javascript's elliptic library was successfully accomplished using golang's elliptic library. The question of this thread is how can the reverse be achieved? That is successfully verifying a valid digital signature generated using golang in javascript. The elliptic curve being used is the secp256k1.

Golang实用程序功能:

Golang utility functions:

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函数中。

The issue exists in the SignExternalMessage function in code sample above.

负责验证签名的Typescript函数:

The Typescript function responsible for verifying the signature:

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
    ) { }
}

上面的打字稿代码没有错误。

The Typescript code above has no bugs.

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

Demo code on client (Javascript) using values generated from the golang ecdsa library:

// 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)}`)

代码

如果您有任何疑问或建议,请在讨论部分中进行讨论。

If you have any question or suggestions please discourse in the discussion section.

JavaScript椭圆库

Golang edcsa库

推荐答案

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

I've managed to solve the problem on my own. The solution was to cast the hash as a byte-array before generating the signature. After doing this the Javascript signature verification evaluated to true. Below is the corrected function:

//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的椭圆库验证从golang ecdsa库生成的签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆