Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7fd4ef7

Browse filesBrowse files
mayankasthanacodebytere
authored andcommitted
doc: update crypto examples to not use deprecated api
Updated Cipher and Decipher examples to not use deprecated `crypto.createCipher` and `crypto.createDecipher` in examples and instead use `createCipheriv` and `createDecipheriv`. Fixes: #24046 PR-URL: #24107 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com>
1 parent c29c510 commit 7fd4ef7
Copy full SHA for 7fd4ef7

File tree

Expand file treeCollapse file tree

1 file changed

+67
-10
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+67
-10
lines changed
Open diff view settings
Collapse file

‎doc/api/crypto.md‎

Copy file name to clipboardExpand all lines: doc/api/crypto.md
+67-10Lines changed: 67 additions & 10 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,18 @@ Example: Using `Cipher` objects as streams:
185185

186186
```js
187187
const crypto = require('crypto');
188-
const cipher = crypto.createCipher('aes192', 'a password');
188+
189+
const algorithm = 'aes-192-cbc';
190+
const password = 'Password used to generate key';
191+
// Key length is dependent on the algorithm. In this case for aes192, it is
192+
// 24 bytes (192 bits).
193+
// Use async `crypto.scrypt()` instead.
194+
const key = crypto.scryptSync(password, 'salt', 24);
195+
// Use `crypto.randomBytes()` to generate a random iv instead of the static iv
196+
// shown here.
197+
const iv = Buffer.alloc(16, 0); // Initialization vector.
198+
199+
const cipher = crypto.createCipheriv(algorithm, key, iv);
189200

190201
let encrypted = '';
191202
cipher.on('readable', () => {
@@ -195,7 +206,7 @@ cipher.on('readable', () => {
195206
});
196207
cipher.on('end', () => {
197208
console.log(encrypted);
198-
// Prints: ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504
209+
// Prints: e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa
199210
});
200211

201212
cipher.write('some clear text data');
@@ -207,7 +218,16 @@ Example: Using `Cipher` and piped streams:
207218
```js
208219
const crypto = require('crypto');
209220
const fs = require('fs');
210-
const cipher = crypto.createCipher('aes192', 'a password');
221+
222+
const algorithm = 'aes-192-cbc';
223+
const password = 'Password used to generate key';
224+
// Use the async `crypto.scrypt()` instead.
225+
const key = crypto.scryptSync(password, 'salt', 24);
226+
// Use `crypto.randomBytes()` to generate a random iv instead of the static iv
227+
// shown here.
228+
const iv = Buffer.alloc(16, 0); // Initialization vector.
229+
230+
const cipher = crypto.createCipheriv(algorithm, key, iv);
211231

212232
const input = fs.createReadStream('test.js');
213233
const output = fs.createWriteStream('test.enc');
@@ -219,12 +239,21 @@ Example: Using the [`cipher.update()`][] and [`cipher.final()`][] methods:
219239

220240
```js
221241
const crypto = require('crypto');
222-
const cipher = crypto.createCipher('aes192', 'a password');
242+
243+
const algorithm = 'aes-192-cbc';
244+
const password = 'Password used to generate key';
245+
// Use the async `crypto.scrypt()` instead.
246+
const key = crypto.scryptSync(password, 'salt', 24);
247+
// Use `crypto.randomBytes` to generate a random iv instead of the static iv
248+
// shown here.
249+
const iv = Buffer.alloc(16, 0); // Initialization vector.
250+
251+
const cipher = crypto.createCipheriv(algorithm, key, iv);
223252

224253
let encrypted = cipher.update('some clear text data', 'utf8', 'hex');
225254
encrypted += cipher.final('hex');
226255
console.log(encrypted);
227-
// Prints: ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504
256+
// Prints: e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa
228257
```
229258

230259
### cipher.final([outputEncoding])
@@ -340,7 +369,17 @@ Example: Using `Decipher` objects as streams:
340369

341370
```js
342371
const crypto = require('crypto');
343-
const decipher = crypto.createDecipher('aes192', 'a password');
372+
373+
const algorithm = 'aes-192-cbc';
374+
const password = 'Password used to generate key';
375+
// Key length is dependent on the algorithm. In this case for aes192, it is
376+
// 24 bytes (192 bits).
377+
// Use the async `crypto.scrypt()` instead.
378+
const key = crypto.scryptSync(password, 'salt', 24);
379+
// The IV is usually passed along with the ciphertext.
380+
const iv = Buffer.alloc(16, 0); // Initialization vector.
381+
382+
const decipher = crypto.createDecipheriv(algorithm, key, iv);
344383

345384
let decrypted = '';
346385
decipher.on('readable', () => {
@@ -353,8 +392,9 @@ decipher.on('end', () => {
353392
// Prints: some clear text data
354393
});
355394

395+
// Encrypted with same algorithm, key and iv.
356396
const encrypted =
357-
'ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504';
397+
'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa';
358398
decipher.write(encrypted, 'hex');
359399
decipher.end();
360400
```
@@ -364,7 +404,15 @@ Example: Using `Decipher` and piped streams:
364404
```js
365405
const crypto = require('crypto');
366406
const fs = require('fs');
367-
const decipher = crypto.createDecipher('aes192', 'a password');
407+
408+
const algorithm = 'aes-192-cbc';
409+
const password = 'Password used to generate key';
410+
// Use the async `crypto.scrypt()` instead.
411+
const key = crypto.scryptSync(password, 'salt', 24);
412+
// The IV is usually passed along with the ciphertext.
413+
const iv = Buffer.alloc(16, 0); // Initialization vector.
414+
415+
const decipher = crypto.createDecipheriv(algorithm, key, iv);
368416

369417
const input = fs.createReadStream('test.enc');
370418
const output = fs.createWriteStream('test.js');
@@ -376,10 +424,19 @@ Example: Using the [`decipher.update()`][] and [`decipher.final()`][] methods:
376424

377425
```js
378426
const crypto = require('crypto');
379-
const decipher = crypto.createDecipher('aes192', 'a password');
380427

428+
const algorithm = 'aes-192-cbc';
429+
const password = 'Password used to generate key';
430+
// Use the async `crypto.scrypt()` instead.
431+
const key = crypto.scryptSync(password, 'salt', 24);
432+
// The IV is usually passed along with the ciphertext.
433+
const iv = Buffer.alloc(16, 0); // Initialization vector.
434+
435+
const decipher = crypto.createDecipheriv(algorithm, key, iv);
436+
437+
// Encrypted using same algorithm, key and iv.
381438
const encrypted =
382-
'ca981be48e90867604588e75d04feabb63cc007a8f8ad89b10616ed84d815504';
439+
'e5f79c5915c02171eec6b212d5520d44480993d7d622a7c4c2da32f6efda0ffa';
383440
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
384441
decrypted += decipher.final('utf8');
385442
console.log(decrypted);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.