类:Cipher
添加于:v0.1.94
Cipher
类的实例用于加密数据。可以通过以下两种方式之一使用该类:
- 作为可读可写的stream,写入普通未加密数据以在可读端生成加密数据,或
- 使用
cipher.update()
cipher.final()
或 crypto.createCipher()
方法用于创建 crypto.createCipheriv()
Cipher
实例。 Cipher
对象不能直接使用new
关键字创建。
示例:使用 Cipher
对象作为流:
const { scrypt, randomFill, createCipheriv } = await import('node:crypto'); const algorithm = 'aes-192-cbc'; const password = 'Password used to generate key'; // First, we'll generate the key. The key length is dependent on the algorithm. // In this case for aes192, it is 24 bytes (192 bits). scrypt(password, 'salt', 24, (err, key) => { if (err) throw err; // Then, we'll generate a random initialization vector randomFill(new Uint8Array(16), (err, iv) => { if (err) throw err; // Once we have the key and iv, we can create and use the cipher... const cipher = createCipheriv(algorithm, key, iv); let encrypted = ''; cipher.setEncoding('hex'); cipher.on('data', (chunk) => encrypted += chunk); cipher.on('end', () => console.log(encrypted)); cipher.write('some clear text data'); cipher.end(); }); });
const { scrypt, randomFill, createCipheriv } = require('node:crypto'); const algorithm = 'aes-192-cbc'; const password = 'Password used to generate key'; // First, we'll generate the key. The key length is dependent on the algorithm. // In this case for aes192, it is 24 bytes (192 bits). scrypt(password, 'salt', 24, (err, key) => { if (err) throw err; // Then, we'll generate a random initialization vector randomFill(new Uint8Array(16), (err, iv) => { if (err) throw err; // Once we have the key and iv, we can create and use the cipher... const cipher = createCipheriv(algorithm, key, iv); let encrypted = ''; cipher.setEncoding('hex'); cipher.on('data', (chunk) => encrypted += chunk); cipher.on('end', () => console.log(encrypted)); cipher.write('some clear text data'); cipher.end(); }); });
示例:使用 Cipher
和管道流:
import { createReadStream, createWriteStream, } from 'fs'; import { pipeline } from 'stream'; const { scrypt, randomFill, createCipheriv } = await import('node:crypto'); const algorithm = 'aes-192-cbc'; const password = 'Password used to generate key'; // First, we'll generate the key. The key length is dependent on the algorithm. // In this case for aes192, it is 24 bytes (192 bits). scrypt(password, 'salt', 24, (err, key) => { if (err) throw err; // Then, we'll generate a random initialization vector randomFill(new Uint8Array(16), (err, iv) => { if (err) throw err; const cipher = createCipheriv(algorithm, key, iv); const input = createReadStream('test.js'); const output = createWriteStream('test.enc'); pipeline(input, cipher, output, (err) => { if (err) throw err; }); }); });
const { createReadStream, createWriteStream, } = require('node:fs'); const { pipeline } = require('node:stream'); const { scrypt, randomFill, createCipheriv, } = require('node:crypto'); const algorithm = 'aes-192-cbc'; const password = 'Password used to generate key'; // First, we'll generate the key. The key length is dependent on the algorithm. // In this case for aes192, it is 24 bytes (192 bits). scrypt(password, 'salt', 24, (err, key) => { if (err) throw err; // Then, we'll generate a random initialization vector randomFill(new Uint8Array(16), (err, iv) => { if (err) throw err; const cipher = createCipheriv(algorithm, key, iv); const input = createReadStream('test.js'); const output = createWriteStream('test.enc'); pipeline(input, cipher, output, (err) => { if (err) throw err; }); }); });
示例:使用
和 cipher.update()
方法:cipher.final()
const { scrypt, randomFill, createCipheriv } = await import('node:crypto'); const algorithm = 'aes-192-cbc'; const password = 'Password used to generate key'; // First, we'll generate the key. The key length is dependent on the algorithm. // In this case for aes192, it is 24 bytes (192 bits). scrypt(password, 'salt', 24, (err, key) => { if (err) throw err; // Then, we'll generate a random initialization vector randomFill(new Uint8Array(16), (err, iv) => { if (err) throw err; const cipher = createCipheriv(algorithm, key, iv); let encrypted = cipher.update('some clear text data', 'utf8', 'hex'); encrypted += cipher.final('hex'); console.log(encrypted); }); });
const { scrypt, randomFill, createCipheriv, } = require('node:crypto'); const algorithm = 'aes-192-cbc'; const password = 'Password used to generate key'; // First, we'll generate the key. The key length is dependent on the algorithm. // In this case for aes192, it is 24 bytes (192 bits). scrypt(password, 'salt', 24, (err, key) => { if (err) throw err; // Then, we'll generate a random initialization vector randomFill(new Uint8Array(16), (err, iv) => { if (err) throw err; const cipher = createCipheriv(algorithm, key, iv); let encrypted = cipher.update('some clear text data', 'utf8', 'hex'); encrypted += cipher.final('hex'); console.log(encrypted); }); });
相关用法
- Node.js Console用法及代码示例
- Node.js Console.timeLog([label][, ...data])用法及代码示例
- Node.js CSV转Excel用法及代码示例
- Node.js Console.countReset([label])用法及代码示例
- Node.js Console.trace([message][, ...args])用法及代码示例
- Node.js Certificate.exportPublicKey(spkac[, encoding])用法及代码示例
- Node.js Channel.unsubscribe(onMessage)用法及代码示例
- Node.js Console.timeEnd([label])用法及代码示例
- Node.js MySQL CONCAT_WS()用法及代码示例
- Node.js Channel.hasSubscribers用法及代码示例
- Node.js Console.assert(value[, ...message])用法及代码示例
- Node.js Channel.publish(message)用法及代码示例
- Node.js MySQL CHARACTER_LENGTH()用法及代码示例
- Node.js MySQL Count()用法及代码示例
- Node.js Certificate.verifySpkac(spkac[, encoding])用法及代码示例
- Node.js Console.log([data][, ...args])用法及代码示例
- Node.js Console.count([label])用法及代码示例
- Node.js MySQL CHAR_LENGTH()用法及代码示例
- Node.js Certificate.exportChallenge(spkac[, encoding])用法及代码示例
- Node.js Console.table(tabularData[, properties])用法及代码示例
- Node.js Console.error([data][, ...args])用法及代码示例
- Node.js ClientHttp2Session.request(headers[, options])用法及代码示例
- Node.js Channel.subscribe(onMessage)用法及代码示例
- Node.js ServerHttp2Stream http2stream.pushStream(headers[, options], callback)用法及代码示例
- Node.js http2.Http2ServerRequest request.url用法及代码示例
注:本文由纯净天空筛选整理自nodejs.org大神的英文原创作品 Cipher。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。