crypto.createHash(algorithm[, options])
algorithm<string>options<Object>stream.transform选项customization<string> | <ArrayBuffer> | <Buffer> | <TypedArray> | <DataView> 对于 cSHAKE 哈希函数,指定自定义字节串。默认: 空字节串。functionName<string> | <ArrayBuffer> | <Buffer> | <TypedArray> | <DataView> 对于 cSHAKE 哈希函数,指定 NIST 函数名的字节字符串。**默认值:**一个空的字节字符串。outputLength<number> 对于 XOF 哈希函数,指定所需的输出长度(以字节为单位)。
- 返回:<Hash>
创建并返回一个 Hash 对象,可用于使用给定的 algorithm 生成哈希摘要。可选的 options 参数用于控制流行为。对于像 'shake256' 这样的 XOF 哈希函数,outputLength 选项可用于指定所需的输出字节长度。
🌐 Creates and returns a Hash object that can be used to generate hash digests
using the given algorithm. Optional options argument controls stream
behavior. For XOF hash functions such as 'shake256', the outputLength option
can be used to specify the desired output length in bytes.
functionName 和 customization 选项只适用于 cSHAKE-128 和 cSHAKE-256。它们只有在 Node.js 使用 OpenSSL 4.0 或更高版本构建,并且所选提供程序支持相应的摘要参数时才受支持。字符串以 UTF-8 编码,字符串和字节值都不能包含 NUL 字节。两个选项的默认值都是空字节串。对于 OpenSSL 内置提供程序,functionName 区分大小写,且必须是 ''、'TupleHash'、'ParallelHash' 或 'KMAC'。其他提供程序可能会有不同的限制。如果两个选项都为空,cSHAKE 会生成与相同输出长度的对应 SHAKE 函数相同的输出。cshake-128 和 cshake-256 的默认输出长度分别是 32 和 64 字节。
🌐 The functionName and customization options apply only to cSHAKE-128 and
cSHAKE-256. They are supported only when Node.js is built with OpenSSL 4.0 or
later and the selected provider supports the corresponding digest parameters.
Strings are encoded as UTF-8, and neither strings nor byte values may contain
NUL bytes. Both options default to an empty byte string. For OpenSSL's built-in
providers, functionName is case-sensitive and must be '', 'TupleHash',
'ParallelHash', or 'KMAC'. Other providers can impose different
restrictions. With both options empty, cSHAKE produces the same output as the
corresponding SHAKE function for the same output length. cshake-128 and
cshake-256 default to output lengths of 32 and 64 bytes, respectively.
当数据量很小(< 5MB)且容易获取时,crypto.hash() 通常会更快。
🌐 When the data is small (< 5MB) and readily available, crypto.hash() is usually faster.
可用的算法取决于平台上的 OpenSSL 版本和配置。例如有 'sha256' 和 'sha512'。使用 crypto.getHashes() 可以获取 Node.js 进程可用的哈希算法列表。
🌐 The available algorithms depend on the version and configuration of OpenSSL on
the platform. Examples are 'sha256' and 'sha512'. Use
crypto.getHashes() to obtain the list of hash algorithms available to the
Node.js process.
示例:生成文件的 sha256 校验和
🌐 Example: generating the sha256 sum of a file
import {
createReadStream,
} from 'node:fs';
import { argv } from 'node:process';
const {
createHash,
} = await import('node:crypto');
const filename = argv[2];
const hash = createHash('sha256');
const input = createReadStream(filename);
input.on('readable', () => {
// Only one element is going to be produced by the
// hash stream.
const data = input.read();
if (data)
hash.update(data);
else {
console.log(`${hash.digest('hex')} ${filename}`);
}
});const {
createReadStream,
} = require('node:fs');
const {
createHash,
} = require('node:crypto');
const { argv } = require('node:process');
const filename = argv[2];
const hash = createHash('sha256');
const input = createReadStream(filename);
input.on('readable', () => {
// Only one element is going to be produced by the
// hash stream.
const data = input.read();
if (data)
hash.update(data);
else {
console.log(`${hash.digest('hex')} ${filename}`);
}
});