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 毫秒,但是如果有成千上萬的并發用戶,使用異步構建這些方法會更好嗎?我無法在早期的測試中承諾它們,所以我想也許模塊不能異步?
nodejs 方法 crypto.createCipheriv
qq_花開花謝_0
2022-12-09 16:30:39