您当前的位置: 首页 > 

培根芝士

暂无认证

  • 4浏览

    0关注

    446博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

NodeJS使用crypto进行MD5加密

培根芝士 发布时间:2018-07-06 18:52:59 ,浏览量:4

crypto 是Nodejs的内置模块,提供了加密功能,包括对 OpenSSL 的哈希、HMAC、加密、解密、签名、以及验证功能的一整套封装。

MD5加密

const crypto = require('crypto');
const md5 = crypto.createHash('md5');
var cryptostr = md5.update('Hello, world!').digest('hex');

SHA1加密

const crypto = require('crypto');
const md5 = crypto.createHash('sha1');
var cryptostr = md5.update('Hello, world!').digest('hex');

HMAC加密

HMAC算法也是一种哈希算法,它可以利用MD5或SHA1等哈希算法,需要配置密钥。

const crypto = require('crypto');
const hmac = crypto.createHmac('sha256', 'secret-key');
var cryptostr = md5.update('Hello, world!').digest('hex');

 AES加解密

const crypto = require('crypto');

function aesEncrypt(data, key) {
    const cipher = crypto.createCipher('aes192', key);
    var crypted = cipher.update(data, 'utf8', 'hex');
    crypted += cipher.final('hex');
    return crypted;
}

function aesDecrypt(encrypted, key) {
    const decipher = crypto.createDecipher('aes192', key);
    var decrypted = decipher.update(encrypted, 'hex', 'utf8');
    decrypted += decipher.final('utf8');
    return decrypted;
}

 

关注
打赏
1660824269
查看更多评论
立即登录/注册

微信扫码登录

0.0347s