@ -1,6 +1,5 @@
'use strict' ;
var common = require ( '../common' ) ;
var assert = require ( 'assert' ) ;
const common = require ( '../common' ) ;
if ( ! common . hasCrypto ) {
common . skip ( 'missing crypto' ) ;
@ -10,21 +9,22 @@ if (common.hasFipsCrypto) {
common . skip ( 'not supported in FIPS mode' ) ;
return ;
}
var crypto = require ( 'crypto' ) ;
const crypto = require ( 'crypto' ) ;
const assert = require ( 'assert' ) ;
function testCipher1 ( key ) {
// Test encryption and decryption
var plaintext = 'Keep this a secret? No! Tell everyone about node.js!' ;
var cipher = crypto . createCipher ( 'aes192' , key ) ;
const plaintext = 'Keep this a secret? No! Tell everyone about node.js!' ;
const cipher = crypto . createCipher ( 'aes192' , key ) ;
// encrypt plaintext which is in utf8 format
// to a ciphertext which will be in hex
var ciph = cipher . update ( plaintext , 'utf8' , 'hex' ) ;
let ciph = cipher . update ( plaintext , 'utf8' , 'hex' ) ;
// Only use binary or hex, not base64.
ciph += cipher . final ( 'hex' ) ;
var decipher = crypto . createDecipher ( 'aes192' , key ) ;
var txt = decipher . update ( ciph , 'hex' , 'utf8' ) ;
const decipher = crypto . createDecipher ( 'aes192' , key ) ;
let txt = decipher . update ( ciph , 'hex' , 'utf8' ) ;
txt += decipher . final ( 'utf8' ) ;
assert . strictEqual ( txt , plaintext , 'encryption and decryption' ) ;
@ -33,11 +33,11 @@ function testCipher1(key) {
// NB: In real life, it's not guaranteed that you can get all of it
// in a single read() like this. But in this case, we know it's
// quite small, so there's no harm.
var cStream = crypto . createCipher ( 'aes192' , key ) ;
const cStream = crypto . createCipher ( 'aes192' , key ) ;
cStream . end ( plaintext ) ;
ciph = cStream . read ( ) ;
var dStream = crypto . createDecipher ( 'aes192' , key ) ;
const dStream = crypto . createDecipher ( 'aes192' , key ) ;
dStream . end ( ciph ) ;
txt = dStream . read ( ) . toString ( 'utf8' ) ;
@ -48,19 +48,19 @@ function testCipher1(key) {
function testCipher2 ( key ) {
// encryption and decryption with Base64
// reported in https://github.com/joyent/node/issues/738
var plaintext =
const plaintext =
'32|RmVZZkFUVmpRRkp0TmJaUm56ZU9qcnJkaXNNWVNpTTU*|iXmckfRWZBGWWELw' +
'eCBsThSsfUHLeRe0KCsK8ooHgxie0zOINpXxfZi/oNG7uq9JWFVCk70gfzQH8ZUJ' +
'jAfaFg**' ;
var cipher = crypto . createCipher ( 'aes256' , key ) ;
const cipher = crypto . createCipher ( 'aes256' , key ) ;
// encrypt plaintext which is in utf8 format
// to a ciphertext which will be in Base64
var ciph = cipher . update ( plaintext , 'utf8' , 'base64' ) ;
let ciph = cipher . update ( plaintext , 'utf8' , 'base64' ) ;
ciph += cipher . final ( 'base64' ) ;
var decipher = crypto . createDecipher ( 'aes256' , key ) ;
var txt = decipher . update ( ciph , 'base64' , 'utf8' ) ;
const decipher = crypto . createDecipher ( 'aes256' , key ) ;
let txt = decipher . update ( ciph , 'base64' , 'utf8' ) ;
txt += decipher . final ( 'utf8' ) ;
assert . strictEqual ( txt , plaintext , 'encryption and decryption with Base64' ) ;
@ -119,12 +119,12 @@ testCipher2(Buffer.from('0123456789abcdef'));
const key = '0123456789abcdef' ;
const plaintext = 'Top secret!!!' ;
const c = crypto . createCipher ( 'aes192' , key ) ;
var ciph = c . update ( plaintext , 'utf16le' , 'base64' ) ;
let ciph = c . update ( plaintext , 'utf16le' , 'base64' ) ;
ciph += c . final ( 'base64' ) ;
var decipher = crypto . createDecipher ( 'aes192' , key ) ;
let decipher = crypto . createDecipher ( 'aes192' , key ) ;
var txt ;
let txt ;
assert . doesNotThrow ( ( ) => txt = decipher . update ( ciph , 'base64' , 'ucs2' ) ) ;
assert . doesNotThrow ( ( ) => txt += decipher . final ( 'ucs2' ) ) ;
assert . strictEqual ( txt , plaintext , 'decrypted result in ucs2' ) ;