Hash.js is a JavaScript library providing comprehensive cryptographic hash functions that work seamlessly in both browser and Node.js environments. It implements popular hash algorithms including SHA-1, SHA-224, SHA-256, SHA-384, SHA-512, and RIPEMD-160, along with HMAC functionality for message authentication.
npm install hash.jsconst hash = require('hash.js');Selective imports for smaller bundle size:
// Individual hash functions
const sha1 = require('hash.js/lib/hash/sha/1');
const sha224 = require('hash.js/lib/hash/sha/224');
const sha256 = require('hash.js/lib/hash/sha/256');
const sha384 = require('hash.js/lib/hash/sha/384');
const sha512 = require('hash.js/lib/hash/sha/512');
// RIPEMD and HMAC
const ripemd = require('hash.js/lib/hash/ripemd'); // exports: { ripemd160 }
const hmac = require('hash.js/lib/hash/hmac');
// Utilities and common functionality
const utils = require('hash.js/lib/hash/utils');
const common = require('hash.js/lib/hash/common'); // exports: { BlockHash }const hash = require('hash.js');
// SHA-256 hash
const sha256Digest = hash.sha256().update('hello world').digest('hex');
console.log(sha256Digest); // "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"
// SHA-1 hash
const sha1Digest = hash.sha1().update('hello world').digest('hex');
// RIPEMD-160 hash
const ripemdDigest = hash.ripemd160().update('hello world').digest('hex');
// HMAC authentication
const hmacDigest = hash.hmac(hash.sha256, 'secret-key').update('message').digest('hex');Hash.js is built around several key components:
Complete implementation of the SHA (Secure Hash Algorithm) family including SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512.
// SHA-1 (160-bit output)
function sha1(): SHA1Instance;
// SHA-224 (224-bit output)
function sha224(): SHA224Instance;
// SHA-256 (256-bit output)
function sha256(): SHA256Instance;
// SHA-384 (384-bit output)
function sha384(): SHA384Instance;
// SHA-512 (512-bit output)
function sha512(): SHA512Instance;
interface HashInstance {
update(msg: any, enc?: 'hex'): this;
digest(): number[];
digest(enc: 'hex'): string;
blockSize: number;
outSize: number;
}RIPEMD-160 cryptographic hash function producing 160-bit hash values, often used as an alternative to SHA-1.
function ripemd160(): RIPEMD160Instance;
interface RIPEMD160Instance {
update(msg: any, enc?: 'hex'): this;
digest(): number[];
digest(enc: 'hex'): string;
blockSize: 512;
outSize: 160;
hmacStrength: 192;
padLength: 64;
endian: 'little';
}Hash-based Message Authentication Code (HMAC) construction for message authentication and integrity verification.
function hmac(hashFn: HashConstructor, key: any, enc?: 'hex'): HMACInstance;
interface HMACInstance {
update(msg: any, enc?: 'hex'): this;
digest(): number[];
digest(enc: 'hex'): string;
blockSize: number;
outSize: number;
}
type HashConstructor = typeof SHA1 | typeof SHA256 | typeof SHA512 | typeof RIPEMD160;Data conversion and manipulation utilities for working with different data formats and encodings.
interface Utils {
toArray(msg: any, enc?: 'hex'): number[];
toHex(msg: number[]): string;
inherits(ctor: Function, superCtor: Function): void;
}
const utils: Utils;Core base classes and shared functionality used by all hash implementations.
interface Common {
BlockHash: BlockHashConstructor;
}
interface BlockHashConstructor {
new(): BlockHash;
blockSize: number;
outSize: number;
hmacStrength: number;
padLength: number;
}
interface BlockHash {
update(msg: any, enc?: 'hex'): this;
digest(): number[];
digest(enc: 'hex'): string;
blockSize: number;
outSize: number;
hmacStrength: number;
padLength: number;
endian: 'big' | 'little';
pending: number[] | null;
pendingTotal: number;
_update(msg: number[], start: number, end: number): void;
_digest(enc?: 'hex'): number[] | string;
_pad(): number[];
}
const common: Common;The
commonBlockHashOrganized access to hash functions through dedicated namespaces.
interface ShaNamespace {
sha1: Sha1Constructor;
sha224: Sha224Constructor;
sha256: Sha256Constructor;
sha384: Sha384Constructor;
sha512: Sha512Constructor;
}
interface RipemdNamespace {
ripemd160: Ripemd160Constructor;
}
const sha: ShaNamespace;
const ripemd: RipemdNamespace;These namespaces provide alternative access patterns:
const hash = require('hash.js');
// Direct access
const digest1 = hash.sha256().update('test').digest('hex');
// Namespace access (equivalent)
const digest2 = hash.sha.sha256().update('test').digest('hex');
const digest3 = hash.ripemd.ripemd160().update('test').digest('hex');// Core hash instance interface
interface BlockHash {
update(msg: any, enc?: 'hex'): this;
digest(): number[];
digest(enc: 'hex'): string;
blockSize: number;
outSize: number;
hmacStrength: number;
padLength: number;
endian: 'big' | 'little';
}
// Input types
type MessageInput = string | number[] | Uint8Array;
type Encoding = 'hex';
// Output types
type DigestOutput = number[] | string;
// Constructor types
type Sha1Constructor = () => SHA1Instance;
type Sha224Constructor = () => SHA224Instance;
type Sha256Constructor = () => SHA256Instance;
type Sha384Constructor = () => SHA384Instance;
type Sha512Constructor = () => SHA512Instance;
type Ripemd160Constructor = () => RIPEMD160Instance;
type HmacConstructor = (hashFn: HashConstructor, key: any, enc?: 'hex') => HMACInstance;
type HashConstructor = Sha1Constructor | Sha224Constructor | Sha256Constructor | Sha384Constructor | Sha512Constructor | Ripemd160Constructor;