crypto.hash(algorithm, data[, options])
algorithm<string> | <undefined>data<string> | <Buffer> | <TypedArray> | <DataView> 当data是字符串时,它将在被哈希之前以 UTF-8 编码。如果希望对字符串输入使用不同的编码,用户可以使用TextEncoder或Buffer.from()将字符串编码为TypedArray,然后将编码后的TypedArray传入此 API。options<Object> | <string>customization<string> | <ArrayBuffer> | <Buffer> | <TypedArray> | <DataView> 对于 cSHAKE 哈希函数,指定自定义字节串。默认: 空字节串。functionName<string> | <ArrayBuffer> | <Buffer> | <TypedArray> | <DataView> 对于 cSHAKE 哈希函数,指定 NIST 函数名的字节字符串。**默认值:**一个空的字节字符串。outputEncoding<string> 编码 用于编码返回的摘要。默认值:'hex'。outputLength<number> 对于像 'shake256' 这样的 XOF 哈希函数,可以使用 outputLength 选项来指定所需的输出长度(以字节为单位)。
- 返回:<string> | <Buffer>
一个用于创建数据一次性哈希摘要的工具。当哈希较小量(<= 5MB)且数据随手可得时,它可能比基于对象的 crypto.createHash() 更快。如果数据可能较大或是流式的,仍然建议使用 crypto.createHash()。
🌐 A utility for creating one-shot hash digests of data. It can be faster than
the object-based crypto.createHash() when hashing a smaller amount of data
(<= 5MB) that's readily available. If the data can be big or if it is streamed,
it's still recommended to use crypto.createHash() instead.
可用的算法取决于平台上的 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.
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.
如果 options 是一个字符串,那么它指定了 outputEncoding。
🌐 If options is a string, then it specifies the outputEncoding.
示例:
🌐 Example:
const crypto = require('node:crypto');
const { Buffer } = require('node:buffer');
// Hashing a string and return the result as a hex-encoded string.
const string = 'Node.js';
// 10b3493287f831e81a438811a1ffba01f8cec4b7
console.log(crypto.hash('sha1', string));
// Encode a base64-encoded string into a Buffer, hash it and return
// the result as a buffer.
const base64 = 'Tm9kZS5qcw==';
// <Buffer 10 b3 49 32 87 f8 31 e8 1a 43 88 11 a1 ff ba 01 f8 ce c4 b7>
console.log(crypto.hash('sha1', Buffer.from(base64, 'base64'), 'buffer'));import crypto from 'node:crypto';
import { Buffer } from 'node:buffer';
// Hashing a string and return the result as a hex-encoded string.
const string = 'Node.js';
// 10b3493287f831e81a438811a1ffba01f8cec4b7
console.log(crypto.hash('sha1', string));
// Encode a base64-encoded string into a Buffer, hash it and return
// the result as a buffer.
const base64 = 'Tm9kZS5qcw==';
// <Buffer 10 b3 49 32 87 f8 31 e8 1a 43 88 11 a1 ff ba 01 f8 ce c4 b7>
console.log(crypto.hash('sha1', Buffer.from(base64, 'base64'), 'buffer'));