use aes-cbc

This commit is contained in:
Timo Hocker 2020-03-05 10:19:27 +01:00
parent 182527a690
commit 44cfbc0b9b

View File

@ -11,9 +11,8 @@ const crypto = require ('crypto');
const encoding = require ('@scode/encoding-helper'); const encoding = require ('@scode/encoding-helper');
const encryption = { const encryption = {
algorithm: 'aes-256-gcm', algorithm: 'aes-256-cbc',
nonce_size: 12, nonce_size: 16,
tag_size: 16,
key_size: 32, key_size: 32,
hash: 'sha256', hash: 'sha256',
salt_size: 16, salt_size: 16,
@ -153,23 +152,22 @@ function checksum (data) {
* @returns {string} encrypted * @returns {string} encrypted
*/ */
function encrypt_aes (text, pass) { function encrypt_aes (text, pass) {
const salt = crypto.randomBytes (16); const salt = crypto.randomBytes (encryption.salt_size);
// eslint-disable-next-line no-sync // eslint-disable-next-line no-sync
const key = crypto.pbkdf2Sync ( const key = crypto.pbkdf2Sync (
Buffer.from (pass, 'utf-8'), Buffer.from (pass, 'utf-8'),
salt, salt,
32767, encryption.iterations,
32, encryption.key_size,
'sha256' encryption.hash
); );
const nonce = crypto.randomBytes (12); const nonce = crypto.randomBytes (encryption.nonce_size);
const cipher = crypto.createCipheriv ('aes-256-gcm', key, nonce); const cipher = crypto.createCipheriv (encryption.algorithm, key, nonce);
return Buffer.concat ([ return Buffer.concat ([
salt, salt,
nonce, nonce,
cipher.update (Buffer.from (text)), cipher.update (Buffer.from (text)),
cipher.final (), cipher.final ()
cipher.getAuthTag ()
]) ])
.toString ('base64'); .toString ('base64');
} }
@ -196,12 +194,7 @@ function decrypt_aes (ciphertext, pass) {
); );
const nonce = buf.slice (0, encryption.nonce_size); const nonce = buf.slice (0, encryption.nonce_size);
buf = buf.slice (encryption.nonce_size); buf = buf.slice (encryption.nonce_size);
const tag = buf.slice (
buf.length - encryption.tag_size
);
buf = buf.slice (0, buf.length - encryption.tag_size);
const cipher = crypto.createDecipheriv (encryption.algorithm, key, nonce); const cipher = crypto.createDecipheriv (encryption.algorithm, key, nonce);
cipher.setAuthTag (tag);
return Buffer.concat ([ return Buffer.concat ([
cipher.update (buf), cipher.update (buf),
cipher.final () cipher.final ()