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');
}
}
以上是我的代码,我想知道加密和解密方法是否应该异步?使用我的测试数据执行时间不到 1 毫秒,但是如果有成千上万的并发用户,使用异步构建这些方法会更好吗?我无法在早期的测试中承诺它们,所以我想也许模块不能异步?
绝地无双
相关分类