Curve25519 Signatures and Key Agreement

0.0.4 · maintenance · verified Sun Apr 19

curve25519-js provides a JavaScript implementation of Curve25519, facilitating both digital signatures and X25519 Diffie-Hellman key agreement. The current stable version is 0.0.4. While its release cadence appears infrequent, with a significant rewrite in 2019, it serves as a functional library for cryptographic operations. A key differentiator is its ability to use a single X25519 key for both signing and key agreement, a feature that distinguishes it from standard Ed25519 implementations which typically use separate key types or require explicit conversion. This is achieved by embedding and extracting a sign bit into the signature during the process. The library is derived from TweetNaCl.js and is suitable for environments where direct Curve25519 operations are needed.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to compute a shared secret key using `sharedKey` with pre-defined private and public keys, and also shows how to generate a new key pair using `generateKeyPair` with a cryptographically secure random seed.

import { sharedKey } from 'curve25519-js';
import { Buffer } from 'buffer'; // Explicit import for browser compatibility

const ALICE_PRIV = '77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a';
const BOB_PUB = 'de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f';

// Ensure Buffer is available for hex conversion
// In a browser environment, you might need a polyfill or alternative conversion
const alicePriv = Uint8Array.from(Buffer.from(ALICE_PRIV, 'hex'));
const bobPub = Uint8Array.from(Buffer.from(BOB_PUB, 'hex'));

// Perform the Diffie-Hellman key exchange to get the shared secret
const secret = sharedKey(alicePriv, bobPub);

console.log('Secret:', Buffer.from(secret).toString('hex'));

// Example of key generation (requires a CSPRNG seed)
import { generateKeyPair } from 'curve25519-js';
import crypto from 'crypto'; // For Node.js CSPRNG

const seed = crypto.randomBytes(32); // Generate a 32-byte cryptographically secure random seed
const keyPair = generateKeyPair(seed);
console.log('Generated Private Key:', Buffer.from(keyPair.private).toString('hex'));
console.log('Generated Public Key:', Buffer.from(keyPair.public).toString('hex'));

view raw JSON →