aes.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import AES from 'crypto-js'
  2. //加密方法
  3. export function Encrypt(word) {
  4. //十六位十六进制数作为密钥
  5. const key = AES.enc.Utf8.parse('0123456789ASDFGH')
  6. //十六位十六进制数作为密钥偏移量
  7. const iv = AES.enc.Utf8.parse('ASDFGH0123456789')
  8. const src = AES.enc.Utf8.parse(word)
  9. const encrypted = AES.AES.encrypt(
  10. src,
  11. key,
  12. {
  13. iv: iv,
  14. mode: AES.mode.CBC,
  15. padding: AES.pad.Pkcs7
  16. })
  17. return encrypted.ciphertext.toString().toUpperCase()
  18. }
  19. //解密方法
  20. export function Decrypt(word) {
  21. const encryptedHexStr = AES.enc.Hex.parse(word)
  22. const src = AES.enc.Base64.stringify(encryptedHexStr)
  23. const decrypt = AES.AES.decrypt(
  24. src,
  25. key,
  26. {
  27. iv: iv,
  28. mode: AES.mode.CBC,
  29. padding: AES.pad.Pkcs7
  30. })
  31. const decryptedStr = decrypt.toString(AES.enc.Utf8)
  32. return decryptedStr.toString()
  33. }
  34. // 节流
  35. export function throttle(fn, time) {
  36. let timer = null
  37. time = time || 1000
  38. return function(...args) {
  39. if (timer) {
  40. return
  41. }
  42. const _this = this
  43. timer = setTimeout(() => {
  44. timer = null
  45. }, time)
  46. fn.apply(_this, args)
  47. }
  48. }
  49. // 防抖
  50. export function debounce(fn, time) {
  51. time = time || 200
  52. // 定时器
  53. let timer = null
  54. return function(...args) {
  55. var _this = this
  56. if (timer) {
  57. clearTimeout(timer)
  58. }
  59. timer = setTimeout(function() {
  60. timer = null
  61. fn.apply(_this, args)
  62. }, time)
  63. }
  64. }
  65. export function debounceMax(func, wait, immediate = false) {
  66. let timeout
  67. return function() {
  68. let context = this
  69. let args = arguments
  70. if (timeout) clearTimeout(timeout)
  71. if (immediate) {
  72. var callNow = !timeout
  73. timeout = setTimeout(() => {
  74. timeout = null
  75. }, wait)
  76. if (callNow) func.apply(context, args)
  77. } else {
  78. timeout = setTimeout(function() {
  79. func.apply(context, args)
  80. }, wait)
  81. }
  82. }
  83. }
  84. export function getClientIP(onNewIP) {
  85. let MyPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection
  86. let pc = new MyPeerConnection({
  87. iceServers: []
  88. })
  89. let noop = () => {
  90. }
  91. let localIPs = {}
  92. let ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g
  93. let iterateIP = (ip) => {
  94. if (!localIPs[ip]) onNewIP(ip)
  95. localIPs[ip] = true
  96. }
  97. pc.createDataChannel('')
  98. pc.createOffer().then((sdp) => {
  99. sdp.sdp.split('\n').forEach(function(line) {
  100. if (line.indexOf('candidate') < 0) return
  101. line.match(ipRegex).forEach(iterateIP)
  102. })
  103. pc.setLocalDescription(sdp, noop, noop)
  104. }).catch((reason) => {
  105. })
  106. pc.onicecandidate = (ice) => {
  107. if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return
  108. ice.candidate.candidate.match(ipRegex).forEach(iterateIP)
  109. }
  110. }