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