Skip to content

Commit

Permalink
Base 64 encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas C. Zakas committed Nov 15, 2009
1 parent 543ee9f commit 7e2fdbd
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 0 deletions.
76 changes: 76 additions & 0 deletions encodings/base64/base64.htm
@@ -0,0 +1,76 @@
<html>
<head>
<title>Base64 Tests</title>
<!-- Combo-handled YUI CSS files: -->
<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">
<!-- Combo-handled YUI JS files: -->
<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>
<script type="text/javascript" src="base64.js"></script>


</head>
<body>
<h1>Doubly Linked List Tests</h1>
<script type="text/javascript">

YAHOO.namespace("test");

YAHOO.test.Base64 = (function(){

var assert = YAHOO.util.Assert;

//-------------------------------------------------------------------------
// Base Test Suite
//-------------------------------------------------------------------------

var suite = new YAHOO.tool.TestSuite("Base64 Tests");

//-------------------------------------------------------------------------
// Test Case for adding
//-------------------------------------------------------------------------

suite.add(new YAHOO.tool.TestCase({

name : "Base 64 Encoding Tests",

//---------------------------------------------------------------------
// Tests
//---------------------------------------------------------------------

testMan: function(){
var result = base64Encode("Man");
assert.areEqual("TWFu", result);
},

testHelloWorld: function(){
var result = base64Encode("Hello world");
assert.areEqual("SGVsbG8gd29ybGQ=", result);
},

testPhrase: function(){
var expected = "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=";
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.");
assert.areEqual(expected, result);
}
}));


//return it
return suite;

})();

(function (){
//create the logger
var logger = new YAHOO.tool.TestLogger();

//add the tests
YAHOO.tool.TestRunner.add(YAHOO.test.Base64);
YAHOO.tool.TestRunner.run();

})();


</script>
</body>
</html>
98 changes: 98 additions & 0 deletions encodings/base64/base64.js
@@ -0,0 +1,98 @@
/*
* Base 64 implementation in JavaScript
* Copyright (c) 2009 Nicholas C. Zakas. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

/**
* Base64-encodes a string of text.
* @param {String} text The text to encode
* @return {String} The base64-encoded string.
*/
function base64Encode(text){

//helper function to left-pad an array with zeros
function padLeft(bits, length){
while (bits.length < length){
bits.unshift("0");
}
return bits;
}

//helper function to right-pad an array with zeros
function padRight(bits, length){
while (bits.length < length){
bits.push("0");
}
return bits;
}

//local variables
var digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
part, index,
i=0, j=0,
padding="",
quantaCount,
bits = [],
result = [];

//create an array of binary digits representing the text
for (; i < text.length; i++){
part = text.charCodeAt(i).toString(2);
bits = bits.concat(padLeft(part.split(""), 8));
}

//figure out how many 24-bit quanta are in the array
quantaCount = Math.floor(bits.length / 24);

//encode all bits
encodeBits: while(true){

//must encode one complete quanta at a time
for(i=0; i < quantaCount; i++){
for (j=0; j < 4; j++){
part = bits.splice(0, 6).join("");
index = parseInt(part,2);
result.push(digits[index]);
}
}

//take care of any extra bits
switch(bits.length){
case 8:
padRight(bits, 12);
padding = "==";
continue encodeBits;
case 16:
padRight(bits, 18);
padding = "=";
continue encodeBits;
default:
break encodeBits;
}
}

//add any padding to the result
result.push(padding);

//return a string
return result.join("");

}

0 comments on commit 7e2fdbd

Please sign in to comment.