nodejs-buffer

https://www.runoob.com/nodejs/nodejs-buffer.html

变更前new Buffer

var crypto = require('crypto');
module.exports = {
    encrypt: function (callback, plaintext, key, iv) {
        var ecb = 'des-ecb';//ecb  cbc
        var enkey = new Buffer(key);
        var eniv = new Buffer(iv ? iv : 0);
        var cipher = crypto.createCipheriv(ecb, enkey, eniv);
        cipher.setAutoPadding(true);  //default true
        var ciph = cipher.update(plaintext, 'utf8', 'base64');
        ciph += cipher.final('base64');
        callback(null /* error */, ciph);
    },
    decrypt: function (callback, encrypt_text, key, iv) {
        var ecb = 'des-ecb';//ecb cbc
        var dekey = new Buffer(key);
        var deiv = new Buffer(iv ? iv : 0);
        var decipher = crypto.createDecipheriv(ecb, dekey, deiv);
        decipher.setAutoPadding(true);
        var txt = decipher.update(encrypt_text, 'base64', 'utf8');
        txt += decipher.final('utf8');
        callback(null, txt);
    },
    encryptHmacSHA256: function (callback, plaintext, key) {
        var hashInBase64 = crypto.createHmac('sha256', key)
            .update(plaintext)
            .digest('hex');
        callback(null /* error */, hashInBase64);
    },
    createHashMd5: function (callback, plaintext, key) {
        var hashInBaseMd5 = crypto.createHash('md5').update(plaintext).digest("hex");
        callback(null /* error */, hashInBaseMd5);
    }
};

变更后Buffer.from

var crypto = require('crypto');
module.exports = {
    encrypt: function (callback, plaintext, key, iv) {
        var ecb = 'des-ecb';//ecb  cbc
        //var enkey = new Buffer(key);
        //var eniv = new Buffer(iv ? iv : 0);
        var enkey = Buffer.from(key);
        var eniv = Buffer.alloc(iv ? iv : 0);
        var cipher = crypto.createCipheriv(ecb, enkey, eniv);
        cipher.setAutoPadding(true);  //default true
        var ciph = cipher.update(plaintext, 'utf8', 'base64');
        ciph += cipher.final('base64');
        callback(null /* error */, ciph);
    },
    decrypt: function (callback, encrypt_text, key, iv) {
        var ecb = 'des-ecb';//ecb cbc
        //var dekey = new Buffer(key);
        //var deiv = new Buffer(iv ? iv : 0);
        var dekey = Buffer.from(key);
        var deiv = Buffer.alloc(iv ? iv : 0);
        var decipher = crypto.createDecipheriv(ecb, dekey, deiv);
        decipher.setAutoPadding(true);
        var txt = decipher.update(encrypt_text, 'base64', 'utf8');
        txt += decipher.final('utf8');
        callback(null, txt);
    },
    encryptHmacSHA256: function (callback, plaintext, key) {
        var hashInBase64 = crypto.createHmac('sha256', key)
            .update(plaintext)
            .digest('hex');
        callback(null /* error */, hashInBase64);
    },
    createHashMd5: function (callback, plaintext, key) {
        var hashInBaseMd5 = crypto.createHash('md5').update(plaintext).digest("hex");
        callback(null /* error */, hashInBaseMd5);
    }
};
文档更新时间: 2021-11-28 17:20   作者:admin