const crypto = require('crypto');
const fs = require('fs');
// Generate a keypair for this example
function generateKeyPair() {
return crypto.generateKeyPairSync('rsa', {
modulusLength: 2048,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem'
}
});
}
// For this example, generate keys in memory
// In a real application, you would load existing keys from storage
const { privateKey, publicKey } = generateKeyPair();
// Message to sign
const message = 'This is a message to be signed';
// Create a Sign object
const sign = crypto.createSign('SHA256');
// Update with the message
sign.update(message);
// Sign the message with the private key
const signature = sign.sign(privateKey, 'hex');
console.log('Message:', message);
console.log('Signature:', signature);
// We'll save these for the verification example
fs.writeFileSync('message.txt', message);
fs.writeFileSync('signature.hex', signature);
fs.writeFileSync('public_key.pem', publicKey);