Basic Usage

This section provides comprehensive examples of how to use cryptographer.js for common cryptographic operations.

Hash Functions

Simple Hashing

import crypto from 'cryptographer.js';

// SHA-256 hash
const hash = crypto.sha.sha256('Hello World');
console.log('SHA-256:', hash);
// Output: a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e

// SHA-512 hash
const hash512 = crypto.sha.sha512('Hello World');
console.log('SHA-512:', hash512);

// BLAKE3 hash (faster)
const blake3Hash = crypto.sha.blake3('Hello World');
console.log('BLAKE3:', blake3Hash);

// BLAKE3 options
// Keyed hashing (32-byte key)
const b3Keyed = crypto.sha.blake3('Hello World', { keyed: Buffer.alloc(32, 1) });
// Derive-key mode
const b3Derive = crypto.sha.blake3('Hello World', { deriveKey: 'com.example.app' });
// Extendable output (XOF) length
const b3Xof = crypto.sha.blake3('Hello World', { hashLength: 64, outputFormat: 'base64' });

Different Output Formats

File Hashing

HMAC (Hash-based Message Authentication Code)

Basic HMAC

API Authentication Example

Symmetric Encryption

Basic Encryption/Decryption

Different AES Modes

ChaCha20 / ChaCha20-Poly1305

DES / 3DES (Legacy)

Public-Key and Key Exchange

File Encryption

Digital Signatures (DSA)

Key Derivation Functions

Argon2 Password Hashing

bcrypt Password Hashing

PBKDF2 Key Derivation

Complete Example: Secure File Storage

Error Handling

Performance Testing

These examples demonstrate the basic usage of all major features in cryptographer.js. Each example includes error handling and follows security best practices.

Last updated