A cryptographic library providing secure key management, encryption, and signature operations for Node.js and browser applications.
npm install @chelonia/crypto
- Key generation and management for various cryptographic algorithms
- Password-based key derivation
- Message signing and verification
- Symmetric and asymmetric encryption
- Support for ed25519, curve25519, and XSalsa20-Poly1305
edwards25519sha512batch
- Ed25519 for signaturescurve25519xsalsa20poly1305
- Curve25519 for asymmetric encryptionxsalsa20poly1305
- XSalsa20-Poly1305 for symmetric encryption
import { keygen, EDWARDS25519SHA512BATCH, CURVE25519XSALSA20POLY1305, XSALSA20POLY1305 } from '@chelonia/crypto';
// Generate an Ed25519 key pair for signing
const signingKey = keygen(EDWARDS25519SHA512BATCH);
// Generate a Curve25519 key pair for asymmetric encryption
const encryptionKey = keygen(CURVE25519XSALSA20POLY1305);
// Generate a symmetric encryption key
const symmetricKey = keygen(XSALSA20POLY1305);
import { serializeKey, deserializeKey } from '@chelonia/crypto';
// Serialize a key (with secret key)
const serializedKey = serializeKey(key, true);
// Serialize a key (public key only)
const serializedPublicKey = serializeKey(key, false);
// Deserialize a key
const deserializedKey = deserializeKey(serializedKey);
import { deriveKeyFromPassword, generateSalt, EDWARDS25519SHA512BATCH } from '@chelonia/crypto';
// Generate a random salt
const salt = generateSalt();
// Derive a key from a password and salt
const key = await deriveKeyFromPassword(EDWARDS25519SHA512BATCH, 'password123', salt);
import { sign, verifySignature, EDWARDS25519SHA512BATCH, keygen } from '@chelonia/crypto';
const key = keygen(EDWARDS25519SHA512BATCH);
const data = 'message to sign';
// Sign a message
const signature = sign(key, data);
// Verify a signature
try {
verifySignature(key, data, signature);
console.log('Signature is valid');
} catch (error) {
console.error('Signature verification failed:', error);
}
import { encrypt, decrypt, keygen, CURVE25519XSALSA20POLY1305, XSALSA20POLY1305 } from '@chelonia/crypto';
// Asymmetric encryption
const asymmetricKey = keygen(CURVE25519XSALSA20POLY1305);
const encryptedData = encrypt(asymmetricKey, 'secret message');
const decryptedData = decrypt(asymmetricKey, encryptedData);
// Symmetric encryption
const symmetricKey = keygen(XSALSA20POLY1305);
const encryptedWithSymmetric = encrypt(symmetricKey, 'secret message');
const decryptedWithSymmetric = decrypt(symmetricKey, encryptedWithSymmetric);
// With additional data (AD)
const encryptedWithAD = encrypt(symmetricKey, 'secret message', 'additional data');
const decryptedWithAD = decrypt(symmetricKey, encryptedWithAD, 'additional data');
keygen(type: string): Key
- Generate a key pair of the specified typeserializeKey(key: Key, saveSecretKey: boolean): string
- Serialize a key to JSONdeserializeKey(data: string): Key
- Deserialize a key from JSONkeygenOfSameType(key: Key | string): Key
- Generate a new key of the same typekeyId(key: Key | string): string
- Generate a unique ID for a keygenerateSalt(): string
- Generate a random saltderiveKeyFromPassword(type: string, password: string, salt: string): Promise<Key>
- Derive a key from a password
sign(key: Key | string, data: string): string
- Sign data with a keyverifySignature(key: Key | string, data: string, signature: string): void
- Verify a signatureencrypt(key: Key | string, data: string, ad?: string): string
- Encrypt data with a keydecrypt(key: Key | string, data: string, ad?: string): string
- Decrypt data with a key
AGPL 3.0.