首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >nodejs方法crypto.createCipheriv、crypto.createCipheriv.update和crypto.createCipheriv.final可以进行异步吗?

nodejs方法crypto.createCipheriv、crypto.createCipheriv.update和crypto.createCipheriv.final可以进行异步吗?
EN

Stack Overflow用户
提问于 2020-07-28 12:11:26
回答 1查看 521关注 0票数 2
代码语言:javascript
复制
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毫秒,但是如果有数千个并发用户,使用异步构建这些方法会更好吗?我不能在早期的测试中缩短它们,所以我想也许模块不能被异步?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-28 12:27:57

将函数设置为异步不会提高多个用户的性能,特别是如果这是一个REST服务。你应该考虑通过线程来利用你的硬件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63126907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档