Skip to content

Instantly share code, notes, and snippets.

@sergeevabc
Last active September 11, 2021 04:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sergeevabc/f20d49ee3bda647aed39 to your computer and use it in GitHub Desktop.
Save sergeevabc/f20d49ee3bda647aed39 to your computer and use it in GitHub Desktop.
Base32 Crockford's edition https://github.com/agnoster/base32-js 0123456789abcdefghjkmnpqrtuvwxyz
(function () {
var alphabet = '0123456789abcdefghjkmnpqrtuvwxyz';
var alias = {
o: 0,
i: 1,
l: 1,
s: 5
};
var lookup = function () {
var table = {};
for (var i = 0; i < alphabet.length; i++) {
table[alphabet[i]] = i;
}
for (var key in alias) {
if (!alias.hasOwnProperty(key))
continue;
table[key] = table['' + alias[key]];
}
lookup = function () {
return table;
};
return table;
};
function Encoder() {
var skip = 0;
var bits = 0;
this.output = '';
this.readByte = function (byte) {
if (typeof byte == 'string')
byte = byte.charCodeAt(0);
if (skip < 0) {
bits |= byte >> -skip;
} else {
bits = byte << skip & 248;
}
if (skip > 3) {
skip -= 8;
return 1;
}
if (skip < 4) {
this.output += alphabet[bits >> 3];
skip += 5;
}
return 0;
};
this.finish = function (check) {
var output = this.output + (skip < 0 ? alphabet[bits >> 3] : '') + (check ? '$' : '');
this.output = '';
return output;
};
}
Encoder.prototype.update = function (input, flush) {
for (var i = 0; i < input.length;) {
i += this.readByte(input[i]);
}
var output = this.output;
this.output = '';
if (flush) {
output += this.finish();
}
return output;
};
function Decoder() {
var skip = 0;
var byte = 0;
this.output = '';
this.readChar = function (char) {
if (typeof char != 'string') {
if (typeof char == 'number') {
char = String.fromCharCode(char);
}
}
char = char.toLowerCase();
var val = lookup()[char];
if (typeof val == 'undefined') {
return;
}
val <<= 3;
byte |= val >>> skip;
skip += 5;
if (skip >= 8) {
this.output += String.fromCharCode(byte);
skip -= 8;
if (skip > 0)
byte = val << 5 - skip & 255;
else
byte = 0;
}
};
this.finish = function (check) {
var output = this.output + (skip < 0 ? alphabet[bits >> 3] : '') + (check ? '$' : '');
this.output = '';
return output;
};
}
Decoder.prototype.update = function (input, flush) {
for (var i = 0; i < input.length; i++) {
this.readChar(input[i]);
}
var output = this.output;
this.output = '';
if (flush) {
output += this.finish();
}
return output;
};
function encode(input) {
var encoder = new Encoder();
var output = encoder.update(input, true);
return output;
}
function decode(input) {
var decoder = new Decoder();
var output = decoder.update(input, true);
return output;
}
var base32 = {
Decoder: Decoder,
Encoder: Encoder,
encode: encode,
decode: decode,
};
if (typeof window !== 'undefined') {
window.base32 = base32;
}
if (typeof module !== 'undefined' && module.exports) {
module.exports = base32;
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment