[Node.js] SHA256-RSA 验证签名 Node.js sign Golang verifySign

源代码

OpenSSL 生成 PKCS1 私钥公钥

openssl genrsa -out publicKey.pem 2048
openssl rsa -in privateKey.pem -pubout -out publicKey.pem

NodeJS 签名验签写入sign , Golang读取Sign验签 ps: pkcs8 密钥须转换

NodeJS 验证签名 SHA256-RSA

// rsa_test.js
const crypto            = require('crypto')
const fs                = require('fs')
const publicKey         = fs.readFileSync("publicKey.pem")
const privateKey        = fs.readFileSync("privateKey.pem")
const dataStr           = "verifiableData"

const signer            = crypto.createSign('sha256')
signer.update(dataStr)
const sign              = signer.sign(privateKey,'base64')

const verifier          = crypto.createVerify('sha256')
verifier.update(dataStr);
const ver = verifier.verify(publicKey, sign,'base64');
console.log(sign,ver);

fs.writeFileSync("sign.txt",sign)
TIP

oCvBK9YEJe58gkB1jzoTPOH4ToEa...==

true

Golang 验证签名 SHA256-RSA

// rsa_test.go
package main

import (
  "crypto"
  "log"
  "os"
  "testing"
)

func TestRsa2Rsa2PriSign(t *testing.T) {

	PUBLIC_KEY, _ := os.ReadFile("./publicKey.pem")
	signTxt, _ := os.ReadFile("./sign.txt")
	signStr := string(signTxt)
	publicKey := string(PUBLIC_KEY)

	str := "verifiableData"
	res := RSAPubCheckSign(str, signStr, publicKey, crypto.SHA256)
	fmt.Println(signStr)
	fmt.Println(res)

}