-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBase32EncoderAndDecoder.html
More file actions
238 lines (201 loc) · 6.73 KB
/
Copy pathBase32EncoderAndDecoder.html
File metadata and controls
238 lines (201 loc) · 6.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Base32编码/解码工具 - JustHTML 只需网页</title>
<script>
/**
* 动态加载(强制去除缓存) CSS 的函数
* @param {string} src - 要加载的 CSS 的 src 链接
* @returns {Promise} 返回一个 Promise 对象,用于处理异步加载
*/
function loadStyle(src) {
return new Promise((resolve, reject) => {
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = src + '?v=' + new Date().getTime(); // 使用时间戳避免缓存
link.async = true;
link.onload = resolve;
link.onerror = reject;
document.head.appendChild(link);
});
}
/**
* 动态加载(强制去除缓存) JS 的函数
* @param {string} src - 要加载的 JS 的 src 链接
* @returns {Promise} 返回一个 Promise 对象,用于处理异步加载
*/
function loadScript(src) {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = src + '?v=' + new Date().getTime(); // 使用时间戳避免缓存
script.async = true;
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
}
</script>
<script>
loadStyle('../assets/style.css')
let scriptArray = [
"../assets/AddHeader_Tools.js",
"../assets/AddFooter.js",
"../assets/AddIco.js",
"../assets/AutoRun.js",
]
scriptArray.forEach(script => {
loadScript(script);
});
</script>
<script src="../package/Vue/code/vue.global.prod.js"></script>
<script type="module" src="../package/Sober/code/main.js"></script>
</head>
<body>
<div id="app">
<div class="container">
<a href="../index.html" class="back-link">← 返回首页</a>
<div class="tool-section">
<h2>Base32编码/解码工具</h2>
<p>将文本转换为Base32编码或将Base32编码解码为原文本</p>
<div>
<textarea
v-model="inputText"
placeholder="在此输入要编码或解码的文本..."
rows="8"
></textarea>
<button @click="encodeBase32">Base32编码</button>
<button @click="decodeBase32">Base32解码</button>
<button @click="clearText">清空</button>
<div class="result" v-if="result">
<p><strong>结果:</strong></p>
<pre>{{ result }}</pre>
</div>
<div class="result" v-if="error" style="color: #ff6b6b;">
<p><strong>错误:</strong></p>
<p>{{ error }}</p>
</div>
</div>
</div>
</div>
</div>
<script>
const { createApp, ref } = Vue;
createApp({
setup() {
const inputText = ref('');
const result = ref('');
const error = ref('');
// Base32字符表
const BASE32_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
// Base32编码函数
const encodeBase32 = () => {
error.value = '';
try {
result.value = base32Encode(inputText.value);
} catch (e) {
error.value = `编码失败: ${e.message}`;
}
};
// Base32解码函数
const decodeBase32 = () => {
error.value = '';
try {
result.value = base32Decode(inputText.value);
} catch (e) {
error.value = `解码失败: ${e.message}`;
}
};
// Base32编码实现
function base32Encode(str) {
if (str === "") return "";
// 将字符串转换为字节数组
const bytes = new TextEncoder().encode(str);
let result = "";
let bits = 0;
let bitCount = 0;
for (let i = 0; i < bytes.length; i++) {
bits = (bits << 8) | bytes[i];
bitCount += 8;
// 每5个字节取一次数据
while (bitCount >= 5) {
const index = (bits >>> (bitCount - 5)) & 0x1F;
result += BASE32_CHARS[index];
bitCount -= 5;
bits &= (1 << bitCount) - 1;
}
}
// 处理余下的位
if (bitCount > 0) {
const index = (bits << (5 - bitCount)) & 0x1F;
result += BASE32_CHARS[index];
// 添加填充字符
const paddingCount = (8 - result.length % 8) % 8;
for (let i = 0; i < paddingCount; i++) {
result += '=';
}
}
return result;
}
// Base32解码实现
function base32Decode(str) {
if (str === "") return "";
// 移除可能的空白字符
str = str.replace(/\s/g, '');
// 验证Base32字符
if (/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=]/.test(str)) {
throw new Error("输入包含无效的Base32字符");
}
// 移除填充字符
const paddedStr = str.replace(/=/g, '');
const paddingCount = str.length - paddedStr.length;
if (paddingCount > 7) {
throw new Error("无效的填充字符数");
}
const bytes = [];
let bits = 0;
let bitCount = 0;
for (let i = 0; i < paddedStr.length; i++) {
const c = paddedStr[i];
const index = BASE32_CHARS.indexOf(c);
if (index === -1) {
throw new Error(`未知的Base32字符: ${c}`);
}
bits = (bits << 5) | index;
bitCount += 5;
if (bitCount >= 8) {
bytes.push((bits >>> (bitCount - 8)) & 0xFF);
bitCount -= 8;
bits &= (1 << bitCount) - 1;
}
}
// 解码后不应该有多余的位
if (bits & ((1 << bitCount) - 1)) {
throw new Error("无效的Base32编码");
}
// 尝试将字节数组转换为字符串
try {
return new TextDecoder().decode(new Uint8Array(bytes));
} catch (e) {
throw new Error(`解码错误: ${e.message}`);
}
}
const clearText = () => {
inputText.value = '';
result.value = '';
error.value = '';
};
return {
inputText,
result,
error,
encodeBase32,
decodeBase32,
clearText
};
}
}).mount('#app');
</script>
</body>
</html>