const crypto = require('crypto');
// Create an ECDH instance
const ecdh = crypto.createECDH('prime256v1');
ecdh.generateKeys();
// Get public key in different formats
const uncompressedKey = ecdh.getPublicKey('hex', 'uncompressed');
const compressedKey = ecdh.getPublicKey('hex', 'compressed');
const hybridKey = ecdh.getPublicKey('hex', 'hybrid');
console.log('Public Key Formats:');
console.log('='.repeat(50));
console.log('Uncompressed (65 bytes):', uncompressedKey);
console.log('Compressed (33 bytes): ', compressedKey);
console.log('Hybrid (65 bytes): ', hybridKey);
// Get key length in each format
console.log('\nKey lengths:');
console.log('='.repeat(50));
console.log('Uncompressed:', Buffer.from(uncompressedKey, 'hex').length, 'bytes');
console.log('Compressed: ', Buffer.from(compressedKey, 'hex').length, 'bytes');
console.log('Hybrid: ', Buffer.from(hybridKey, 'hex').length, 'bytes');
// Test key exchange with different formats
const otherEcdh = crypto.createECDH('prime256v1');
otherEcdh.generateKeys();
console.log('\nKey Exchange with Different Formats:');
console.log('='.repeat(50));
// Compute secrets using different formats
const secret1 = otherEcdh.computeSecret(Buffer.from(uncompressedKey, 'hex'));
const secret2 = otherEcdh.computeSecret(Buffer.from(compressedKey, 'hex'));
const secret3 = otherEcdh.computeSecret(Buffer.from(hybridKey, 'hex'));
console.log('Same secret from uncompressed?', secret1.equals(secret2));
console.log('Same secret from compressed? ', secret1.equals(secret3));
console.log('All secrets identical? ', secret1.equals(secret2) && secret2.equals(secret3));