Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- String.prototype.toBase64 = function()
- {
- var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split("");
- return this.split("").map(function(q, i, a) { return i % 3 === 0 ? a.slice(i, i + 3) : null; }).filter(function(e) { return e; }).map(function(q)
- {
- var value = q.reduce(function(p, c, i) { return p | (c.charCodeAt(0) << (2 - i) * 8); }, 0) >>> 0;
- return [18, 12, 6, 0].map(function(i) { return alphabet[(value >> i) & 0x3f]; }).join("").substring(0, q.length + 1) + "==".substring(0, 3 - q.length);
- }).join("");
- }
- String.prototype.fromBase64 = function()
- {
- var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split("").reduce(function(p, c, i) { p[c] = i; return p; }, { "=": 0});
- return (this.replace(/\s/gm, "").match(/.{1,4}/g) || []).map(function(q)
- {
- var value = q.split("").reduce(function(p, c, i) { return (p << 6) + alphabet[c]; }, 0) >>> 0;
- return [16, 8, 0].map(function(i) { return String.fromCharCode((value >>> i) & 0xff); }).join("").substr(0, q.split("=")[0].length - 1);
- }).join("");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement