为了账号安全,请及时绑定邮箱和手机立即绑定

nodejs 方法 crypto.createCipheriv

nodejs 方法 crypto.createCipheriv

qq_花开花谢_0 2022-12-09 16:30:39
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 毫秒,但是如果有成千上万的并发用户,使用异步构建这些方法会更好吗?我无法在早期的测试中承诺它们,所以我想也许模块不能异步?
查看完整描述

1 回答

?
绝地无双

TA贡献1946条经验 获得超4个赞

使函数异步不会提高多个用户的性能,特别是如果这是一个 REST 服务。你应该看看通过线程使用你的硬件。



查看完整回答
反对 回复 2022-12-09
  • 1 回答
  • 0 关注
  • 232 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信