Permalink
Browse files

lib/base64: added 'nonsafe' base64

	modified:   lib/base64.js
  • Loading branch information...
1 parent 2075a71 commit d3981f8683cb13713f8dce2aa38cb34744aea1ec @jeromeetienne committed Sep 6, 2010
Showing with 27 additions and 14 deletions.
  1. +27 −14 lib/base64.js
View
@@ -18,18 +18,18 @@
var base64 = {
// private property
- _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=",
+ _keyStr_defl : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-/=",
+ _keyStr_safe : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=",
// public method for encoding
- encode_safe : function (input) {
+ _encode_base : function (input, keyStr) {
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
input = base64._utf8_encode(input);
while (i < input.length) {
-
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
@@ -46,16 +46,15 @@ var base64 = {
}
output = output +
- base64._keyStr.charAt(enc1) + base64._keyStr.charAt(enc2) +
- base64._keyStr.charAt(enc3) + base64._keyStr.charAt(enc4);
-
+ keyStr.charAt(enc1) + keyStr.charAt(enc2) +
+ keyStr.charAt(enc3) + keyStr.charAt(enc4);
}
return output;
},
// public method for decoding
- decode_safe : function (input) {
+ _decode_base : function (input, keyStr) {
var output = "";
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
@@ -65,10 +64,10 @@ var base64 = {
while (i < input.length) {
- enc1 = base64._keyStr.indexOf(input.charAt(i++));
- enc2 = base64._keyStr.indexOf(input.charAt(i++));
- enc3 = base64._keyStr.indexOf(input.charAt(i++));
- enc4 = base64._keyStr.indexOf(input.charAt(i++));
+ enc1 = keyStr.indexOf(input.charAt(i++));
+ enc2 = keyStr.indexOf(input.charAt(i++));
+ enc3 = keyStr.indexOf(input.charAt(i++));
+ enc4 = keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
@@ -82,14 +81,28 @@ var base64 = {
if (enc4 != 64) {
output = output + String.fromCharCode(chr3);
}
-
}
-
output = base64._utf8_decode(output);
-
return output;
+ },
+ // public method for encoding
+ encode : function (input) {
+ return base64._encode_base(input, base64._keyStr_defl);
},
+ // public method for decoding
+ decode : function (input) {
+ return base64._decode_base(input, base64._keyStr_defl);
+ },
+ // public method for encoding
+ encode_safe : function (input) {
+ return base64._encode_base(input, base64._keyStr_safe);
+ },
+ // public method for decoding
+ decode_safe : function (input) {
+ return base64._decode_base(input, base64._keyStr_safe);
+ },
+
// private method for UTF-8 encoding
_utf8_encode : function (string) {

0 comments on commit d3981f8

Please sign in to comment.