-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
379 lines (346 loc) · 13.5 KB
/
index.html
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
---
layout: empty
title: Home
---
<script src="/assets/base32h/base32h.js"></script>
<script>
// @license magnet:?xt=urn:btih:b8999bbaf509c08d127678643c515b9ab0836bae&dn=ISC.txt ISC
console.log(base32h.encodeBin(base32h.decodeBin('hello world')));
var decBase = 10;
var decBin = false;
function switchDecBase() {
console.log("Switching...");
var baseField = document.getElementById("demo-base");
var decField = document.getElementById("demo-decoded");
decBase = baseField.options[baseField.selectedIndex].value;
switch (decBase) {
case "binstr":
decField.setAttribute("placeholder",
"Decoded (What in tarnation???)");
decField.setAttribute("type", "text");
decBin = true;
break;
case "bin64":
decField.setAttribute("placeholder",
"Decoded (V2hhdCBpbiB0YXJuYXRpb24/Pz8=)");
decField.setAttribute("type", "text");
decBin = true;
break;
case "bin16":
decField.setAttribute("placeholder",
"Decoded ()");
decField.setAttribute("type", "text");
decBin = true;
break;
case "16":
decField.setAttribute("placeholder", "Decoded (1234CAFE)");
decField.setAttribute("type", "text");
decBin = false;
break;
default:
decField.setAttribute("placeholder", "Decoded (1234567890)");
decField.setAttribute("type", "number");
decBin = false;
}
}
function encode() {
if (decBin) {
encodeBin();
} else {
encodeNum();
}
}
function encodeBin() {
console.log("Encoding binary...");
clearErrors();
var encField = document.getElementById("demo-encoded");
var decField = document.getElementById("demo-decoded");
var decoded = decField.value;
if (decBase == "bin64") {
decoded = atob(decoded);
}
if (decBase == "bin16") {
decoded = hex2bin(decoded);
}
if (decoded.length % 5 > 0) {
decField.classList.add("is-warning");
var msg = document.createElement("p");
msg.id = "demo-decoded-message";
msg.classList.add("help");
msg.classList.add("is-warning");
decField.parentElement.parentElement.appendChild(msg);
msg.textContent = "The number of bytes in the data you're \
decoding ain't cleanly divisible by 5, so the encoded value includes \
nulls for padding, which will show up if you decode the below encoded \
data.";
}
encField.value = base32h.encodeBin(decoded);
}
function encodeNum() {
console.log("Encoding number...");
clearErrors();
var encField = document.getElementById("demo-encoded");
var decField = document.getElementById("demo-decoded");
var decoded = parseInt(decField.value, parseInt(decBase));
if (isNaN(decoded)) {
decField.classList.add("is-danger");
var msg = document.createElement("p");
msg.id = "demo-decoded-message";
msg.classList.add("help");
msg.classList.add("is-danger");
decField.parentElement.parentElement.appendChild(msg);
msg.textContent = "Not a valid integer.";
return;
}
if (decoded > Number.MAX_SAFE_INTEGER) {
decField.classList.add("is-warning");
var msg = document.createElement("p");
msg.id = "demo-decoded-message";
msg.classList.add("help");
msg.classList.add("is-warning");
decField.parentElement.parentElement.appendChild(msg);
msg.textContent = "The number you're encoding is bigger than \
what Javascript can accurately represent, so the below value is probably \
incorrect.";
}
encField.value = base32h.encode(decoded);
}
function decode() {
if (decBin) {
decodeBin();
} else {
decodeNum();
}
}
function decodeBin() {
console.log("Decoding binary...");
clearErrors();
var encField = document.getElementById("demo-encoded");
var decField = document.getElementById("demo-decoded");
var decoded = base32h.decodeBin(encField.value);
var encoded = ""
for (var i = 0; i < decoded.length; i++) {
encoded += String.fromCharCode(decoded[i]);
}
if (decBase == "bin64") {
encoded = btoa(encoded);
}
if (decBase == "bin16") {
encoded = bin2hex(encoded);
}
decField.value = encoded;
}
function decodeNum() {
console.log("Decoding number...");
clearErrors();
var encField = document.getElementById("demo-encoded");
var decField = document.getElementById("demo-decoded");
var decoded = base32h.decode(encField.value);
if (decoded > Number.MAX_SAFE_INTEGER) {
encField.classList.add("is-warning");
var msg = document.createElement("p");
msg.id = "demo-encoded-message";
msg.classList.add("help");
msg.classList.add("is-warning");
encField.parentElement.parentElement.appendChild(msg);
msg.textContent = "The number you're decoding is bigger than \
what Javascript can accurately represent, so the above value is probably \
incorrect.";
}
decField.value = decoded.toString(parseInt(decBase));
}
function nuke() {
console.log("Clearing...");
clearErrors();
var encField = document.getElementById("demo-encoded");
var decField = document.getElementById("demo-decoded");
encField.value = "";
decField.value = "";
}
function clearErrors() {
var encField = document.getElementById("demo-encoded");
var decField = document.getElementById("demo-decoded");
var encWrap = encField.parentElement.parentElement;
var decWrap = decField.parentElement.parentElement;
var encMsg = document.getElementById("demo-encoded-message");
var decMsg = document.getElementById("demo-decoded-message");
encField.classList.remove("is-danger");
decField.classList.remove("is-danger");
encField.classList.remove("is-warning");
decField.classList.remove("is-warning");
if (encMsg) {encWrap.removeChild(encMsg);}
if (decMsg) {decWrap.removeChild(decMsg);}
}
function bin2hex(bin) {
var hex = "";
for (var i = 0; i < bin.length; i++) {
var byte = bin.charCodeAt(i).toString(16);
if (byte.length == 1) {
byte = "0" + byte;
}
hex += byte;
}
return hex.toUpperCase();
}
function hex2bin(hex) {
var hexDigits = [
'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
]
hex = hex.toUpperCase();
hex = hex.split('').filter(h => hexDigits.includes(h)).join('');
if (hex.length % 2 > 0) {
hex = "0" + hex;
}
var bin = "";
for (var i = 0; i < hex.length; i += 2) {
bin += String.fromCharCode(parseInt(hex.substr(i,2), 16));
}
return bin;
}
// @license-end
</script>
<header>
<h1>Base32H</h1>
<p><em>n.</em> a human-friendly duotrigesimal number system</p>
</header>
<main class="tile is-ancestor">
<div class="tile is-vertical is-parent is-main">
<section class="tile is-child box">
<h2 class="title">Try it out!</h2>
<div class="field has-addons">
<div class="control is-expanded">
<input id="demo-decoded"
class="input"
type="number"
onkeydown="if (event.key == 'Enter') {encode()}"
placeholder="Decoded (1234567890)"/>
</div>
<div class="control">
<div class="select">
<select id="demo-base" onchange="switchDecBase()">
<option value="10">Decimal</option>
<option value="16">Hexadecimal</option>
<option value="bin16">Binary (base16)</option>
<option value="bin64">Binary (base64)</option>
<option value="binstr">Binary (string)</option>
</select>
</div>
</div>
</div>
<div class="field is-grouped is-grouped-centered">
<div class="control">
<a class="button is-primary"
onclick="encode()"
onkeydown="encode()">
{% include feather.html icon="arrow-down" %}
Encode
</a>
</div>
<div class="control">
<a class="button is-danger"
onclick="nuke()"
onkeydown="nuke()">
{% include feather.html icon="refresh-cw" %}
Clear
</a>
</div>
<div class="control">
<a class="button is-primary"
onclick="decode()"
onkeydown="decode()">
{% include feather.html icon="arrow-up" %}
Decode
</a>
</div>
</div>
<div class="field">
<div class="control is-expanded">
<input id="demo-encoded"
class="input"
type="text"
onkeydown="if (event.key == 'Enter') {decode()}"
placeholder="Encoded (HELL0W0RLD)"/>
</div>
</div>
</section>
<section class="tile is-child box content">
<h2 class="title">Human-friendly? Duotrigesi-what?</h2>
<p>
32 digits, picked for the right balance of legibility,
audibility, and information density. 0 or O? 1 or I?
5 or S? Doesn't matter; they're equivalent in
Base32H. All the digits can be found on your typical
keyboard, and are totally safe for URLs, filenames,
and anything else that might not like special
characters.
</p>
</section>
<section class="tile is-child box content">
<h2 class="title">Rationale</h2>
<p>
Base32H is handy for <a href="/usecases">all sorts of use
cases</a>, and <a href="/comparisons">stacks up pretty
strong</a> against other number systems:
</p>
<ul>
<li>
More compact than hexadecimal (a 40-bit number
needs only 8 Base32H digits v. 10 hexadecimal
digits)
</li>
<li>
No case-sensitive letters or punctuation like in
Base64 (you can use Base32H in filenames without
worry)
</li>
<li>
No un-decodable letters like in other base-32
systems (any letter or numeral on a US keyboard
will decode to a Base32H digit)
</li>
<li>
Easier to encode byte-aligned data than with
non-power-of-two-radix number systems (no wasted bits)
</li>
</ul>
<p>
These traits make it an excellent choice for any
situation where people need to reliably read numeric
identifiers -- asset tags, product keys, public key
fingerprints, user IDs, etc. -- and especially if that
situation entails reading an identifier aloud to
someone else.
</p>
</section>
<section class="tile is-child box content">
<h2 class="title">Implementations</h2>
<p>
<a href="/implementations">Three official / reference
implementations</a> and counting (all free software
under the <a href="/COPYING.txt">ISC License</a>),
plus multiple community-contributed implementations.
</p>
</section>
</div>
<div class="tile is-parent">
<section class="tile is-child box">
<h2 class="title">Digits</h2>
{% assign canonical = "0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F|G|H|J|K|L|M|N|P|Q|R|T|V|W|X|Y|Z" | split: "|" %}
{% assign aliases = "O o|I i||||S s|||||a|b|c|d|e|f|g|h|j|k|l|m|n|p|q|r|t|v U u|w|x|y|z" | split: "|" %}
<table class="table">
<tr>
<th>Value</th>
<th>Canonical</th>
<th>Aliases</th>
</tr>
{% for i in (0..31) %}
<tr>
<td>{{i}}</td>
<td>{{canonical[i]}}</td>
<td>{{aliases[i]}}</td>
</tr>
{% endfor %}
</table>
</section>
</div>
</main>