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毫秒,但是如果有数千个并发用户,使用异步构建这些方法会更好吗?我不能在早期的测试中缩短它们,所以我想也许模块不能被异步?
发布于 2020-07-28 12:27:57
将函数设置为异步不会提高多个用户的性能,特别是如果这是一个REST服务。你应该考虑通过线程来利用你的硬件。
https://stackoverflow.com/questions/63126907
复制相似问题