crypt.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //import cryptojs from 'crypto-js'
  2. const cryptojs = require('crypto-js');
  3. var codecutil = {}
  4. export default codecutil //全局导出对象
  5. //MD5(Message-Digest Algorithm),信息摘要算法
  6. //一种被广泛使用的密码散列函数,可以产生出一个128位(16字节)的散列值(hash value),用于确保信息传输完整一致
  7. codecutil.md5 = function (data) {
  8. return cryptojs.MD5(data).toString()
  9. }
  10. //SHA1(Secure Hash Algorithm 1),安全散列算法,
  11. //SHA-1可以生成一个被称为消息摘要的160位(20字节)散列值,散列值通常的呈现形式为40个十六进制数
  12. codecutil.sha1 = function (data) {
  13. return cryptojs.SHA1(data).toString()
  14. }
  15. //SHA256(Secure Hash Algorithm 1),安全散列算法,
  16. //对于任意长度的消息,SHA256都会产生一个256位的哈希值,称作消息摘要。这个摘要相当于是个长度为32个字节的数组,共256位,通常由一个长度为64的十六进制字符串来表示
  17. codecutil.sha256 = function (data) {
  18. return cryptojs.SHA256(data).toString()
  19. }
  20. //解析密钥
  21. codecutil.aesDeKey = function (key) {
  22. return cryptojs.enc.Utf8.parse(key)
  23. }
  24. //aes加密
  25. codecutil.aesEncode = function (data, key) {
  26. if (data && key) {
  27. let aesKey = cryptojs.enc.Utf8.parse(key) //将密钥按照utf8格式化
  28. let content = cryptojs.enc.Utf8.parse(data)//将明文按照utf8格式化
  29. //加密后已经是base64编码格式的数据
  30. const ciphertext = cryptojs.AES.encrypt(content, aesKey, { iv: aesKey, mode: cryptojs.mode.CBC, padding: cryptojs.pad.Pkcs7 }).toString()
  31. return ciphertext
  32. } else {
  33. return ''
  34. }
  35. }
  36. //aes解密
  37. codecutil.aesDecode = function (data, key) {
  38. if (data && key) {
  39. var ciphertext = codecutil.base64Decode(data)
  40. // var bytes = cryptojs.AES.decrypt(ciphertext, key, { mode: cryptojs.mode.ECB, padding: cryptojs.pad.Pkcs7 })
  41. var bytes = cryptojs.AES.decrypt(ciphertext, key, { mode: cryptojs.mode.CBC, padding: cryptojs.pad.Pkcs7 })
  42. var plaintext = bytes.toString(cryptojs.enc.Utf8)
  43. return plaintext
  44. } else {
  45. return ''
  46. }
  47. }
  48. //base64加密
  49. codecutil.base64Encode = function (data) {
  50. if (data) {
  51. var bytes = cryptojs.enc.Utf8.parse(data)
  52. var plaintext = bytes.toString(cryptojs.enc.Base64)
  53. plaintext = plaintext.toString()
  54. // plaintext = plaintext.replace(/\//g, '_')
  55. // plaintext = plaintext.replace(/\+/g, '-')
  56. // plaintext = plaintext.replace(/=/g, '')
  57. return plaintext
  58. } else {
  59. return ''
  60. }
  61. }
  62. //base64解密
  63. codecutil.base64Decode = function (data) {
  64. if (data) {
  65. var ciphertext = data.toString()
  66. ciphertext = ciphertext.replace(/_/g, '/')
  67. ciphertext = ciphertext.replace(/-/g, '+')
  68. for (var i = 0; i < 4 - ciphertext.length % 4; i++) {
  69. ciphertext = ciphertext + '='
  70. }
  71. var bytes = cryptojs.enc.Base64.parse(ciphertext)
  72. var plaintext = bytes.toString(cryptojs.enc.Utf8)
  73. return plaintext
  74. } else {
  75. return ''
  76. }
  77. }
  78. codecutil.hexEncode = function (data) {
  79. if (data) {
  80. var bytes = cryptojs.enc.Utf8.parse(data)
  81. var plaintext = bytes.toString(cryptojs.enc.Hex)
  82. return plaintext
  83. } else {
  84. return ''
  85. }
  86. }
  87. codecutil.hexDecode = function (data) {
  88. if (data) {
  89. var bytes = cryptojs.enc.Hex.parse(data)
  90. var plaintext = bytes.toString(cryptojs.enc.Utf8)
  91. return plaintext
  92. } else {
  93. return ''
  94. }
  95. }