import * as crypto from 'crypto'; export interface KeyPair { privateKey: string; publicKey: string; } export function generateKeyPair(): KeyPair { const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', { modulusLength: 2048, publicKeyEncoding: { type: 'spki', format: 'pem' }, privateKeyEncoding: { type: 'pkcs8', format: 'pem' } }); return { privateKey, publicKey }; } export function signData(data: string, privateKey: string): string { const sign = crypto.createSign('SHA256'); sign.update(data); sign.end(); return sign.sign(privateKey, 'base64'); } export function verifySignature(data: string, signature: string, publicKey: string): boolean { const verify = crypto.createVerify('SHA256'); verify.update(data); verify.end(); return verify.verify(publicKey, signature, 'base64'); }