crypto.createHash(algorithm[, options])
历史
版本 | 变化 |
---|---|
v12.8.0 | 为 XOF 哈希函数添加了 |
v0.1.92 | 添加于:v0.1.92 |
参数
algorithm
<string>options
<Object> new stream.Transform([options])- 返回: <Hash>
创建并返回一个 Hash
对象,该对象可用于使用给定的 algorithm
生成哈希摘要。可选的 options
参数控制流行为。对于 'shake256'
等 XOF 哈希函数,outputLength
选项可用于指定所需的输出长度(以字节为单位)。
algorithm
取决于平台上OpenSSL 版本支持的可用算法。例如 'sha256'
、 'sha512'
等。在 OpenSSL 的最新版本中,openssl list -digest-algorithms
将显示可用的摘要算法。
示例:生成文件的 sha256 和
import { createReadStream } from 'fs'; import { argv } from 'node:process'; const { createHash } = await import('node:crypto'); const filename = argv[2]; const hash = createHash('sha256'); const input = createReadStream(filename); input.on('readable', () => { // Only one element is going to be produced by the // hash stream. const data = input.read(); if (data) hash.update(data); else { console.log(`${hash.digest('hex')} ${filename}`); } });
const { createReadStream, } = require('node:fs'); const { createHash, } = require('node:crypto'); const { argv } = require('node:process'); const filename = argv[2]; const hash = createHash('sha256'); const input = createReadStream(filename); input.on('readable', () => { // Only one element is going to be produced by the // hash stream. const data = input.read(); if (data) hash.update(data); else { console.log(`${hash.digest('hex')} ${filename}`); } });
相关用法
- Node.js crypto.createHash()用法及代码示例
- Node.js crypto.createHmac()用法及代码示例
- Node.js crypto.createHmac(algorithm, key[, options])用法及代码示例
- Node.js crypto.createCipheriv()用法及代码示例
- Node.js crypto.createVerify()用法及代码示例
- Node.js crypto.createDiffieHellman()用法及代码示例
- Node.js crypto.createECDH()用法及代码示例
- Node.js crypto.createDiffieHellmanGroup()用法及代码示例
- Node.js crypto.createSign()用法及代码示例
- Node.js crypto.createDecipheriv()用法及代码示例
- Node.js crypto.constants用法及代码示例
- Node.js crypto.checkPrime()用法及代码示例
- Node.js crypto.checkPrimeSync()用法及代码示例
- Node.js crypto.randomFill()用法及代码示例
- Node.js crypto.randomFillSync(buffer[, offset][, size])用法及代码示例
- Node.js crypto.randomInt([min, ]max[, callback])用法及代码示例
- Node.js crypto.publicEncrypt()用法及代码示例
- Node.js crypto.publicDecrypt()用法及代码示例
- Node.js crypto.pbkdf2Sync(password, salt, iterations, keylen, digest)用法及代码示例
- Node.js crypto.hkdfSync()用法及代码示例
- Node.js crypto.randomFillSync()用法及代码示例
- Node.js crypto.sign()用法及代码示例
- Node.js crypto.webcrypto用法及代码示例
- Node.js crypto.generateKeyPairSync(type, options)用法及代码示例
- Node.js crypto.scrypt(password, salt, keylen[, options], callback)用法及代码示例
注:本文由纯净天空筛选整理自nodejs.org大神的英文原创作品 crypto.createHash(algorithm[, options])。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。