Files
cat-catch/lib/base64.js
ChuXun 53f9554f38 1
2025-10-19 20:55:27 +08:00

40 lines
1.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
class Base64 {
/**
* 将字符串编码为Base64支持UTF-8
* @param {string} str - 需要编码的原始字符串
* @returns {string} Base64编码结果
*/
static encode(str) {
// 使用TextEncoder将字符串转换为UTF-8字节数组
const encoder = new TextEncoder();
const data = encoder.encode(str);
// 将字节数组转换为二进制字符串
let binary = '';
data.forEach(byte => binary += String.fromCharCode(byte));
// 使用浏览器内置方法进行Base64编码
return btoa(binary);
}
/**
* 解码Base64字符串为原始字符串支持UTF-8
* @param {string} base64Str - Base64编码字符串
* @returns {string} 解码后的原始字符串
*/
static decode(base64Str) {
// 解码Base64得到二进制字符串
const binaryStr = atob(base64Str);
// 将二进制字符串转换为字节数组
const bytes = new Uint8Array(binaryStr.length);
for (let i = 0; i < binaryStr.length; i++) {
bytes[i] = binaryStr.charCodeAt(i);
}
// 使用TextDecoder将字节数组转换为UTF-8字符串
const decoder = new TextDecoder();
return decoder.decode(bytes);
}
}
window.Base64 = Base64;