SHA.js Hashing Library

2.4.12 · maintenance · verified Sun Apr 19

sha.js is a JavaScript library providing various Secure Hash Algorithm (SHA) implementations in pure JavaScript, primarily intended for Node.js environments but also usable in browsers via tools like Browserify. It offers implementations for SHA-0 (legacy), SHA-1 (legacy), SHA-224, SHA-256, SHA-384, and SHA-512. The package is currently at version 2.4.12, with its last publish being 9 months ago as of July 2025. While it presents a stream-like interface with `update()` and `digest()`, it's important to note it does not implement a true Node.js `stream.Writable` interface, though it allows incremental processing for large inputs without consuming excessive RAM. Its main differentiator is being a pure JavaScript implementation, making it suitable for environments where native crypto modules are unavailable or undesirable. Given its version history and the nature of cryptographic libraries, it likely follows a stable maintenance release cadence, with updates primarily for security patches or critical bug fixes rather than frequent feature additions.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates both the factory function and direct constructor methods for SHA hashing, including incremental updates for large data.

const shajs = require('sha.js');

// Using the factory function style
console.log('SHA-256 (factory function):');
const hashFactory = shajs('sha256');
hashFactory.update('Hello, World!');
console.log(hashFactory.digest('hex'));

// Using the constructor style
console.log('\nSHA-512 (constructor):');
const hashConstructor = new shajs.sha512();
hashConstructor.update('Hello, World!');
console.log(hashConstructor.digest('hex'));

// Example with a stream-like interface (though not a true Node.js stream)
console.log('\nSHA-256 (stream-like update/read):');
const sha256stream = shajs('sha256');
sha256stream.write('Part 1'); // Use write for incremental updates
sha256stream.end(' of Part 2'); // Final chunk via end
console.log(sha256stream.read().toString('hex')); // Get the final hash as a Buffer and convert

view raw JSON →