oidc-plus4u-vault
Version:
CLI tool for managing access codes for insomnia-plugin-oidc-plus4u.
34 lines (23 loc) • 966 B
JavaScript
;
const crypto = require('crypto');
const IV_LENGTH = 16; // For AES, this is always 16
function _createKey(password){
return crypto.createHmac('sha256', password).digest();
}
function encrypt(text, password) {
let iv = crypto.randomBytes(IV_LENGTH);
let cipher = crypto.createCipheriv('aes-256-ctr', _createKey(password), iv);
let encrypted = cipher.update(text);
encrypted = Buffer.concat([encrypted, cipher.final()]);
return iv.toString('hex') + ':' + encrypted.toString('hex');
}
function decrypt(text, password) {
let textParts = text.split(':');
let iv = Buffer.from(textParts.shift(), 'hex');
let encryptedText = Buffer.from(textParts.join(':'), 'hex');
let decipher = crypto.createDecipheriv('aes-256-ctr', _createKey(password), iv);
let decrypted = decipher.update(encryptedText);
decrypted = Buffer.concat([decrypted, decipher.final()]);
return decrypted.toString();
}
module.exports = { decrypt, encrypt };