crypto.createHash()
Creates a Hash object to compute a digest of data.
Syntax
crypto.createHash(algorithm[, options]) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
algorithm | string | Yes | The hash algorithm, e.g. `'sha256'`, `'sha512'`, `'md5'`. |
options | object | No | Stream options such as `outputLength` for XOF algorithms. |
Returns
Hash — A Hash stream you update and digest.
Examples
import { createHash } from 'node:crypto';
const digest = createHash('sha256').update('hello').digest('hex');
console.log(digest);
Output
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
import { createHash } from 'node:crypto';
import { createReadStream } from 'node:fs';
const hash = createHash('sha256');
createReadStream('big.iso').pipe(hash).on('finish', () => {
console.log(hash.digest('hex'));
});
Output
9f86d0818884...
Notes
Use SHA-256+ for integrity/checksums. NEVER hash passwords with a
plain hash; use `scrypt`, bcrypt, or argon2. A Hash object is single
use - call `digest()` once, then it cannot be updated again.