encryption.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. var crypto = require('crypto');
  2. exports.md5 = function (str){
  3. var md5sum = crypto.createHash('md5');
  4. md5sum.update(str);
  5. str = md5sum.digest('hex');
  6. return str;
  7. };
  8. exports.sha256 = function(str) {
  9. var sha = crypto.createHash('sha256');
  10. sha.update(str);
  11. return sha.digest('hex');
  12. }
  13. exports.sha1 = function (str){
  14. var sha1sum = crypto.createHash('sha1');
  15. sha1sum.update(str);
  16. str = sha1sum.digest('hex');
  17. return str;
  18. };
  19. exports.url_encode = function (url){
  20. url = encodeURIComponent(url);
  21. url = url.replace(/\%3A/g, ":");
  22. url = url.replace(/\%2F/g, "/");
  23. url = url.replace(/\%3F/g, "?");
  24. url = url.replace(/\%3D/g, "=");
  25. url = url.replace(/\%26/g, "&");
  26. return url;
  27. }
  28. exports.hmac_sha1 = function (app_key,str){
  29. crypto.createHmac('sha1', app_key).update(str).digest().toString('base64');
  30. }
  31. // 创建加密算法
  32. exports.aseEncode = function(data, password) {
  33. const cipher = crypto.createCipher('aes192', password);
  34. let crypted = cipher.update(data, 'utf-8', 'hex');
  35. crypted += cipher.final('hex');
  36. return crypted;
  37. };
  38. // 创建解密算法
  39. exports.aseDecode = function(data, password) {
  40. const decipher = crypto.createDecipher('aes192', password);
  41. let decrypted = decipher.update(data, 'hex', 'utf-8');
  42. decrypted += decipher.final('utf-8');
  43. return decrypted;
  44. };