Node.js crypto

crypto作为Node.js的基本模块,用于提供通用的加密和哈希算法,使用纯JavaScript实现起速度会非常慢,Node.js使用C/C++实现算法后通过crypto模块暴露为JavaScript接口,使用方便,而且运行速度也快。

监测项目中是否已经安装crypto模块

//检查项目中是否包含crypto模块
try{
    const crypto = require("crypto");
}catch(e){
    console.error("crypto support is disabled");
}

例如:MD5是常用的哈希算法,用于给任意数据一个签名,通常使用十六进制字符串表示。

const value = "password";

const md5 = crypto.createHash("md5").update(value).digest("hex");

function md5(value){
  const hash= crypto.createHash("md5");
  //可任意多次调用update();
  hash.update(value);
  return hash.digest("hex");
}

封装

封装加密解密模块

$ vim cypher.js
//检查项目中是否包含crypto模块
let crypto;
try{
    crypto = require("crypto");
}catch(e){
    console.error("crypto support is disabled");
}
/**
 * 哈希加密
 * @param value mixed 需要加密的数据,默认为UTF-8的字符串或Buffer
 * @param type string 哈希类型可以为 md5/sha1/sha256/sha512
 * @return string 十六进制哈希值
 * */
exports.hash = (value, type="md5")=>{
    const hash = crypto.createHash(type);
    //可多次调用update(),update()方法默认字符串编码格式为UTF-8也可以传入Buffer
    hash.update(value);
    return hash.digest("hex");
};
/**
 * 随机数增强哈希加密
 * 利用MD5或SHA1等哈希算法进行加密,不同之处在于需传入密钥。
 * 只要密钥变化,同样输入的数据会得到不同的签名,可认为hmac是使用随机数增强的哈希算法。
 * @param value mixed 需要加密的数据,默认为UTF-8的字符串或Buffer
 * @param secret string 密钥
 * @param type string 哈希类型 md5/sha1/sha256/sha512
 * @return string 十六进制哈希值
 * */
exports.hmac = (value, secret, type="sha1")=>{
    const hmac = crypto.createHmac(type, secret);
    hmac.update(value);
    return hmac.digest("hex");
};
/**
 * AES对称加密
 * AES是常用的对称加密算法,加密解析都使用同一个密钥。
 * @param value 待加密数据
 * @param secret string 密钥
 * @param type string 对称加密算法类型,支持aes192/aes-128-ebc/aes-256-cbc等
 * */
exports.aesEncrypt = (value, secret, type="aes192")=>{
    const cipher = crypto.createCipher(type, secret);
    let crypted = cipher.update(value, "utf8", "hex");
    crypted += cipher.final("hex");
    return crypted;
};
/**
 * AES对称解密
 * */
exports.aesDecrypt = (crypted, secret)=>{
    const decipher = crypto.createDecipher("aes192", secret);
    let decrypted = decipher.update(crypted, "hex", "utf8");
    decrypted += decipher.final("utf8");
    return decrypted;
};

封装令牌模块

$ vim token.js
const cypher = require("cypher");
/**
 * 创建令牌
 * @param id
 * @param timestamp int 时间戳
 * @param secret string 密钥
 * @return string
 * */
exports.create = function(id, timestamp, secret){
    const code = id + "|" + timestamp;
    return cypher.aseEncrypt(code, secret);
};
/**
 * 解析令牌
 * @param token string 令牌字符串
 * @param secret string 密钥
 * @return string
 * */
exports.parse = function(token, secret){
    const result = cypher.aesDecrypt(token, secret);
    const arr = result.split("|");
    if(arr.length!==2){
        return null;
    }
    return {id:arr[0], timestamp:parseInt(arr[1])};
};
/**
 * 是否过期
 * @param timestamp int 时间戳
 * @param expire int 过期时间
 * @return bool
 * */
exports.isExpire = function(timestamp, expire){
  if(expire < 0){
      return true;
  }
  return Date.now() - timestamp < expire;
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值