Ad

Create Signature In Nodejs Algorithm Rsa-sha1 Private_key.pem

Can I create signature like code below in 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)
Ad

Answer

You should be able to do the same thing in Node.js, creating a signature is quite easy, for example:

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'));

This creates a base64 encoded SHA1 signature of the message.

Ad
source: stackoverflow.com
Ad