Get your own Node server
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);

              
Message: This is a message to be signed
Signature: 1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b1c2d3e4f5a6b7c8d9e0f1a2b3