1// Copyright Joyent, Inc. and other Node contributors. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to permit 8// persons to whom the Software is furnished to do so, subject to the 9// following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20// USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22'use strict'; 23const common = require('../common'); 24if (!common.hasCrypto) 25 common.skip('missing crypto'); 26 27const assert = require('assert'); 28const crypto = require('crypto'); 29 30const iv = Buffer.from('00000000000000000000000000000000', 'hex'); 31const key = Buffer.from('0123456789abcdef0123456789abcdef' + 32 '0123456789abcdef0123456789abcdef', 'hex'); 33 34function encrypt(val, pad) { 35 const c = crypto.createCipheriv('aes256', key, iv); 36 c.setAutoPadding(pad); 37 return c.update(val, 'utf8', 'latin1') + c.final('latin1'); 38} 39 40function decrypt(val, pad) { 41 const c = crypto.createDecipheriv('aes256', key, iv); 42 c.setAutoPadding(pad); 43 return c.update(val, 'latin1', 'utf8') + c.final('utf8'); 44} 45 46// echo 0123456789abcdef0123456789abcdef \ 47// | openssl enc -e -aes256 -nopad -K <key> -iv <iv> \ 48// | openssl enc -d -aes256 -nopad -K <key> -iv <iv> 49let plaintext = '0123456789abcdef0123456789abcdef'; // Multiple of block size 50let encrypted = encrypt(plaintext, false); 51let decrypted = decrypt(encrypted, false); 52assert.strictEqual(decrypted, plaintext); 53 54// echo 0123456789abcdef0123456789abcde \ 55// | openssl enc -e -aes256 -K <key> -iv <iv> \ 56// | openssl enc -d -aes256 -K <key> -iv <iv> 57plaintext = '0123456789abcdef0123456789abcde'; // not a multiple 58encrypted = encrypt(plaintext, true); 59decrypted = decrypt(encrypted, true); 60assert.strictEqual(decrypted, plaintext); 61