Skip to content

Instantly share code, notes, and snippets.

@anniethiessen
Last active June 11, 2021 20:26
Show Gist options
  • Select an option

  • Save anniethiessen/bcb8ca458e324b91f0c1d4cddde51999 to your computer and use it in GitHub Desktop.

Select an option

Save anniethiessen/bcb8ca458e324b91f0c1d4cddde51999 to your computer and use it in GitHub Desktop.
Manual b64 encoder/decoder
// encode
var s = ''
var c, d, e, f, g, h, i, j, o, b = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", k = 0, l = 0, m = "", n = [];
do c = s.charCodeAt(k++), d = s.charCodeAt(k++), e = s.charCodeAt(k++), j = c << 16 | d << 8 | e, f = 63 & j >> 18, g = 63 & j >> 12, h = 63 & j >> 6, i = 63 & j, n[l++] = b.charAt(f) + b.charAt(g) + b.charAt(h) + b.charAt(i); while (k < s.length);
m = n.join(""), o = s.length % 3;
s = (o ? m.slice(0, o - 3) :m) + "===".slice(o || 3);
// decode
var s = ''
var b, c, d, e = {}, f = 0, g = 0, h = "", i = String.fromCharCode, j = s.length;
for (b = 0; 64 > b; b++) e["ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(b)] = b;
for (c = 0; j > c; c++) for (b = e[s.charAt(c)], f = (f << 6) + b, g += 6; g >= 8; ) ((d = 255 & f >>> (g -= 8)) || j - 2 > c) && (h += i(d));
s = h;
function b2a(s) {
var c, d, e, f, g, h, i, j, o;
var b = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var k = 0, l = 0;
var m = "";
var n = [];
do {
c = s.charCodeAt(k++), d = s.charCodeAt(k++), e = s.charCodeAt(k++);
j = c << 16 | d << 8 | e;
f = 63 & j >> 18;
g = 63 & j >> 12;
h = 63 & j >> 6;
i = 63 & j;
n[l++] = b.charAt(f) + b.charAt(g) + b.charAt(h) + b.charAt(i);
} while (k < s.length);
m = n.join("");
o = s.length % 3;
return (o ? m.slice(0, o - 3) :m) + "===".slice(o || 3);
}
function a2b(s) {
var b, c, d;
var e = {};
var f = 0, g = 0;
var h = "";
var i = String.fromCharCode, j = s.length;
for (b = 0; 64 > b; b++) {
e["ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(b)] = b;
}
for (c = 0; j > c; c++) {
for (b = e[s.charAt(c)], f = (f << 6) + b, g += 6; g >= 8;) {
((d = 255 & f >>> (g -= 8)) || j - 2 > c) && (h += i(d));
}
}
return h;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment