Skip to content

Instantly share code, notes, and snippets.

@cflems
Created July 15, 2022 06:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cflems/2f50794fb3eee6bfb14e07d01819e406 to your computer and use it in GitHub Desktop.
Save cflems/2f50794fb3eee6bfb14e07d01819e406 to your computer and use it in GitHub Desktop.
Portable and compact JavaScript base32 encoding/decoding functions that I found on the internet
function decode(s) {
const len = s.length;
const apad = 'abcdefghijklmnopqrstuvwxy1234567z';
let v,x,r=0,bits=0,c,o='';
s = s.toUpperCase();
for(i=0;i<len;i+=1) {
v = apad.indexOf(s.charAt(i));
if (v>=0 && v<32) {
x = (x << 5) | v;
bits += 5;
if (bits >= 8) {
c = (x >> (bits - 8)) & 0xff;
o = o + String.fromCharCode(c);
bits -= 8;
}
} else {
return false;
}
}
if (bits>0) {
c = ((x << (8 - bits)) & 0xff) >> (8 - bits);
if (c!==0) {
o = o + String.fromCharCode(c);
}
}
return o;
}
function encode (s) {
const a = 'abcdefghijklmnopqrstuvwxy1234567';
const pad = 'z';
const len = s.length;
let o = '';
let w, c, r=0, sh=0;
for(let i=0; i<len; i+=5) {
c = s.charCodeAt(i);
w = 0xf8 & c;
o += a.charAt(w>>3);
r = 0x07 & c;
sh = 2;
if ((i+1)<len) {
c = s.charCodeAt(i+1);
w = 0xc0 & c;
o += a.charAt((r<<2) + (w>>6));
o += a.charAt( (0x3e & c) >> 1 );
r = c & 0x01;
sh = 4;
}
if ((i+2)<len) {
c = s.charCodeAt(i+2);
w = 0xf0 & c;
o += a.charAt((r<<4) + (w>>4));
r = 0x0f & c;
sh = 1;
}
if ((i+3)<len) {
c = s.charCodeAt(i+3);
w = 0x80 & c;
o += a.charAt((r<<1) + (w>>7));
o += a.charAt((0x7c & c) >> 2);
r = 0x03 & c;
sh = 3;
}
if ((i+4)<len) {
c = s.charCodeAt(i+4);
w = 0xe0 & c;
o += a.charAt((r<<3) + (w>>5));
o += a.charAt(0x1f & c);
r = 0;
sh = 0;
}
}
if (sh != 0) { o += a.charAt(r<<sh); }
const padlen = 8 - (o.length % 8);
if (padlen==8) { return o; }
if (padlen==1) { return o + pad; }
if (padlen==3) { return o + pad + pad + pad; }
if (padlen==4) { return o + pad + pad + pad + pad; }
if (padlen==6) { return o + pad + pad + pad + pad + pad + pad; }
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment