Crypto.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // 移除自执行函数和全局变量挂载逻辑,改为直接定义并导出
  2. const base64map = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  3. // 直接定义 Crypto 对象(不再依赖 window 或 exports)
  4. const Crypto = {};
  5. // Crypto 工具方法
  6. Crypto.util = {
  7. rotl: function (n, b) {
  8. return (n << b) | (n >>> (32 - b));
  9. },
  10. rotr: function (n, b) {
  11. return (n << (32 - b)) | (n >>> b);
  12. },
  13. endian: function (n) {
  14. if (n.constructor === Number) {
  15. return (Crypto.util.rotl(n, 8) & 16711935) | (Crypto.util.rotl(n, 24) & 4278255360);
  16. }
  17. for (let i = 0; i < n.length; i++) {
  18. n[i] = Crypto.util.endian(n[i]);
  19. }
  20. return n;
  21. },
  22. randomBytes: function (n) {
  23. const bytes = [];
  24. for (; n > 0; n--) {
  25. bytes.push(Math.floor(Math.random() * 256));
  26. }
  27. return bytes;
  28. },
  29. bytesToWords: function (bytes) {
  30. const words = [];
  31. for (let i = 0, b = 0; i < bytes.length; i++, b += 8) {
  32. words[b >>> 5] |= (bytes[i] & 255) << (24 - (b % 32));
  33. }
  34. return words;
  35. },
  36. wordsToBytes: function (words) {
  37. const bytes = [];
  38. for (let b = 0; b < words.length * 32; b += 8) {
  39. bytes.push((words[b >>> 5] >>> (24 - (b % 32))) & 255);
  40. }
  41. return bytes;
  42. },
  43. bytesToHex: function (bytes) {
  44. const hex = [];
  45. for (let i = 0; i < bytes.length; i++) {
  46. hex.push((bytes[i] >>> 4).toString(16));
  47. hex.push((bytes[i] & 15).toString(16));
  48. }
  49. return hex.join('');
  50. },
  51. hexToBytes: function (hex) {
  52. const bytes = [];
  53. for (let c = 0; c < hex.length; c += 2) {
  54. bytes.push(parseInt(hex.substr(c, 2), 16));
  55. }
  56. return bytes;
  57. },
  58. bytesToBase64: function (bytes) {
  59. if (typeof btoa === 'function') {
  60. return btoa(Crypto.charenc.Binary.bytesToString(bytes));
  61. }
  62. const base64 = [];
  63. for (let i = 0; i < bytes.length; i += 3) {
  64. const triplet = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2];
  65. for (let j = 0; j < 4; j++) {
  66. if (i * 8 + j * 6 <= bytes.length * 8) {
  67. base64.push(base64map.charAt((triplet >>> (6 * (3 - j))) & 63));
  68. } else {
  69. base64.push('=');
  70. }
  71. }
  72. }
  73. return base64.join('');
  74. },
  75. base64ToBytes: function (base64) {
  76. if (typeof atob === 'function') {
  77. return Crypto.charenc.Binary.stringToBytes(atob(base64));
  78. }
  79. base64 = base64.replace(/[^A-Z0-9+\/]/gi, '');
  80. const bytes = [];
  81. for (let i = 0, imod4 = 0; i < base64.length; imod4 = ++i % 4) {
  82. if (imod4 === 0) continue;
  83. bytes.push(
  84. ((base64map.indexOf(base64.charAt(i - 1)) & (Math.pow(2, -2 * imod4 + 8) - 1)) << (imod4 * 2)) |
  85. (base64map.indexOf(base64.charAt(i)) >>> (6 - imod4 * 2))
  86. );
  87. }
  88. return bytes;
  89. }
  90. };
  91. // 字符编码模块
  92. Crypto.charenc = {};
  93. // UTF-8 编码
  94. Crypto.charenc.UTF8 = {
  95. stringToBytes: function (str) {
  96. return Crypto.charenc.Binary.stringToBytes(unescape(encodeURIComponent(str)));
  97. },
  98. bytesToString: function (bytes) {
  99. return decodeURIComponent(escape(Crypto.charenc.Binary.bytesToString(bytes)));
  100. }
  101. };
  102. // Binary 编码
  103. Crypto.charenc.Binary = {
  104. stringToBytes: function (str) {
  105. const bytes = [];
  106. for (let i = 0; i < str.length; i++) {
  107. bytes.push(str.charCodeAt(i) & 255);
  108. }
  109. return bytes;
  110. },
  111. bytesToString: function (bytes) {
  112. const str = [];
  113. for (let i = 0; i < bytes.length; i++) {
  114. str.push(String.fromCharCode(bytes[i]));
  115. }
  116. return str.join('');
  117. }
  118. };
  119. // 关键:导出 Crypto 供外部导入
  120. export default Crypto;