const crypto = require('crypto');
// Generate a keypair for this example
const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', {
modulusLength: 2048,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem'
}
});
// Create a Sign object
const sign = crypto.createSign('SHA256');
// Update with multiple pieces of data
sign.update('First part of the message. ');
sign.update('Second part of the message. ');
sign.update('Third part of the message.');
// Create the signature
const signature = sign.sign(privateKey, 'hex');
console.log('Combined message: First part of the message. Second part of the message. Third part of the message.');
console.log('Signature:', signature);
// You can achieve the same result with a single update
const singleSign = crypto.createSign('SHA256');
singleSign.update('First part of the message. Second part of the message. Third part of the message.');
const singleSignature = singleSign.sign(privateKey, 'hex');
console.log('Single update signature matches multiple updates?', singleSignature === signature);