// 移除自执行函数和全局变量挂载逻辑,改为直接定义并导出 const base64map = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; // 直接定义 Crypto 对象(不再依赖 window 或 exports) const Crypto = {}; // Crypto 工具方法 Crypto.util = { rotl: function (n, b) { return (n << b) | (n >>> (32 - b)); }, rotr: function (n, b) { return (n << (32 - b)) | (n >>> b); }, endian: function (n) { if (n.constructor === Number) { return (Crypto.util.rotl(n, 8) & 16711935) | (Crypto.util.rotl(n, 24) & 4278255360); } for (let i = 0; i < n.length; i++) { n[i] = Crypto.util.endian(n[i]); } return n; }, randomBytes: function (n) { const bytes = []; for (; n > 0; n--) { bytes.push(Math.floor(Math.random() * 256)); } return bytes; }, bytesToWords: function (bytes) { const words = []; for (let i = 0, b = 0; i < bytes.length; i++, b += 8) { words[b >>> 5] |= (bytes[i] & 255) << (24 - (b % 32)); } return words; }, wordsToBytes: function (words) { const bytes = []; for (let b = 0; b < words.length * 32; b += 8) { bytes.push((words[b >>> 5] >>> (24 - (b % 32))) & 255); } return bytes; }, bytesToHex: function (bytes) { const hex = []; for (let i = 0; i < bytes.length; i++) { hex.push((bytes[i] >>> 4).toString(16)); hex.push((bytes[i] & 15).toString(16)); } return hex.join(''); }, hexToBytes: function (hex) { const bytes = []; for (let c = 0; c < hex.length; c += 2) { bytes.push(parseInt(hex.substr(c, 2), 16)); } return bytes; }, bytesToBase64: function (bytes) { if (typeof btoa === 'function') { return btoa(Crypto.charenc.Binary.bytesToString(bytes)); } const base64 = []; for (let i = 0; i < bytes.length; i += 3) { const triplet = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2]; for (let j = 0; j < 4; j++) { if (i * 8 + j * 6 <= bytes.length * 8) { base64.push(base64map.charAt((triplet >>> (6 * (3 - j))) & 63)); } else { base64.push('='); } } } return base64.join(''); }, base64ToBytes: function (base64) { if (typeof atob === 'function') { return Crypto.charenc.Binary.stringToBytes(atob(base64)); } base64 = base64.replace(/[^A-Z0-9+\/]/gi, ''); const bytes = []; for (let i = 0, imod4 = 0; i < base64.length; imod4 = ++i % 4) { if (imod4 === 0) continue; bytes.push( ((base64map.indexOf(base64.charAt(i - 1)) & (Math.pow(2, -2 * imod4 + 8) - 1)) << (imod4 * 2)) | (base64map.indexOf(base64.charAt(i)) >>> (6 - imod4 * 2)) ); } return bytes; } }; // 字符编码模块 Crypto.charenc = {}; // UTF-8 编码 Crypto.charenc.UTF8 = { stringToBytes: function (str) { return Crypto.charenc.Binary.stringToBytes(unescape(encodeURIComponent(str))); }, bytesToString: function (bytes) { return decodeURIComponent(escape(Crypto.charenc.Binary.bytesToString(bytes))); } }; // Binary 编码 Crypto.charenc.Binary = { stringToBytes: function (str) { const bytes = []; for (let i = 0; i < str.length; i++) { bytes.push(str.charCodeAt(i) & 255); } return bytes; }, bytesToString: function (bytes) { const str = []; for (let i = 0; i < bytes.length; i++) { str.push(String.fromCharCode(bytes[i])); } return str.join(''); } }; // 关键:导出 Crypto 供外部导入 export default Crypto;