hash.copy([options])
options<Object>stream.transform选项- 返回: <Hash>
创建一个新的 Hash 对象,其中包含当前 Hash 对象内部状态的深拷贝。
【Creates a new Hash object that contains a deep copy of the internal state
of the current Hash object.】
可选的 options 参数用于控制流的行为。对于 XOF 哈希函数,例如 'shake256',可以使用 outputLength 选项来指定所需的输出长度(字节)。
【The 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.】
在调用 Hash 对象的 hash.digest() 方法后,如果尝试复制该对象,将会抛出错误。
【An error is thrown when an attempt is made to copy the Hash object after
its hash.digest() method has been called.】
// Calculate a rolling hash.
const {
createHash,
} = await import('node:crypto');
const hash = createHash('sha256');
hash.update('one');
console.log(hash.copy().digest('hex'));
hash.update('two');
console.log(hash.copy().digest('hex'));
hash.update('three');
console.log(hash.copy().digest('hex'));
// Etc.// Calculate a rolling hash.
const {
createHash,
} = require('node:crypto');
const hash = createHash('sha256');
hash.update('one');
console.log(hash.copy().digest('hex'));
hash.update('two');
console.log(hash.copy().digest('hex'));
hash.update('three');
console.log(hash.copy().digest('hex'));
// Etc.