public
Last active

javascript base64 encode/decode

  • Download Gist
gistfile1.js
JavaScript
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
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<script type="text/javascript">
 
var nativeAtob = window.atob;
var nativeBtoa = window.btoa;
 
// http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#atob
(function (global) {
"use strict";
 
var a64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
 
var a256 = "";
var r64 = [];
var i = -1;
while (++i < 256) {
a256 += String.fromCharCode(i);
}
var counter = 63;
i = -1;
while (counter >= 0) {
var c = String.fromCharCode(++i);
r64[i] = a64.indexOf(c);
if (r64[i] !== -1) {
--counter;
}
}
 
function code(s, discard, alpha, beta, w1, w2) {
var buffer = 0;
var i = -1;
var length = s.length;
var result = '';
var bitsInBuffer = 0;
while (++i < length) {
var c = s.charCodeAt(i);
c = (alpha === null ? (c < 256 ? c : -1) : (c < alpha.length ? alpha[c] : -1));
if (c === -1) {
throw new RangeError();
}
buffer = (buffer << w1) + c;
bitsInBuffer += w1;
while (bitsInBuffer >= w2) {
bitsInBuffer -= w2;
var tmp = buffer >> bitsInBuffer;
result += beta.charAt(tmp);
buffer ^= tmp << bitsInBuffer;
}
}
if (!discard && bitsInBuffer > 0) {
result += beta.charAt(buffer << (w2 - bitsInBuffer));
}
return result;
}
 
global.btoa = function (s) {
s = String(s);
s = code(s, false, null, a64, 8, 6);
return s + "====".slice((s.length % 4) || 4);
};
 
global.atob = function (s) {
s = String(s);
var length = s.length;
var k = -1;
var result = "";
var i = 0;
while (++k < length + 1) {
if (k === length || s.charAt(k) === "=") {
var p = s.slice(i, k);
i = k + 1;
if (p.length % 4 === 1) {
throw new RangeError();
}
result += code(p, true, r64, a256, 6, 8);
}
}
return result;
};
 
}(this));
 
var decodeTests = [
['YW55IGNhcm5hbCBwbGVhc3VyZS4=', 'any carnal pleasure.'],
['YW55IGNhcm5hbCBwbGVhc3VyZQ==', 'any carnal pleasure'],
['YW55IGNhcm5hbCBwbGVhc3Vy', 'any carnal pleasur'],
[Array(1e4).join('YW55IGNhcm5hbCBwbGVhc3Vy'), Array(1e4).join('any carnal pleasur')], // ~24 kb input
['YW55IGNhcm5hbCBwbGVhc3U=', 'any carnal pleasu'],
['YW55IGNhcm5hbCBwbGVhcw==', 'any carnal pleas'],
['YW55IGNhcm5hbCBwbGVhcw', 'any carnal pleas'],
['\uaaaa', null],
['YQ', 'a'],
['YR', 'a'],
['', ''],
['YQA=', 'a\u0000'],
['YW55IGNhcm5hbCBwbGVhcw==YW55IGNhcm5hbCBwbGVhc3VyZS4=', 'any carnal pleasany carnal pleasure.'],
['YW55IGNhcm5hbCBwbGVhcw==YW55IGNhcm5hbCBwbGVhc3VyZS4', 'any carnal pleasany carnal pleasure.']
];
 
var encodeTests = [
['', ''],
['\x00', 'AA=='],
['\xff\xff', '//8='],
['any carnal pleasure.', 'YW55IGNhcm5hbCBwbGVhc3VyZS4='],
['any carnal pleasure', 'YW55IGNhcm5hbCBwbGVhc3VyZQ=='],
['any carnal pleasur', 'YW55IGNhcm5hbCBwbGVhc3Vy'],
[Array(1e4).join('any carnal pleasur'), Array(1e4).join('YW55IGNhcm5hbCBwbGVhc3Vy')], // ~24 kb output
['any carnal pleasu', 'YW55IGNhcm5hbCBwbGVhc3U='],
['any carnal pleas', 'YW55IGNhcm5hbCBwbGVhcw=='],
['a', 'YQ=='],
['', ''],
['\uaaaa', null],
['a\u0000', 'YQA=']
];
 
if (!window.console) {
window.console = {
log: function (x) {
document.body.appendChild(document.createTextNode(x));
document.body.appendChild(document.createElement('br'));
}
};
}
function runTests(tests, func, name) {
var i, r, t = +new Date();
for (i = 0; i < tests.length; i++) {
try {
r = func(tests[i][0]);
} catch (e) {
r = null;
}
if (r !== tests[i][1]) {
console.log(i + ': ' + name + '(' + tests[i][0] + ') === ' + r + '(expected: ' + tests[i][1] + ')');
}
}
t = +new Date() - t;
console.log(name + ' - ' + t + 'ms');
}
 
setTimeout(function () {
if (window.atob) {
runTests(decodeTests, nativeAtob, 'window.atob - native');
}
runTests(decodeTests, window.atob, 'window.atob');
if (window.btoa) {
runTests(encodeTests, nativeBtoa, 'window.btoa - native');
}
runTests(encodeTests, window.btoa, 'window.btoa');
console.log('finished');
}, 1);
 
</script>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.