Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript验证golang ecdsa库生成的签名';椭圆图书馆_Javascript_Typescript_Go_Cryptography_Elliptic Curve - Fatal编程技术网

使用javascript验证golang ecdsa库生成的签名';椭圆图书馆

使用javascript验证golang ecdsa库生成的签名';椭圆图书馆,javascript,typescript,go,cryptography,elliptic-curve,Javascript,Typescript,Go,Cryptography,Elliptic Curve,此线程是找到的旧线程的延续 在前一个线程中,使用golang的椭圆库成功完成了成功验证javascript椭圆库生成的签名的目标。这条线索的问题是如何实现相反的结果?这就是在javascript中成功验证使用golang生成的有效数字签名。使用的椭圆曲线是secp256k1 Golang实用程序功能: package utils import ( "crypto/ecdsa" "crypto/rand" "crypto/

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

在前一个线程中,使用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代码没有bug

使用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签名验证评估为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
}

您的go代码无效,无法在以下位置编译:=ecdsa.Sign(rand.Reader,privateKeyStruct,[]字节(哈希))哈希是
[]字节
,无法写入
[]字节(哈希)
无需将哈希作为字符串,然后作为字节数组。您可以这样写来获得一个字节片。请共享一个完整的可复制go代码来生成公钥和签名。Golang代码已相应更新