secp256k1签名及验签 GO

package custom_util

import (
	"encoding/hex"
	"encoding/json"
	"fmt"
	"github.com/btcsuite/btcd/btcec/v2"
	"github.com/btcsuite/btcd/btcec/v2/ecdsa"
	"github.com/btcsuite/btcd/chaincfg/chainhash"
	"math/big"
)

func SignAndVerifyTest(reqBody json.RawMessage) bool {
	priKeyStr := "55331231652752707450534414287730188629145955228891758798201450763340552825166"
	priKey10Bigint := new(big.Int)
	priKey10Bigint.SetString(priKeyStr, 10)
	priKeyByteArray := priKey10Bigint.Bytes()
	pubKeyStr := "11798277315441798127238305597212517654332197982978466864717364245859439644690363141941628373531488122609933560694747426926865495896421628851963848417303440"
	pubKey10Bigint := new(big.Int)
	pubKey10Bigint.SetString(pubKeyStr, 10)
	pubKeyHex := hex.EncodeToString(pubKey10Bigint.Bytes())
	pubKeyHexStr := "04" + pubKeyHex
	//2 内置的string工具包
	fmt.Println("pubKey Hex String: ", pubKeyHexStr)
	pubKeyByteArray, err := hex.DecodeString(pubKeyHexStr)
	if err != nil {
		fmt.Println("Unable to convert hex to byte. ", err)
	}
	// 使用私钥签名一些数据
	msg := []byte(reqBody)
	msgHash := chainhash.DoubleHashB(msg)

	privKey, _ := btcec.PrivKeyFromBytes(priKeyByteArray)
	msgSign := ecdsa.Sign(privKey, msgHash)
	fmt.Printf("Signature: %x\n", msgSign.Serialize())
	signByte := msgSign.Serialize()
	fmt.Printf("signByte: %x\n", signByte)
	// 拼接转换 []byte 到 string
	sign := hex.EncodeToString(signByte)
	fmt.Printf("signStr: " + sign)
	// 验证签名
	pubKey, err := btcec.ParsePubKey(pubKeyByteArray)
	if err != nil {
		fmt.Println(err)
		return false
	}
	sigBytes, err := hex.DecodeString(sign)
	if err != nil {
		fmt.Println(err)
		return false
	}
	fmt.Printf("hex ")
	signature, err := ecdsa.ParseSignature(sigBytes)
	if err != nil {
		fmt.Println(err)
		return false
	}
	ok := signature.Verify(msgHash, pubKey)
	return ok
}

func Sign(reqBody json.RawMessage, priKeyStr string) string {
	//priKeyStr := "55331231652752707450534414287730188629145955228891758798201450763340552825166"
	priKey10Bigint := new(big.Int)
	priKey10Bigint.SetString(priKeyStr, 10)
	priKeyByteArray := priKey10Bigint.Bytes()
	// 使用私钥签名一些数据
	msg := []byte(reqBody)
	msgHash := chainhash.DoubleHashB(msg)
	privKey, _ := btcec.PrivKeyFromBytes(priKeyByteArray)
	msgSign := ecdsa.Sign(privKey, msgHash)
	fmt.Printf("Signature: %x\n", msgSign.Serialize())
	signByte := msgSign.Serialize()
	fmt.Printf("signByte: %x\n", signByte)
	// 拼接转换 []byte 到 string
	sign := hex.EncodeToString(signByte)
	fmt.Printf("signStr: " + sign)
	return sign
}

func VerifySignature(reqBody json.RawMessage, msgSignHex string, pubKeyStr string) bool {
	//pubKeyStr := "11798277315441798127238305597212517654332197982978466864717364245859439644690363141941628373531488122609933560694747426926865495896421628851963848417303440"
	pubKey10Bigint := new(big.Int)
	pubKey10Bigint.SetString(pubKeyStr, 10)
	pubKeyHex := hex.EncodeToString(pubKey10Bigint.Bytes())
	pubKeyHexStr := "04" + pubKeyHex
	//2 内置的string工具包
	fmt.Println("pubKey Hex String: ", pubKeyHexStr)
	pubKeyByteArray, err := hex.DecodeString(pubKeyHexStr)
	if err != nil {
		fmt.Println("Unable to convert hex to byte. ", err)
	}
	fmt.Printf("msgSignHex: " + msgSignHex)
	// 验证签名
	pubKey, err := btcec.ParsePubKey(pubKeyByteArray)
	if err != nil {
		fmt.Println(err)
		return false
	}
	sigBytes, err := hex.DecodeString(msgSignHex)
	if err != nil {
		fmt.Println(err)
		return false
	}
	fmt.Printf("hex ")
	signature, err := ecdsa.ParseSignature(sigBytes)
	if err != nil {
		fmt.Println(err)
		return false
	}
	msg := []byte(reqBody)
	msgHash := chainhash.DoubleHashB(msg)
	ok := signature.Verify(msgHash, pubKey)
	return ok
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Java中使用secp256k1签名并恢复公钥,你可以使用Bouncy Castle库。以下是一个示例代码: ```java import org.bouncycastle.crypto.digests.SHA256Digest; import org.bouncycastle.crypto.params.ECDomainParameters; import org.bouncycastle.crypto.params.ECPrivateKeyParameters; import org.bouncycastle.crypto.params.ECPublicKeyParameters; import org.bouncycastle.crypto.signers.ECDSASigner; import org.bouncycastle.math.ec.ECPoint; import org.bouncycastle.util.encoders.Hex; import java.math.BigInteger; import java.security.SecureRandom; public class ECDSAExample { public static void main(String[] args) { // Generate a random private key SecureRandom random = new SecureRandom(); BigInteger privateKey = new BigInteger(256, random); // Define the secp256k1 curve parameters ECDomainParameters curve = ECNamedCurveTable.getByName("secp256k1"); // Create an EC private key object ECPrivateKeyParameters privateKeyParams = new ECPrivateKeyParameters(privateKey, curve); // Create an EC signer object ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest())); // Initialize the signer with the private key signer.init(true, privateKeyParams); // Generate a random message to sign byte[] message = "Hello, world!".getBytes(); // Calculate the signature BigInteger[] signature = signer.generateSignature(message); // Print the signature System.out.println("Signature: " + Hex.toHexString(signature[0].toByteArray()) + Hex.toHexString(signature[1].toByteArray())); // Recover the public key from the signature ECPoint publicKey = signer.getPublicKey(); // Create an EC public key object ECPublicKeyParameters publicKeyParams = new ECPublicKeyParameters(publicKey, curve); // Verify the signature using the recovered public key signer.init(false, publicKeyParams); boolean valid = signer.verifySignature(message, signature[0], signature[1]); System.out.println("Valid signature? " + valid); } } ``` 这个示例代码使用Bouncy Castle库生成一个随机的私钥,使用secp256k1曲线参数创建一个EC私钥对象,并使用ECDSASigner进行签名和验证。签名后,公钥从签名中恢复,并用于验证签名

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值