What is crypto-hash?
The crypto-hash npm package is a simple and modern way to create cryptographic hashes in Node.js and the browser. It supports various hashing algorithms like SHA-256, SHA-384, and SHA-512, and provides a promise-based API for asynchronous operations.
What are crypto-hash'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 hash the string 'Hello, world!' and log the resulting hash.
const cryptoHash = require('crypto-hash');
(async () => {
const hash = await cryptoHash.sha256('Hello, world!');
console.log(hash);
})();
Hashing a file
This feature allows you to hash the contents of a file. The code sample demonstrates how to read a file into a buffer and then hash its contents using the SHA-256 algorithm.
const cryptoHash = require('crypto-hash');
const fs = require('fs');
(async () => {
const fileBuffer = fs.readFileSync('path/to/file.txt');
const hash = await cryptoHash.sha256(fileBuffer);
console.log(hash);
})();
Using different algorithms
This feature allows you to use different hashing algorithms. The code sample demonstrates how to hash the same string using both the SHA-384 and SHA-512 algorithms.
const cryptoHash = require('crypto-hash');
(async () => {
const sha384Hash = await cryptoHash.sha384('Hello, world!');
const sha512Hash = await cryptoHash.sha512('Hello, world!');
console.log('SHA-384:', sha384Hash);
console.log('SHA-512:', sha512Hash);
})();
Other packages similar to crypto-hash
crypto
The built-in 'crypto' module in Node.js provides cryptographic functionality that includes a set of wrappers for OpenSSL's hash, HMAC, cipher, decipher, sign, and verify functions. It is more comprehensive but also more complex to use compared to crypto-hash.
hash.js
The hash.js package is a JavaScript library that provides a simple way to create cryptographic hashes. It supports a variety of algorithms and is designed to be used both in Node.js and the browser. It is similar to crypto-hash but offers a more extensive set of algorithms.
bcrypt
The bcrypt package is a library to help you hash passwords. While it is specifically designed for password hashing and not general-purpose hashing, it provides a high level of security and is widely used in authentication systems.
crypto-hash
Tiny hashing module that uses the native crypto API in Node.js and the browser
Useful when you want the same hashing API in all environments. My cat calls it isomorphic.
In Node.js it uses node:crypto
, while in the browser it uses window.crypto
.
The browser version is only ~300 bytes minified & gzipped.
When used in the browser, it must be in a secure context (HTTPS).
This package is for modern browsers. Internet Explorer is not supported.
Install
npm install crypto-hash
Usage
import {sha256} from 'crypto-hash';
console.log(await sha256('🦄'));
API
sha1(input, options?)
sha256(input, options?)
sha384(input, options?)
sha512(input, options?)
Returns a Promise<string>
with a Hex-encoded hash.
In Node.js, the operation is executed using worker_threads
. A thread is lazily spawned on the first operation and lives until the end of the program execution. It's unref
ed, so it won't keep the process alive.
SHA-1 is insecure and should not be used for anything sensitive.
input
Type: string
ArrayBuffer
ArrayBufferView
options
Type: object
outputFormat
Type: string
Values: 'hex' | 'buffer'
Default: 'hex'
Setting this to buffer
makes it return an ArrayBuffer
instead of a string
.
Related
- hasha - Hashing in Node.js made simple