What is fast-sha256?
The fast-sha256 npm package is a JavaScript implementation of the SHA-256 hash function. It is designed to be fast and efficient, making it suitable for use in environments where performance is critical.
What are fast-sha256's main functionalities?
Hashing a string
This feature allows you to hash a string using the SHA-256 algorithm. The code sample demonstrates how to encode a string into a Uint8Array, hash it, and then convert the hash value to a hexadecimal string.
const { hash } = require('fast-sha256');
const encoder = new TextEncoder();
const data = encoder.encode('Hello, world!');
const hashValue = hash(data);
console.log(Buffer.from(hashValue).toString('hex'));
Hashing a file
This feature allows you to hash the contents of a file using the SHA-256 algorithm. The code sample demonstrates how to read a file into a buffer, hash it, and then convert the hash value to a hexadecimal string.
const { hash } = require('fast-sha256');
const fs = require('fs');
const data = fs.readFileSync('path/to/file');
const hashValue = hash(data);
console.log(Buffer.from(hashValue).toString('hex'));
Incremental hashing
This feature allows you to perform incremental hashing, which is useful for hashing data that is received in chunks. The code sample demonstrates how to create a Hash instance, update it with multiple chunks of data, and then obtain the final hash value.
const { Hash } = require('fast-sha256');
const hash = new Hash();
hash.update(new TextEncoder().encode('Hello, '));
hash.update(new TextEncoder().encode('world!'));
const hashValue = hash.digest();
console.log(Buffer.from(hashValue).toString('hex'));
Other packages similar to fast-sha256
crypto
The 'crypto' module is a built-in Node.js module that provides cryptographic functionality, including the SHA-256 hash function. It is highly optimized and widely used. Compared to fast-sha256, the 'crypto' module is more versatile and includes a broader range of cryptographic functions.
js-sha256
The 'js-sha256' package is a JavaScript implementation of the SHA-256 hash function. It is designed to be fast and efficient, similar to fast-sha256. However, 'js-sha256' is more focused on providing a simple API for hashing strings and buffers, whereas fast-sha256 also supports incremental hashing.
sha.js
The 'sha.js' package is a JavaScript implementation of the SHA family of hash functions, including SHA-256. It is designed to be fast and compatible with both Node.js and browser environments. Compared to fast-sha256, 'sha.js' offers a wider range of hash functions but may not be as optimized for performance.
fast-sha256-js
SHA-256 implementation for JavaScript/TypeScript with typed arrays
that works in modern browsers and Node.js.
Implements the hash function, HMAC, and PBKDF2.
Public domain. No warranty.
Installation
You can install fast-sha256-js via NPM:
$ npm install fast-sha256
or download source code.
Usage
Functions accept and return Uint8Array
s.
To convert strings, use external library (for example,
nacl.util).
sha256(message)
Returns a SHA-256 hash of the message.
sha256.hmac(key, message)
Returns an HMAC-SHA-256 of the message for the key.
sha256.pbkdf2(password, salt, rounds, dkLen)
Returns a key of length dkLen derived using PBKDF2-HMAC-SHA256
from the given password, salt, and the number of rounds.
sha256.hkdf(key, salt, info?, length?)
Returns a key of the given length derived using HKDF as
described in RFC 5869.
There are also classes Hash
and HMAC
:
new sha256.Hash()
Constructor for hash instance. Should be used with new
.
Available methods: update()
, digest()
, reset()
, etc.
new sha256.HMAC(key)
Constructor for HMAC instance. Should be used with new
.
Available methods: update()
, digest()
, reset()
, etc.
See comments in src/sha256.ts
for details.
Usage with TypeScript
import sha256, { Hash, HMAC } from "fast-sha256";
sha256(data)
const h = new HMAC(key);
const mac = h.update(data).digest();
import * as sha256 from "fast-sha256";
sha256.pbkdf2(password, salt, iterations, dkLen);
sha256.hash(data)
const hasher = new sha256.Hash();
hasher.update(data1);
hasher.update(data2);
const result = hasher.digest();
Testing and building
Install development dependencies:
$ npm install
Build JavaScript, minified version, and typings:
$ npm run build
Run tests:
$ npm test
Run tests on a different source file:
$ SHA256_SRC=sha256.min.js npm test
Run benchmark:
$ npm run bench
(or in a browser, open tests/bench.html
).
Lint:
$ npm run lint
Notes
While this implementation is pretty fast compared to previous generation
implementations, if you need an even faster one, check out
asmCrypto.