類: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: // 6a2da20943931e9834fc12cfe5bb47bbd9ae43489a30726962b576f4e3993e50const { 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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
