crypto.ts 967 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import CryptoJS from 'crypto-js'
  2. import pako from 'pako'
  3. const CRYPTO_KEY = 'sd-designer'
  4. /**
  5. * 加密
  6. * @param msg 待加密字符串
  7. */
  8. export const encrypt = (msg: string) => {
  9. return CryptoJS.AES.encrypt(msg, CRYPTO_KEY).toString()
  10. }
  11. /**
  12. * 解密
  13. * @param ciphertext 待解密字符串
  14. */
  15. export const decrypt = (ciphertext: string) => {
  16. const bytes = CryptoJS.AES.decrypt(ciphertext, CRYPTO_KEY)
  17. return bytes.toString(CryptoJS.enc.Utf8)
  18. }
  19. export const zip = (str: string) => {
  20. const arr = pako.deflate(str, { gzip: true } as any);
  21. const ret = btoa(String.fromCharCode.apply(null, arr as any));
  22. return ret;
  23. }
  24. export const unzip = (b64Data: string) => {
  25. let strData = atob(b64Data);
  26. const charData = strData.split("").map(function (x) {
  27. return x.charCodeAt(0);
  28. });
  29. const binData = new Uint8Array(charData);
  30. const data = pako.inflate(binData);
  31. strData = new TextDecoder("utf-8").decode(data);
  32. return JSON.parse(strData);
  33. };