Original version:
const CRYPTO = require("crypto");
const ALG_CIPHER = "aes-256-gcm";
const ENC_IN = "utf8";
const ENC_OUT = "base64";
const BUFFER_SIZE = 16;
const KEY = Buffer.from("...", ENC_IN);
/**
* Encrypt data using an initialisation vector
*
* @param {string} data - data to be encrypted
* @returns {Promise<{authTag: Buffer, data: string, iv: Buffer}>} encrypted - encrypted data with decryption parameters
*/
const encrypt = async function encrypt(data) {
const IV = Buffer.from(CRYPTO.randomBytes(BUFFER_SIZE));
const CIPHER = CRYPTO.createCipheriv(ALG_CIPHER, KEY, IV);
let enc = CIPHER.update(data, ENC_IN, ENC_OUT);
enc += CIPHER.final(ENC_OUT);
const ENCRYPTED = Object.freeze({
data: enc,
iv: IV,
authTag: CIPHER.getAuthTag()
});
return ENCRYPTED;
};
/**
* Decrypt data using an initialisation vector
*
* @param {string} data - data to be decrypted
* @param {string} iv - initialisation vector
* @param {Buffer} authTag - authentication tag
* @returns {Promise<string>} decrypted - decrypted data
*/
const decrypt = async function decrypt(data, iv, authTag) {
const DECIPHER = CRYPTO.createDecipheriv(ALG_CIPHER, KEY, iv);
DECIPHER.setAuthTag(authTag);
let decrypted = DECIPHER.update(data, ENC_OUT, ENC_IN);
decrypted += DECIPHER.final(ENC_IN);
return decrypted;
};
Updated on 01.03.2020:
Changes:
ENC_IN
andENC_OUT
have been renamed toENCODING_IN
andENCODING_OUT
respectively;encrypt().ENCRYPTED.data
is now Buffer;decrypt().iv
is now Buffer;- Arranged order of properties in
encrypt().ENCRYPTED
anddecrypt()
.
const CRYPTO = require("crypto");
const ALG_CIPHER = "aes-256-gcm";
const ENCODING_IN = "utf8";
const ENCODING_OUT = "base64";
const BUFFER_SIZE = 16;
const KEY = Buffer.from("...", ENCODING_IN);
/**
* Encrypt data using an initialisation vector
*
* @param {string} data — data for encryption in UTF-8
* @returns {Promise<Readonly<{authTag: Buffer, data: Buffer, iv: Buffer}>>} encrypted — encrypted data with decryption parameters
*/
const encrypt = async function encrypt(data) {
const iv = Buffer.from(crypto.randomBytes(BUFFER_SIZE));
const cipher = crypto.createCipheriv(ALG_CIPHER, key, iv);
let enc = cipher.update(data, ENCODING_IN, ENCODING_OUT);
enc += cipher.final(ENCODING_OUT);
const ENCRYPTED = Object.freeze({
authTag: cipher.getAuthTag(),
data: Buffer.from(enc, ENCODING_OUT),
iv: iv
});
return ENCRYPTED;
};
/**
* Decrypt data using an initialisation vector
*
* @param {Buffer} authTag — authentication tag
* @param {Buffer} data — data for decryption
* @param {Buffer} iv — initialisation vector
* @returns {Promise<string>} decrypted — decrypted data in UTF-8
*/
const decrypt = async function decrypt(authTag, data, iv) {
const decipher = crypto.createDecipheriv(ALG_CIPHER, key, iv);
decipher.setAuthTag(authTag);
let decrypted = decipher.update(data, ENCODING_OUT, ENCODING_IN);
decrypted += decipher.final(ENCODING_IN);
return decrypted;
};
Original version:
const CRYPTO = require("crypto");
const ALG_CIPHER = "aes-256-gcm";
const ENC_IN = "utf8";
const ENC_OUT = "base64";
const BUFFER_SIZE = 16;
const KEY = Buffer.from("...", ENC_IN);
/**
* Encrypt data using an initialisation vector
*
* @param {string} data - data to be encrypted
* @returns {Promise<{authTag: Buffer, data: string, iv: Buffer}>} encrypted - encrypted data with decryption parameters
*/
const encrypt = async function encrypt(data) {
const IV = Buffer.from(CRYPTO.randomBytes(BUFFER_SIZE));
const CIPHER = CRYPTO.createCipheriv(ALG_CIPHER, KEY, IV);
let enc = CIPHER.update(data, ENC_IN, ENC_OUT);
enc += CIPHER.final(ENC_OUT);
const ENCRYPTED = Object.freeze({
data: enc,
iv: IV,
authTag: CIPHER.getAuthTag()
});
return ENCRYPTED;
};
/**
* Decrypt data using an initialisation vector
*
* @param {string} data - data to be decrypted
* @param {string} iv - initialisation vector
* @param {Buffer} authTag - authentication tag
* @returns {Promise<string>} decrypted - decrypted data
*/
const decrypt = async function decrypt(data, iv, authTag) {
const DECIPHER = CRYPTO.createDecipheriv(ALG_CIPHER, KEY, iv);
DECIPHER.setAuthTag(authTag);
let decrypted = DECIPHER.update(data, ENC_OUT, ENC_IN);
decrypted += DECIPHER.final(ENC_IN);
return decrypted;
};
Updated on 01.03.2020:
Changes:
ENC_IN
andENC_OUT
have been renamed toENCODING_IN
andENCODING_OUT
respectively;encrypt().ENCRYPTED.data
is now Buffer;decrypt().iv
is now Buffer;- Arranged order of properties in
encrypt().ENCRYPTED
anddecrypt()
.
const CRYPTO = require("crypto");
const ALG_CIPHER = "aes-256-gcm";
const ENCODING_IN = "utf8";
const ENCODING_OUT = "base64";
const BUFFER_SIZE = 16;
const KEY = Buffer.from("...", ENCODING_IN);
/**
* Encrypt data using an initialisation vector
*
* @param {string} data — data for encryption in UTF-8
* @returns {Promise<Readonly<{authTag: Buffer, data: Buffer, iv: Buffer}>>} encrypted — encrypted data with decryption parameters
*/
const encrypt = async function encrypt(data) {
const iv = Buffer.from(crypto.randomBytes(BUFFER_SIZE));
const cipher = crypto.createCipheriv(ALG_CIPHER, key, iv);
let enc = cipher.update(data, ENCODING_IN, ENCODING_OUT);
enc += cipher.final(ENCODING_OUT);
const ENCRYPTED = Object.freeze({
authTag: cipher.getAuthTag(),
data: Buffer.from(enc, ENCODING_OUT),
iv: iv
});
return ENCRYPTED;
};
/**
* Decrypt data using an initialisation vector
*
* @param {Buffer} authTag — authentication tag
* @param {Buffer} data — data for decryption
* @param {Buffer} iv — initialisation vector
* @returns {Promise<string>} decrypted — decrypted data in UTF-8
*/
const decrypt = async function decrypt(authTag, data, iv) {
const decipher = crypto.createDecipheriv(ALG_CIPHER, key, iv);
decipher.setAuthTag(authTag);
let decrypted = decipher.update(data, ENCODING_OUT, ENCODING_IN);
decrypted += decipher.final(ENCODING_IN);
return decrypted;
};
const CRYPTO = require("crypto");
const ALG_CIPHER = "aes-256-gcm";
const ENC_IN = "utf8";
const ENC_OUT = "base64";
const BUFFER_SIZE = 16;
const KEY = Buffer.from("...", ENC_IN);
/**
* Encrypt data using an initialisation vector
*
* @param {string} data - data to be encrypted
* @returns {Promise<{authTag: Buffer, data: string, iv: Buffer}>} encrypted - encrypted data with decryption parameters
*/
const encrypt = async function encrypt(data) {
const IV = Buffer.from(CRYPTO.randomBytes(BUFFER_SIZE));
const CIPHER = CRYPTO.createCipheriv(ALG_CIPHER, KEY, IV);
let enc = CIPHER.update(data, ENC_IN, ENC_OUT);
enc += CIPHER.final(ENC_OUT);
const ENCRYPTED = Object.freeze({
data: enc,
iv: IV,
authTag: CIPHER.getAuthTag()
});
return ENCRYPTED;
};
/**
* Decrypt data using an initialisation vector
*
* @param {string} data - data to be decrypted
* @param {string} iv - initialisation vector
* @param {Buffer} authTag - authentication tag
* @returns {Promise<string>} decrypted - decrypted data
*/
const decrypt = async function decrypt(data, iv, authTag) {
const DECIPHER = CRYPTO.createDecipheriv(ALG_CIPHER, KEY, iv);
DECIPHER.setAuthTag(authTag);
let decrypted = DECIPHER.update(data, ENC_OUT, ENC_IN);
decrypted += DECIPHER.final(ENC_IN);
return decrypted;
};
- Removed
ENC_IN
fromandBuffer.from("...")ENC_OUT
ofhave been renamed toKEYENCODING_IN
; andENCODING_OUT
respectively; encrypt().ENCRYPTED.data
is now Buffer;decrypt().iv
is now Buffer;- Arranged order of properties in
encrypt().ENCRYPTED
anddecrypt()
.
const CRYPTO = require("crypto");
const ALG_CIPHER = "aes-256-gcm";
const ENC_INENCODING_IN = "utf8";
const ENC_OUTENCODING_OUT = "base64";
const BUFFER_SIZE = 16;
const KEY = Buffer.from("...", ENCODING_IN);
/**
* Encrypt data using an initialisation vector
*
* @param {string} data — data for encryption in UTF-8
* @returns {Promise<Readonly<{authTag: Buffer, data: Buffer, iv: Buffer}>>} encrypted — encrypted data with decryption parameters
*/
const encrypt = async function encrypt(data) {
const iv = Buffer.from(crypto.randomBytes(BUFFER_SIZE));
const cipher = crypto.createCipheriv(ALG_CIPHER, key, iv);
let enc = cipher.update(data, ENC_INENCODING_IN, ENC_OUTENCODING_OUT);
enc += cipher.final(ENC_OUTENCODING_OUT);
const ENCRYPTED = Object.freeze({
authTag: cipher.getAuthTag(),
data: Buffer.from(enc, ENC_OUTENCODING_OUT),
iv: iv
});
return ENCRYPTED;
};
/**
* Decrypt data using an initialisation vector
*
* @param {Buffer} authTag — authentication tag
* @param {Buffer} data — data for decryption
* @param {Buffer} iv — initialisation vector
* @returns {Promise<string>} decrypted — decrypted data in UTF-8
*/
const decrypt = async function decrypt(authTag, data, iv) {
const decipher = crypto.createDecipheriv(ALG_CIPHER, key, iv);
decipher.setAuthTag(authTag);
let decrypted = decipher.update(data, ENC_OUTENCODING_OUT, ENC_INENCODING_IN);
decrypted += decipher.final(ENC_INENCODING_IN);
return decrypted;
};
- Removed
ENC_IN
fromBuffer.from("...")
ofKEY
; encrypt().ENCRYPTED.data
is now Buffer;decrypt().iv
is now Buffer;- Arranged order of properties in
encrypt().ENCRYPTED
anddecrypt()
.
const CRYPTO = require("crypto");
const ALG_CIPHER = "aes-256-gcm";
const ENC_IN = "utf8";
const ENC_OUT = "base64";
const BUFFER_SIZE = 16;
const KEY = Buffer.from("...");
/**
* Encrypt data using an initialisation vector
*
* @param {string} data — data for encryption in UTF-8
* @returns {Promise<Readonly<{authTag: Buffer, data: Buffer, iv: Buffer}>>} encrypted — encrypted data with decryption parameters
*/
const encrypt = async function encrypt(data) {
const iv = Buffer.from(crypto.randomBytes(BUFFER_SIZE));
const cipher = crypto.createCipheriv(ALG_CIPHER, key, iv);
let enc = cipher.update(data, ENC_IN, ENC_OUT);
enc += cipher.final(ENC_OUT);
const ENCRYPTED = Object.freeze({
authTag: cipher.getAuthTag(),
data: Buffer.from(enc, ENC_OUT),
iv: iv
});
return ENCRYPTED;
};
/**
* Decrypt data using an initialisation vector
*
* @param {Buffer} authTag — authentication tag
* @param {Buffer} data — data for decryption
* @param {Buffer} iv — initialisation vector
* @returns {Promise<string>} decrypted — decrypted data in UTF-8
*/
const decrypt = async function decrypt(authTag, data, iv) {
const decipher = crypto.createDecipheriv(ALG_CIPHER, key, iv);
decipher.setAuthTag(authTag);
let decrypted = decipher.update(data, ENC_OUT, ENC_IN);
decrypted += decipher.final(ENC_IN);
return decrypted;
};
ENC_IN
andENC_OUT
have been renamed toENCODING_IN
andENCODING_OUT
respectively;encrypt().ENCRYPTED.data
is now Buffer;decrypt().iv
is now Buffer;- Arranged order of properties in
encrypt().ENCRYPTED
anddecrypt()
.
const CRYPTO = require("crypto");
const ALG_CIPHER = "aes-256-gcm";
const ENCODING_IN = "utf8";
const ENCODING_OUT = "base64";
const BUFFER_SIZE = 16;
const KEY = Buffer.from("...", ENCODING_IN);
/**
* Encrypt data using an initialisation vector
*
* @param {string} data — data for encryption in UTF-8
* @returns {Promise<Readonly<{authTag: Buffer, data: Buffer, iv: Buffer}>>} encrypted — encrypted data with decryption parameters
*/
const encrypt = async function encrypt(data) {
const iv = Buffer.from(crypto.randomBytes(BUFFER_SIZE));
const cipher = crypto.createCipheriv(ALG_CIPHER, key, iv);
let enc = cipher.update(data, ENCODING_IN, ENCODING_OUT);
enc += cipher.final(ENCODING_OUT);
const ENCRYPTED = Object.freeze({
authTag: cipher.getAuthTag(),
data: Buffer.from(enc, ENCODING_OUT),
iv: iv
});
return ENCRYPTED;
};
/**
* Decrypt data using an initialisation vector
*
* @param {Buffer} authTag — authentication tag
* @param {Buffer} data — data for decryption
* @param {Buffer} iv — initialisation vector
* @returns {Promise<string>} decrypted — decrypted data in UTF-8
*/
const decrypt = async function decrypt(authTag, data, iv) {
const decipher = crypto.createDecipheriv(ALG_CIPHER, key, iv);
decipher.setAuthTag(authTag);
let decrypted = decipher.update(data, ENCODING_OUT, ENCODING_IN);
decrypted += decipher.final(ENCODING_IN);
return decrypted;
};
Original version:
Updated on 01.03.2020:
Changes:
- Removed
ENC_IN
fromBuffer.from("...")
ofKEY
; encrypt().ENCRYPTED.data
is now Buffer;decrypt().iv
is now Buffer;- Arranged order of properties in
encrypt().ENCRYPTED
anddecrypt()
.
const CRYPTO = require("crypto");
const ALG_CIPHER = "aes-256-gcm";
const ENC_IN = "utf8";
const ENC_OUT = "base64";
const BUFFER_SIZE = 16;
const KEY = Buffer.from("...", ENC_IN);
/**
* Encrypt data using an initialisation vector
*
* @param {string} data — data for encryption in UTF-8
* @returns {Promise<Readonly<{authTag: Buffer, data: Buffer, iv: Buffer}>>} encrypted — encrypted data with decryption parameters
*/
const encrypt = async function encrypt(data) {
const iv = Buffer.from(crypto.randomBytes(BUFFER_SIZE));
const cipher = crypto.createCipheriv(ALG_CIPHER, key, iv);
let enc = cipher.update(data, ENC_IN, ENC_OUT);
enc += cipher.final(ENC_OUT);
const ENCRYPTED = Object.freeze({
authTag: cipher.getAuthTag(),
data: Buffer.from(enc, ENC_OUT),
iv: iv
});
return ENCRYPTED;
};
/**
* Decrypt data using an initialisation vector
*
* @param {Buffer} authTag — authentication tag
* @param {Buffer} data — data for decryption
* @param {Buffer} iv — initialisation vector
* @returns {Promise<string>} decrypted — decrypted data in UTF-8
*/
const decrypt = async function decrypt(authTag, data, iv) {
const decipher = crypto.createDecipheriv(ALG_CIPHER, key, iv);
decipher.setAuthTag(authTag);
let decrypted = decipher.update(data, ENC_OUT, ENC_IN);
decrypted += decipher.final(ENC_IN);
return decrypted;
};
Original version
Updated on 01.03.2020
const CRYPTO = require("crypto");
const ALG_CIPHER = "aes-256-gcm";
const ENC_IN = "utf8";
const ENC_OUT = "base64";
const BUFFER_SIZE = 16;
const KEY = Buffer.from("...", ENC_IN);
/**
* Encrypt data using an initialisation vector
*
* @param {string} data — data for encryption in UTF-8
* @returns {Promise<Readonly<{authTag: Buffer, data: Buffer, iv: Buffer}>>} encrypted — encrypted data with decryption parameters
*/
const encrypt = async function encrypt(data) {
const iv = Buffer.from(crypto.randomBytes(BUFFER_SIZE));
const cipher = crypto.createCipheriv(ALG_CIPHER, key, iv);
let enc = cipher.update(data, ENC_IN, ENC_OUT);
enc += cipher.final(ENC_OUT);
const ENCRYPTED = Object.freeze({
authTag: cipher.getAuthTag(),
data: Buffer.from(enc, ENC_OUT),
iv: iv
});
return ENCRYPTED;
};
/**
* Decrypt data using an initialisation vector
*
* @param {Buffer} authTag — authentication tag
* @param {Buffer} data — data for decryption
* @param {Buffer} iv — initialisation vector
* @returns {Promise<string>} decrypted — decrypted data in UTF-8
*/
const decrypt = async function decrypt(authTag, data, iv) {
const decipher = crypto.createDecipheriv(ALG_CIPHER, key, iv);
decipher.setAuthTag(authTag);
let decrypted = decipher.update(data, ENC_OUT, ENC_IN);
decrypted += decipher.final(ENC_IN);
return decrypted;
};
Original version:
Updated on 01.03.2020:
Changes:
- Removed
ENC_IN
fromBuffer.from("...")
ofKEY
; encrypt().ENCRYPTED.data
is now Buffer;decrypt().iv
is now Buffer;- Arranged order of properties in
encrypt().ENCRYPTED
anddecrypt()
.
const CRYPTO = require("crypto");
const ALG_CIPHER = "aes-256-gcm";
const ENC_IN = "utf8";
const ENC_OUT = "base64";
const BUFFER_SIZE = 16;
const KEY = Buffer.from("...");
/**
* Encrypt data using an initialisation vector
*
* @param {string} data — data for encryption in UTF-8
* @returns {Promise<Readonly<{authTag: Buffer, data: Buffer, iv: Buffer}>>} encrypted — encrypted data with decryption parameters
*/
const encrypt = async function encrypt(data) {
const iv = Buffer.from(crypto.randomBytes(BUFFER_SIZE));
const cipher = crypto.createCipheriv(ALG_CIPHER, key, iv);
let enc = cipher.update(data, ENC_IN, ENC_OUT);
enc += cipher.final(ENC_OUT);
const ENCRYPTED = Object.freeze({
authTag: cipher.getAuthTag(),
data: Buffer.from(enc, ENC_OUT),
iv: iv
});
return ENCRYPTED;
};
/**
* Decrypt data using an initialisation vector
*
* @param {Buffer} authTag — authentication tag
* @param {Buffer} data — data for decryption
* @param {Buffer} iv — initialisation vector
* @returns {Promise<string>} decrypted — decrypted data in UTF-8
*/
const decrypt = async function decrypt(authTag, data, iv) {
const decipher = crypto.createDecipheriv(ALG_CIPHER, key, iv);
decipher.setAuthTag(authTag);
let decrypted = decipher.update(data, ENC_OUT, ENC_IN);
decrypted += decipher.final(ENC_IN);
return decrypted;
};
default