猿问

nodejs 方法 crypto.createCipheriv

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 毫秒,但是如果有成千上万的并发用户,使用异步构建这些方法会更好吗?我无法在早期的测试中承诺它们,所以我想也许模块不能异步?


qq_花开花谢_0
浏览 233回答 1
1回答

绝地无双

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

相关分类

JavaScript
我要回答