author | Dan Fuhry <dan@fuhry.us> |
Sat, 16 Feb 2013 20:47:45 -0500 | |
changeset 7 | faf6f7941e8f |
parent 4 | 2212b2ded8bf |
permissions | -rw-r--r-- |
4
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
1 |
var keyStr = "ABCDEFGHIJKLMNOP" + |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
2 |
"QRSTUVWXYZabcdef" + |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
3 |
"ghijklmnopqrstuv" + |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
4 |
"wxyz0123456789+/" + |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
5 |
"="; |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
6 |
|
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
7 |
var Base64 = { |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
8 |
|
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
9 |
encode: function (input) { |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
10 |
input = escape(input); |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
11 |
var output = ""; |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
12 |
var chr1, chr2, chr3 = ""; |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
13 |
var enc1, enc2, enc3, enc4 = ""; |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
14 |
var i = 0; |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
15 |
|
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
16 |
do { |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
17 |
chr1 = input.charCodeAt(i++); |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
18 |
chr2 = input.charCodeAt(i++); |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
19 |
chr3 = input.charCodeAt(i++); |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
20 |
|
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
21 |
enc1 = chr1 >> 2; |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
22 |
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
23 |
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
24 |
enc4 = chr3 & 63; |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
25 |
|
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
26 |
if (isNaN(chr2)) { |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
27 |
enc3 = enc4 = 64; |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
28 |
} else if (isNaN(chr3)) { |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
29 |
enc4 = 64; |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
30 |
} |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
31 |
|
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
32 |
output = output + |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
33 |
keyStr.charAt(enc1) + |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
34 |
keyStr.charAt(enc2) + |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
35 |
keyStr.charAt(enc3) + |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
36 |
keyStr.charAt(enc4); |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
37 |
chr1 = chr2 = chr3 = ""; |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
38 |
enc1 = enc2 = enc3 = enc4 = ""; |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
39 |
} while (i < input.length); |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
40 |
|
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
41 |
return output; |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
42 |
} |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
43 |
|
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
44 |
, decode : function (input) { |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
45 |
var output = ""; |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
46 |
var chr1, chr2, chr3 = ""; |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
47 |
var enc1, enc2, enc3, enc4 = ""; |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
48 |
var i = 0; |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
49 |
|
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
50 |
// remove all characters that are not A-Z, a-z, 0-9, +, /, or = |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
51 |
var base64test = /[^A-Za-z0-9\+\/\=]/g; |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
52 |
if (base64test.exec(input)) { |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
53 |
alert("There were invalid base64 characters in the input text.\n" + |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
54 |
"Valid base64 characters are A-Z, a-z, 0-9, '+', '/',and '='\n" + |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
55 |
"Expect errors in decoding."); |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
56 |
} |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
57 |
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, ""); |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
58 |
|
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
59 |
do { |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
60 |
enc1 = keyStr.indexOf(input.charAt(i++)); |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
61 |
enc2 = keyStr.indexOf(input.charAt(i++)); |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
62 |
enc3 = keyStr.indexOf(input.charAt(i++)); |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
63 |
enc4 = keyStr.indexOf(input.charAt(i++)); |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
64 |
|
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
65 |
chr1 = (enc1 << 2) | (enc2 >> 4); |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
66 |
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
67 |
chr3 = ((enc3 & 3) << 6) | enc4; |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
68 |
|
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
69 |
output = output + String.fromCharCode(chr1); |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
70 |
|
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
71 |
if (enc3 != 64) { |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
72 |
output = output + String.fromCharCode(chr2); |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
73 |
} |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
74 |
if (enc4 != 64) { |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
75 |
output = output + String.fromCharCode(chr3); |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
76 |
} |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
77 |
|
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
78 |
chr1 = chr2 = chr3 = ""; |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
79 |
enc1 = enc2 = enc3 = enc4 = ""; |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
80 |
|
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
81 |
} while (i < input.length); |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
82 |
|
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
83 |
return unescape(output); |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
84 |
} |
2212b2ded8bf
Added OpenSSH public key support in LDAP
Dan Fuhry <dan@fuhry.us>
parents:
diff
changeset
|
85 |
} |