zlib.crc32(data[, value])
-
data
<string> | <Buffer> | <TypedArray> | <DataView> 当data
是字符串时,在用于计算之前将其编码为 UTF-8。¥
data
<string> | <Buffer> | <TypedArray> | <DataView> Whendata
is a string, it will be encoded as UTF-8 before being used for computation. -
value
<integer> 可选的起始值。它必须是 32 位无符号整数。默认值:0
¥
value
<integer> An optional starting value. It must be a 32-bit unsigned integer. Default:0
-
返回:<integer> 包含校验和的 32 位无符号整数。
¥Returns: <integer> A 32-bit unsigned integer containing the checksum.
计算 data
的 32 位 循环冗余校验 校验和。如果指定了 value
,则作为校验和的起始值,否则使用 0 作为起始值。
¥Computes a 32-bit Cyclic Redundancy Check checksum of data
. If
value
is specified, it is used as the starting value of the checksum,
otherwise, 0 is used as the starting value.
CRC 算法旨在计算校验和并检测数据传输中的错误。它不适合加密身份验证。
¥The CRC algorithm is designed to compute checksums and to detect error in data transmission. It's not suitable for cryptographic authentication.
为了与其他 API 保持一致,如果 data
是字符串,则会先进行 UTF-8 编码后再用于计算。如果用户仅使用 Node.js 来计算和匹配校验和,则这与默认使用 UTF-8 编码的其他 API 配合良好。
¥To be consistent with other APIs, if the data
is a string, it will
be encoded with UTF-8 before being used for computation. If users only
use Node.js to compute and match the checksums, this works well with
other APIs that uses the UTF-8 encoding by default.
一些第三方 JavaScript 库根据 str.charCodeAt()
计算字符串的校验和,以便它可以在浏览器中运行。如果用户想要在浏览器中匹配使用此类库计算的校验和,那么最好在 Node.js 中使用相同的库(如果它也在 Node.js 中运行)。如果用户必须使用 zlib.crc32()
来匹配此类第三方库生成的校验和:
¥Some third-party JavaScript libraries compute the checksum on a
string based on str.charCodeAt()
so that it can be run in browsers.
If users want to match the checksum computed with this kind of library
in the browser, it's better to use the same library in Node.js
if it also runs in Node.js. If users have to use zlib.crc32()
to
match the checksum produced by such a third-party library:
-
如果库接受
Uint8Array
作为输入,则在浏览器中使用TextEncoder
将字符串编码为 UTF-8 编码的Uint8Array
,并根据浏览器中的 UTF-8 编码字符串计算校验和。¥If the library accepts
Uint8Array
as input, useTextEncoder
in the browser to encode the string into aUint8Array
with UTF-8 encoding, and compute the checksum based on the UTF-8 encoded string in the browser. -
如果库只接受字符串并根据
str.charCodeAt()
计算数据,则在 Node.js 端,使用Buffer.from(str, 'utf16le')
将字符串转换为缓冲区。¥If the library only takes a string and compute the data based on
str.charCodeAt()
, on the Node.js side, convert the string into a buffer usingBuffer.from(str, 'utf16le')
.
import zlib from 'node:zlib';
import { Buffer } from 'node:buffer';
let crc = zlib.crc32('hello'); // 907060870
crc = zlib.crc32('world', crc); // 4192936109
crc = zlib.crc32(Buffer.from('hello', 'utf16le')); // 1427272415
crc = zlib.crc32(Buffer.from('world', 'utf16le'), crc); // 4150509955
const zlib = require('node:zlib');
const { Buffer } = require('node:buffer');
let crc = zlib.crc32('hello'); // 907060870
crc = zlib.crc32('world', crc); // 4192936109
crc = zlib.crc32(Buffer.from('hello', 'utf16le')); // 1427272415
crc = zlib.crc32(Buffer.from('world', 'utf16le'), crc); // 4150509955