crypto-browserify provides a browser-compatible implementation of Node.js's crypto module, enabling cryptographic operations in web browsers through pure JavaScript implementations. It offers essential cryptographic primitives including hash functions, HMAC generation, PBKDF2 key derivation, random number generation, symmetric encryption/decryption, asymmetric cryptography, and key exchange protocols.
npm install crypto-browserifyconst crypto = require('crypto-browserify');
// or specific imports:
const { createHash, createHmac, randomBytes } = require('crypto-browserify');For ES modules:
import * as crypto from 'crypto-browserify';
// or specific imports:
import { createHash, createHmac, randomBytes } from 'crypto-browserify';const crypto = require('crypto-browserify');
// Generate random bytes
const randomData = crypto.randomBytes(16);
// Create a hash
const hash = crypto.createHash('sha256')
.update('hello world')
.digest('hex');
// Create HMAC
const hmac = crypto.createHmac('sha256', 'secret-key')
.update('data to authenticate')
.digest('hex');
// Encrypt data
const cipher = crypto.createCipher('aes192', 'password');
let encrypted = cipher.update('plaintext', 'utf8', 'hex');
encrypted += cipher.final('hex');crypto-browserify is structured as a collection of specialized modules:
Cryptographically secure random number generation for initialization vectors, keys, and nonces.
function randomBytes(size) // size: number, Returns Buffer
function randomFill(buffer, callback) // buffer: Buffer, callback: (err, buf) => void
function randomFillSync(buffer) // buffer: Buffer, Returns Buffer
// Aliases for randomBytes
const rng = randomBytes;
const pseudoRandomBytes = randomBytes;
const prng = randomBytes;Cryptographic hash functions including SHA-1, SHA-2 family, MD5, and RIPEMD-160.
function createHash(algorithm) // algorithm: string, Returns Hash object
function getHashes() // Returns string[] of supported hash algorithm names
// Hash class (alias for createHash)
const Hash = createHash;
// Hash object methods:
hash.update(data, inputEncoding) // data: string|Buffer, inputEncoding?: string, Returns Hash (chainable)
hash.digest(encoding) // encoding?: string, Returns Buffer or stringMessage authentication using hash-based message authentication codes.
function createHmac(algorithm, key) // algorithm: string, key: string|Buffer, Returns HMAC object
// Hmac class (alias for createHmac)
const Hmac = createHmac;
// HMAC object methods:
hmac.update(data, inputEncoding) // data: string|Buffer, inputEncoding?: string, Returns HMAC (chainable)
hmac.digest(encoding) // encoding?: string, Returns Buffer or stringPassword-based key derivation using PBKDF2 for secure key generation from passwords.
function pbkdf2(password, salt, iterations, keylen, digest, callback)
// password: string|Buffer, salt: string|Buffer, iterations: number, keylen: number, digest: string, callback: (err, derivedKey) => void
function pbkdf2Sync(password, salt, iterations, keylen, digest)
// password: string|Buffer, salt: string|Buffer, iterations: number, keylen: number, digest: string, Returns BufferAES and other symmetric encryption algorithms for data confidentiality.
function createCipher(algorithm, password) // algorithm: string, password: string, Returns Cipher object
function createCipheriv(algorithm, key, iv) // algorithm: string, key: Buffer, iv: Buffer, Returns Cipher object
function createDecipher(algorithm, password) // algorithm: string, password: string, Returns Decipher object
function createDecipheriv(algorithm, key, iv) // algorithm: string, key: Buffer, iv: Buffer, Returns Decipher object
function getCiphers() // Returns string[] of supported cipher algorithm names
function listCiphers() // Alias for getCiphers
// Cipher/Decipher classes (direct access)
const Cipher = createCipher;
const Cipheriv = createCipheriv;
const Decipher = createDecipher;
const Decipheriv = createDecipheriv;
// Cipher/Decipher object methods:
cipher.update(data, inputEncoding, outputEncoding) // data: string|Buffer, Returns encrypted data
cipher.final(outputEncoding) // outputEncoding?: string, Returns final encrypted chunkRSA public key cryptography for encryption, decryption, and key management.
function publicEncrypt(key, buffer) // key: string|Buffer|Object, buffer: Buffer, Returns Buffer (encrypted data)
function privateEncrypt(key, buffer) // key: string|Buffer|Object, buffer: Buffer, Returns Buffer (encrypted data)
function publicDecrypt(key, buffer) // key: string|Buffer|Object, buffer: Buffer, Returns Buffer (decrypted data)
function privateDecrypt(key, buffer) // key: string|Buffer|Object, buffer: Buffer, Returns Buffer (decrypted data)RSA and ECDSA digital signatures for authentication and non-repudiation.
function createSign(algorithm) // algorithm: string, Returns Sign object
function createVerify(algorithm) // algorithm: string, Returns Verify object
// Sign/Verify classes (direct access)
const Sign = createSign;
const Verify = createVerify;
// Sign object methods:
sign.update(data) // data: string|Buffer, Returns Sign (chainable)
sign.sign(privateKey, outputFormat) // privateKey: string|Buffer, outputFormat?: string, Returns signature as Buffer or string
// Verify object methods:
verify.update(data) // data: string|Buffer, Returns Verify (chainable)
verify.verify(publicKey, signature, signatureFormat) // publicKey: string|Buffer, signature: string|Buffer, signatureFormat?: string, Returns booleanDiffie-Hellman and Elliptic Curve Diffie-Hellman for secure key establishment.
function createDiffieHellman(prime, primeEncoding) // prime: number|string|Buffer, primeEncoding?: string, Returns DiffieHellman object
function createDiffieHellmanGroup(name) // name: string, Returns DiffieHellmanGroup object
function getDiffieHellman(groupName) // groupName: string, Returns DiffieHellmanGroup object
function createECDH(curveName) // curveName: string, Returns ECDH object
// DiffieHellman classes (direct access)
const DiffieHellman = createDiffieHellman;
const DiffieHellmanGroup = createDiffieHellmanGroup;const constants = {
// Diffie-Hellman constants
DH_CHECK_P_NOT_SAFE_PRIME: 2,
DH_CHECK_P_NOT_PRIME: 1,
DH_UNABLE_TO_CHECK_GENERATOR: 4,
DH_NOT_SUITABLE_GENERATOR: 8,
// TLS constants
NPN_ENABLED: 1,
ALPN_ENABLED: 1,
// RSA padding constants
RSA_PKCS1_PADDING: 1,
RSA_SSLV23_PADDING: 2,
RSA_NO_PADDING: 3,
RSA_PKCS1_OAEP_PADDING: 4,
RSA_X931_PADDING: 5,
RSA_PKCS1_PSS_PADDING: 6,
// Elliptic curve point conversion constants
POINT_CONVERSION_COMPRESSED: 2,
POINT_CONVERSION_UNCOMPRESSED: 4,
POINT_CONVERSION_HYBRID: 6
};