nodejs常用暗号化方式
1748 ワード
nodejs常用暗号化方式
/**
* @
* @md5 ,
* @author lwt
* */
//
var crypto = require('crypto');
/**
* @aes192
* @param str string
* @param secret string ( , )
* @retrun string
* */
exports.getEncAse192 = function(str, secret) {
var cipher = crypto.createCipher("aes192", secret); //
var enc = cipher.update(str, "utf8", "hex"); // utf-8 hex;
enc += cipher.final("hex"); // hex;
return enc; //
}
/**
* @aes192
* @param str string
* @param secret string ( , )
* @retrun string
* */
exports.getDecAse192 = function(str, secret) {
var decipher = crypto.createDecipher("aes192", secret);
var dec = decipher.update(str, "hex", "utf8");// hex utf-8;
dec += decipher.final("utf8");// utf-8;
return dec;
}
/**
* @Hmac-sha1 ( , )
* @param str string
* @param secret string
* @retrun string
* */
exports.getHmac = function(str, secret) {
var buf = crypto.randomBytes(16);
secret = buf.toString("hex");// ;
var Signture = crypto.createHmac("sha1", secret);//
Signture.update(str);
var miwen=Signture.digest().toString("base64");// pbkdf2 ;
return miwen;
}
/**
* @sha1 ( , )
* @param str string
* @retrun string
* */
exports.getSha1 = function(str) {
var sha1 = crypto.createHash("sha1");// :md5 , md5 hash ;
sha1.update(str);
var res = sha1.digest("hex"); // d
return res;
}