类:Hash
添加于:v0.1.92
Hash
类是用于创建数据哈希摘要的实用程序。它可以通过以下两种方式之一使用:
- 作为可读可写的stream,其中写入数据以在可读端生成计算的哈希摘要,或
- 使用
hash.update()
hash.digest()
方法用于创建crypto.createHash()
Hash
实例。 Hash
对象不能直接使用new
关键字创建。
示例:使用 Hash
对象作为流:
const { createHash } = await import('node:crypto'); const hash = createHash('sha256'); hash.on('readable', () => { // Only one element is going to be produced by the // hash stream. const data = hash.read(); if (data) { console.log(data.toString('hex')); // Prints: // 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50 } }); hash.write('some data to hash'); hash.end();
const { createHash, } = require('node:crypto'); const hash = createHash('sha256'); hash.on('readable', () => { // Only one element is going to be produced by the // hash stream. const data = hash.read(); if (data) { console.log(data.toString('hex')); // Prints: // 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50 } }); hash.write('some data to hash'); hash.end();
示例:使用 Hash
和管道流:
import { createReadStream } from 'node:fs'; import { stdout } from 'node:process'; const { createHash } = await import('node:crypto'); const hash = createHash('sha256'); const input = createReadStream('test.js'); input.pipe(hash).setEncoding('hex').pipe(stdout);
const { createReadStream } = require('node:fs'); const { createHash } = require('node:crypto'); const { stdout } = require('node:process'); const hash = createHash('sha256'); const input = createReadStream('test.js'); input.pipe(hash).setEncoding('hex').pipe(stdout);
示例:使用
和 hash.update()
方法:hash.digest()
const { createHash } = await import('node:crypto'); const hash = createHash('sha256'); hash.update('some data to hash'); console.log(hash.digest('hex')); // Prints: // 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50
const { createHash, } = require('node:crypto'); const hash = createHash('sha256'); hash.update('some data to hash'); console.log(hash.digest('hex')); // Prints: // 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50
相关用法
- Node.js Hash.copy([options])用法及代码示例
- Node.js Http2ServerResponse.finished用法及代码示例
- Node.js Http2Stream close用法及代码示例
- Node.js Http2Session stream用法及代码示例
- Node.js Http2ServerResponse.getHeaderNames()用法及代码示例
- Node.js Http2Session.ping([payload, ]callback)用法及代码示例
- Node.js Http2ServerResponse.statusMessage用法及代码示例
- Node.js Http2ServerResponse.writableEnded用法及代码示例
- Node.js Http2Session timeout用法及代码示例
- Node.js Http2ServerRequest.stream用法及代码示例
- Node.js Http2ServerResponse.setHeader()用法及代码示例
- Node.js Http2ServerRequest.method用法及代码示例
- Node.js Http2ServerResponse.getHeader()用法及代码示例
- Node.js Http2Stream Headers用法及代码示例
- Node.js Http2ServerResponse.addTrailers()用法及代码示例
- Node.js Http2ServerRequest.url用法及代码示例
- Node.js Hmac用法及代码示例
- Node.js Http2ServerResponse.setTimeout()用法及代码示例
- Node.js Http2ServerRequest.complete用法及代码示例
- Node.js Http2ServerResponse.stream用法及代码示例
- Node.js Http2Stream.setTimeout(msecs, callback)用法及代码示例
- Node.js Http2ServerRequest.destroy()用法及代码示例
- Node.js Http2ServerResponse.removeHeader()用法及代码示例
- Node.js Http2Stream Response用法及代码示例
- Node.js Http2ServerResponse.hasHeader()用法及代码示例
注:本文由纯净天空筛选整理自nodejs.org大神的英文原创作品 Hash。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。