const crypto = require('crypto');
const util = require('util');
class AES {
constructor(key, iv) {
if (!key) throw new Error('A 32 byte / 256 bit key is required.');
if (!iv) throw new Error('Initialization vector is required.');
this.key = key;
this.iv = iv;
}
encrypt(data) {
let cipher = crypto.createCipheriv('aes-256-cbc', this.key, this.iv);
let encrypted = cipher.update(data, 'utf-8', 'hex');
encrypted += cipher.final('hex');
return encrypted;
}
decrypt(data) {
let decipher = crypto.createDecipheriv('aes-256-cbc', this.key, this.iv);
let decrypted = decipher.update(data, 'hex', 'utf-8');
decrypted += decipher.final('utf-8');
return decrypted;
}
static randomBytes = async bytes => {
let result;
result = await util.promisify(crypto.randomBytes)(bytes);
return result;
}
sha256(data) {
return crypto.createHash('sha256').update(data).digest('hex');
}
}
The above is my code and I'm wondering if the encrypt and decrypt methods should be made async? It takes less than 1 ms to execute using my test data, but w/thousands of concurrent users, would it be better to build these methods using async? I wasn't able to promisify them in earlier tests so I thought maybe the module can't be async'd?