sha.js provides pure JavaScript implementations of SHA (Secure Hash Algorithm) functions including SHA-0, SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512. It offers a Node.js-compatible API for creating streamable hash objects that can process data incrementally, making it suitable for hashing large amounts of data without loading everything into memory.
npm install sha.jsconst shajs = require('sha.js');
// Or import specific algorithms
const { sha256, sha1, sha512 } = require('sha.js');For ES modules:
import shajs from 'sha.js';
// Or import specific algorithms
import { sha256, sha1, sha512 } from 'sha.js';const shajs = require('sha.js');
// Using factory function
const hash = shajs('sha256').update('hello world').digest('hex');
console.log(hash); // "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"
// Using direct constructor
const sha256Hash = new shajs.sha256().update('hello world').digest('hex');
// Streaming usage
const stream = shajs('sha256');
stream.update('hello ');
stream.update('world');
const result = stream.digest('hex');sha.js is built around several key components:
Creates hash instances for any supported SHA algorithm.
/**
* Creates a SHA hash instance for the specified algorithm
* @param {string} algorithm - Algorithm name (case-insensitive): 'sha', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512'
* @returns {Hash} Hash instance for the specified algorithm
* @throws {Error} If algorithm is not supported
*/
function shajs(algorithm);Usage Example:
// Create different hash types
const sha1Hash = shajs('sha1');
const sha256Hash = shajs('sha256');
const sha512Hash = shajs('SHA512'); // Case insensitiveDirect access to specific SHA algorithm constructors.
/**
* SHA-0 hash constructor (legacy, deprecated)
*/
function Sha();
/**
* SHA-1 hash constructor (legacy, deprecated for security)
*/
function Sha1();
/**
* SHA-224 hash constructor
*/
function Sha224();
/**
* SHA-256 hash constructor
*/
function Sha256();
/**
* SHA-384 hash constructor
*/
function Sha384();
/**
* SHA-512 hash constructor
*/
function Sha512();Usage Example:
const { sha256, sha1 } = require('sha.js');
// Direct instantiation
const hash1 = new sha256();
const hash2 = new sha1();All hash instances provide the same interface for incremental data processing.
/**
* Updates the hash with new data
* @param {string|Buffer} data - Data to add to hash
* @param {string} [encoding='utf8'] - Encoding for string data
* @returns {Hash} this (for method chaining)
*/
update(data, encoding);
/**
* Finalizes the hash computation and returns the result
* @param {string} [encoding] - Output encoding ('hex', 'base64', etc.)
* @returns {Buffer|string} Hash result as Buffer (no encoding) or string (with encoding)
*/
digest(encoding);
/**
* Initializes/resets the hash state to start fresh
* @returns {Hash} this
*/
init();Usage Examples:
const shajs = require('sha.js');
// Method chaining
const result = shajs('sha256')
.update('hello')
.update(' ')
.update('world')
.digest('hex');
// Step by step
const hash = shajs('sha256');
hash.update('data chunk 1');
hash.update('data chunk 2');
const hexResult = hash.digest('hex');
const bufferResult = hash.digest(); // Returns Buffer
// Reset and reuse
hash.init();
hash.update('new data');
const newResult = hash.digest('hex');sha.js includes a command-line tool for hashing files or stdin.
# Hash a file with SHA-256
sha.js sha256 filename.txt
# Hash stdin with SHA-1 (default)
echo "hello world" | sha.js
# Hash stdin with specific algorithm
echo "hello world" | sha.js sha512
# Show help
sha.js --help// Available algorithm names (case-insensitive)
type SupportedAlgorithm = 'sha' | 'sha1' | 'sha224' | 'sha256' | 'sha384' | 'sha512';const shajs = require('sha.js');
try {
const hash = shajs('unsupported-algorithm');
} catch (error) {
console.log(error.message); // "unsupported-algorithm is not supported (we accept pull requests)"
}/**
* Base hash class that all algorithm implementations extend
*/
interface Hash {
/** Update hash with new data */
update(data: string | Buffer, encoding?: string): Hash;
/** Finalize and return hash result */
digest(encoding?: string): Buffer | string;
/** Reset hash state */
init(): Hash;
}
/**
* SHA-0 hash implementation (legacy)
*/
interface Sha extends Hash {}
/**
* SHA-1 hash implementation (legacy)
*/
interface Sha1 extends Hash {}
/**
* SHA-224 hash implementation
*/
interface Sha224 extends Hash {}
/**
* SHA-256 hash implementation
*/
interface Sha256 extends Hash {}
/**
* SHA-384 hash implementation
*/
interface Sha384 extends Hash {}
/**
* SHA-512 hash implementation
*/
interface Sha512 extends Hash {}