Created
April 21, 2015 09:14
-
-
Save kopiro/fc2852ff26672cdc5407 to your computer and use it in GitHub Desktop.
B64 - EncoDeco
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function B64(c) { | |
if (c == 63) return 47; | |
else if (c == 62) return 43; | |
else if (c >= 52) return c - 4; | |
else if (c >= 26) return c + 71; | |
else return c + 65; | |
} | |
function B10(c) { | |
if (c == 47) return 63; | |
else if (c == 43) return 62; | |
else if (c >= 97) return c - 71; | |
else if (c >= 65) return c - 65; | |
else return c + 4; | |
} | |
String.prototype.toBase64 = function() { | |
var bits = this.split('').reduce(function(carry, char) { carry += ('00000000' + (char.charCodeAt()>>>0).toString(2)).substr(-8); return carry; }, ''); | |
var enc = ''; for (var i = 0; i < Math.ceil(bits.length / 6); i++) enc += String.fromCharCode(B64(parseInt(bits.substr(i*6, 6),2))); return enc; | |
}; | |
String.prototype.fromBase64 = function() { | |
var bits = this.split('').reduce(function(carry, char) { carry += ('00000000' + (B10(char.charCodeAt())>>>0).toString(2)).substr(-6); return carry; }, ''); | |
var enc = ''; for (var i = 0; i < Math.ceil(bits.length / 8); i++) enc += String.fromCharCode(parseInt(bits.substr(i*8, 8),2)); return enc; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment