在Node.js算法rsa-sha1 private_key.pem中创建签名

时间:2019-07-10 10:09:05

标签: ruby-on-rails node.js openssl rsa sha1

我可以在下面的Nodejs中创建类似代码的签名吗?

 # Load PRIVATE key
    private_key = OpenSSL::PKey::RSA.new(File.read(Rails.root + ENV['EPAY_PRIVATE_KEY']))

    # Sign your data
    signMessage = private_key.sign(OpenSSL::Digest::SHA1.new, message)

    # Base64 message
    baseMessage = Base64.encode64(signMessage.to_s)

1 个答案:

答案 0 :(得分:0)

您应该能够在Node.js中执行相同的操作,创建签名非常容易,例如:

const crypto = require('crypto');
const fs = require('fs');
const privateKey = fs.readFileSync('./private-key.pem', 'utf8');

const message = "some message data";
const sign = crypto.createSign('SHA1');
sign.update(message);
sign.end();
const signature = sign.sign(privateKey);
console.log("Signature: ", signature.toString('base64'));

这将创建邮件的base64编码的SHA1签名。