Skip to content

Commit 7e2fdbd

Browse files
author
Nicholas C. Zakas
committed
Base 64 encoding
1 parent 543ee9f commit 7e2fdbd

File tree

2 files changed

+174
-0
lines changed

2 files changed

+174
-0
lines changed

encodings/base64/base64.htm

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<html>
2+
<head>
3+
<title>Base64 Tests</title>
4+
<!-- Combo-handled YUI CSS files: -->
5+
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/combo?2.7.0/build/logger/assets/logger.css&2.7.0/build/yuitest/assets/testlogger.css">
6+
<!-- Combo-handled YUI JS files: -->
7+
<script type="text/javascript" src="http://yui.yahooapis.com/combo?2.7.0/build/yahoo-dom-event/yahoo-dom-event.js&2.7.0/build/logger/logger-min.js&2.7.0/build/yuitest/yuitest-min.js"></script>
8+
<script type="text/javascript" src="base64.js"></script>
9+
10+
11+
</head>
12+
<body>
13+
<h1>Doubly Linked List Tests</h1>
14+
<script type="text/javascript">
15+
16+
YAHOO.namespace("test");
17+
18+
YAHOO.test.Base64 = (function(){
19+
20+
var assert = YAHOO.util.Assert;
21+
22+
//-------------------------------------------------------------------------
23+
// Base Test Suite
24+
//-------------------------------------------------------------------------
25+
26+
var suite = new YAHOO.tool.TestSuite("Base64 Tests");
27+
28+
//-------------------------------------------------------------------------
29+
// Test Case for adding
30+
//-------------------------------------------------------------------------
31+
32+
suite.add(new YAHOO.tool.TestCase({
33+
34+
name : "Base 64 Encoding Tests",
35+
36+
//---------------------------------------------------------------------
37+
// Tests
38+
//---------------------------------------------------------------------
39+
40+
testMan: function(){
41+
var result = base64Encode("Man");
42+
assert.areEqual("TWFu", result);
43+
},
44+
45+
testHelloWorld: function(){
46+
var result = base64Encode("Hello world");
47+
assert.areEqual("SGVsbG8gd29ybGQ=", result);
48+
},
49+
50+
testPhrase: function(){
51+
var expected = "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=";
52+
var result = base64Encode("Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.");
53+
assert.areEqual(expected, result);
54+
}
55+
}));
56+
57+
58+
//return it
59+
return suite;
60+
61+
})();
62+
63+
(function (){
64+
//create the logger
65+
var logger = new YAHOO.tool.TestLogger();
66+
67+
//add the tests
68+
YAHOO.tool.TestRunner.add(YAHOO.test.Base64);
69+
YAHOO.tool.TestRunner.run();
70+
71+
})();
72+
73+
74+
</script>
75+
</body>
76+
</html>

encodings/base64/base64.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Base 64 implementation in JavaScript
3+
* Copyright (c) 2009 Nicholas C. Zakas. All rights reserved.
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
/**
25+
* Base64-encodes a string of text.
26+
* @param {String} text The text to encode
27+
* @return {String} The base64-encoded string.
28+
*/
29+
function base64Encode(text){
30+
31+
//helper function to left-pad an array with zeros
32+
function padLeft(bits, length){
33+
while (bits.length < length){
34+
bits.unshift("0");
35+
}
36+
return bits;
37+
}
38+
39+
//helper function to right-pad an array with zeros
40+
function padRight(bits, length){
41+
while (bits.length < length){
42+
bits.push("0");
43+
}
44+
return bits;
45+
}
46+
47+
//local variables
48+
var digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
49+
part, index,
50+
i=0, j=0,
51+
padding="",
52+
quantaCount,
53+
bits = [],
54+
result = [];
55+
56+
//create an array of binary digits representing the text
57+
for (; i < text.length; i++){
58+
part = text.charCodeAt(i).toString(2);
59+
bits = bits.concat(padLeft(part.split(""), 8));
60+
}
61+
62+
//figure out how many 24-bit quanta are in the array
63+
quantaCount = Math.floor(bits.length / 24);
64+
65+
//encode all bits
66+
encodeBits: while(true){
67+
68+
//must encode one complete quanta at a time
69+
for(i=0; i < quantaCount; i++){
70+
for (j=0; j < 4; j++){
71+
part = bits.splice(0, 6).join("");
72+
index = parseInt(part,2);
73+
result.push(digits[index]);
74+
}
75+
}
76+
77+
//take care of any extra bits
78+
switch(bits.length){
79+
case 8:
80+
padRight(bits, 12);
81+
padding = "==";
82+
continue encodeBits;
83+
case 16:
84+
padRight(bits, 18);
85+
padding = "=";
86+
continue encodeBits;
87+
default:
88+
break encodeBits;
89+
}
90+
}
91+
92+
//add any padding to the result
93+
result.push(padding);
94+
95+
//return a string
96+
return result.join("");
97+
98+
}

0 commit comments

Comments
 (0)