Full answer
Asymmetric cryptography uses a key pair: a private key (secret) and a public key (shareable).
Signatures:
- Purpose: authenticity and integrity. Examples: signed software updates, JWTs with RS256 or EdDSA, signed API requests, license files.
- Ed25519 is a modern, fast signature scheme with small keys (32 bytes) and no parameter choices to get wrong.
- Node: crypto.generateKeyPairSync('ed25519'), crypto.sign(null, data, privateKey), crypto.verify(null, data, publicKey, signature). The algorithm argument is null for Ed25519, because the hash is part of the scheme.
- A signature does not hide the data. It only proves origin and integrity.
Key agreement:
- Purpose: two parties who have never shared a secret create one over an open channel. This is the core of TLS.
- X25519: each side generates a key pair, sends its public key, and computes crypto.diffieHellman({ privateKey, publicKey: otherPublic }). Both get the same bytes.
- Never use the raw shared secret as a key. Pass it through a KDF: crypto.hkdfSync('sha256', secret, salt, info, 32), which gives well-distributed key material bound to a context string (info).
- Plain key agreement does not authenticate the other side. An attacker in the middle can substitute public keys, so real protocols add signatures or certificates.
Choosing: signatures when someone must prove who made something. Key agreement when two parties need a fresh shared key for symmetric encryption. In practice, use TLS or a vetted protocol rather than building your own.
Key storage: export keys as PEM or JWK (keyObject.export), keep private keys in a secret manager or hardware module, and rotate them with key ids.
const crypto = require('node:crypto');
// 1. Digital signature with Ed25519
const { publicKey, privateKey } = crypto.generateKeyPairSync('ed25519');
const document = Buffer.from('Certificate: Asha completed Node.js, grade A');
const signature = crypto.sign(null, document, privateKey);
console.log('signature length:', signature.length, 'bytes');
console.log('valid signature verifies:', crypto.verify(null, document, publicKey, signature));
console.log('changed document fails: ', !crypto.verify(null, Buffer.from('Certificate: Asha completed Node.js, grade A+'), publicKey, signature));
console.log('another key fails: ', !crypto.verify(null, document, crypto.generateKeyPairSync('ed25519').publicKey, signature));
console.log('public key as PEM starts with:', publicKey.export({ type: 'spki', format: 'pem' }).split('\n')[0]);
// 2. Key agreement with X25519: Alice and Bob never send a secret.
const alice = crypto.generateKeyPairSync('x25519');
const bob = crypto.generateKeyPairSync('x25519');
const aliceSecret = crypto.diffieHellman({ privateKey: alice.privateKey, publicKey: bob.publicKey });
const bobSecret = crypto.diffieHellman({ privateKey: bob.privateKey, publicKey: alice.publicKey });
console.log('both sides computed the same secret:', aliceSecret.equals(bobSecret));
// 3. Turn the shared secret into a proper key with HKDF
const salt = crypto.randomBytes(16);
const keyForAlice = Buffer.from(crypto.hkdfSync('sha256', aliceSecret, salt, 'chat-messages-v1', 32));
const keyForBob = Buffer.from(crypto.hkdfSync('sha256', bobSecret, salt, 'chat-messages-v1', 32));
const otherPurpose = Buffer.from(crypto.hkdfSync('sha256', aliceSecret, salt, 'file-uploads-v1', 32));
console.log('derived keys match:', keyForAlice.equals(keyForBob));
console.log('a different context gives a different key:', !keyForAlice.equals(otherPurpose));Key pairs are random, so the snippet prints facts that are always true instead of key bytes. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): signature length: 64 bytes valid signature verifies: true changed document fails: true another key fails: true public key as PEM starts with: -----BEGIN PUBLIC KEY----- both sides computed the same secret: true derived keys match: true a different context gives a different key: true
Key points
- Signatures: private key signs, public key verifies.
- Ed25519 passes null as the algorithm to sign and verify.
- X25519 gives both sides the same shared secret.
- Run the shared secret through HKDF before using it.
- Key agreement alone does not prove identity.
Common mistakes
- Using the raw Diffie-Hellman output as an encryption key.
- Assuming key agreement proves who the other side is.
- Signing a hash that the verifier cannot reproduce.
- Keeping the private key in the repository.
Follow-ups
- How do JWT signature algorithms HS256 and EdDSA differ?
- What is forward secrecy?
- How do certificates solve the identity problem?
Tips
- Start from what each tool proves or creates.
- Say HKDF after the shared secret.
- Note that you would use TLS in practice, not hand-built protocols.