base64-min
Advanced tools
Comparing version 1.1.0 to 1.2.0
454
base64.js
@@ -1,253 +0,245 @@ | ||
/** | ||
* node module -> Base64, created by victorfern91 (a.k.a Victor Fernandes - victorfern91[at]gmail.com) | ||
* Module Version : 0.5.0 | ||
* Avaiable Functions : encoding & decoding / MIME | ||
* Outputs: Coded string or Decoded string, using Base64 | ||
*/ | ||
/*jslint node:true, plusplus: true, bitwise: true, vars: true*/ | ||
/*global describe, it*/ | ||
'use strict'; | ||
var encodeDictionary ='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; | ||
// for file function | ||
var fs = require('fs'); | ||
var encodeDictionary = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/', | ||
fs = require('fs'); | ||
/** | ||
* encode function() - public method | ||
*/ | ||
module.exports.encode = function (str, type){ | ||
var encodedString = ''; | ||
// First step, divid the input bytes streams into blocks of 3 bytes. | ||
var inputSliced = []; | ||
for(i = 0, length = str.length; i < length; i = i + 3){ | ||
inputSliced.push(str.slice(i,i+3)); | ||
} | ||
//encode all 3 byte blocks | ||
for(i = 0, length = inputSliced.length; i < length; i++){ | ||
encodedString += encodingSystem(inputSliced[i]); | ||
} | ||
//Functions Helpers (Private functions) | ||
// encoding type | ||
switch(type){ | ||
case 'MIME': | ||
encodedString = convertToMIME(encodedString); | ||
break; | ||
default: | ||
break; | ||
} | ||
// return encoded string | ||
return encodedString; | ||
}; | ||
function setCharAt(str, index, chr) { | ||
return (index > str.length - 1) ? str : str.substr(0, index) + chr + str.substr(index + 1); | ||
} | ||
/** | ||
* encodeWithKey | ||
* encode Messages with one key XOR | ||
*/ | ||
module.exports.encodeWithKey = function(str, key){ | ||
var strLength = str.length; | ||
var keyLength = key.length; | ||
var keyMsg = '' | ||
for(i = 0; i < strLength; i ++ ){ | ||
keyMsg += String.fromCharCode( str.charCodeAt(i) ^ key.charCodeAt(i%keyLength)); | ||
} | ||
* encoding block - private function | ||
* Update: Changed binary masks into Hexadecimal | ||
*/ | ||
function encodingBlock(slice) { | ||
var encodedSlice = '', | ||
j, | ||
sliceLength, | ||
charOne, | ||
charTwo, | ||
charThree, | ||
charFour; | ||
return this.encode(keyMsg); | ||
for (j = 0, sliceLength = slice.length; j < sliceLength; j++) { | ||
switch (j) { | ||
case 0: | ||
charOne = (slice.charCodeAt(j)) >> 2; | ||
encodedSlice += encodeDictionary[charOne]; | ||
break; | ||
case 1: | ||
charTwo = (slice.charCodeAt(j - 1) & 0x3) << 4; | ||
charTwo += (slice.charCodeAt(j) & 0xF0) >> 4; | ||
encodedSlice += encodeDictionary[charTwo]; | ||
charThree = (slice.charCodeAt(j) & 0xF) << 2; | ||
charThree += (slice.charCodeAt(j + 1) & 0xC0) >> 6; | ||
encodedSlice += encodeDictionary[charThree]; | ||
break; | ||
case 2: | ||
charFour = slice.charCodeAt(j) & 0x3F; | ||
encodedSlice += encodeDictionary[charFour]; | ||
break; | ||
} | ||
} | ||
return encodedSlice; | ||
} | ||
/** | ||
* encodeFile function() - public method | ||
* Only tested with the PNG Files | ||
*/ | ||
module.exports.encodeFile = function (file){ | ||
var inputData = fs.readFileSync(file); | ||
var fileToStr = ''; | ||
for(i = 0; i < inputData.length; i++){ | ||
fileToStr += String.fromCharCode(inputData[i]); | ||
} | ||
var inputSliced = []; | ||
for(i = 0, length = fileToStr.length; i < length; i = i + 3){ | ||
inputSliced.push(fileToStr.slice(i,i+3)); | ||
} | ||
var encodedString= ''; | ||
for(i = 0, length = inputSliced.length; i < length; i++){ | ||
encodedString += encodingSystem(inputSliced[i]); | ||
} | ||
return encodedString; | ||
}; | ||
function encodingSystem(slice) { | ||
var sliceLength = slice.length, | ||
encoded = ''; | ||
//if this slice/block have 3 bytes | ||
if (sliceLength === 3) { | ||
encoded = encodingBlock(slice); | ||
} else { // if slice/block doesn't have 3 bytes | ||
switch (sliceLength) { | ||
case 1: | ||
slice += '\u0000\u0000'; | ||
encoded = encodingBlock(slice); | ||
//add '=''=' | ||
encoded = setCharAt(encoded, 2, '='); | ||
encoded = setCharAt(encoded, 3, '='); | ||
break; | ||
case 2: | ||
slice += '\u0000'; | ||
encoded = encodingBlock(slice); | ||
// add = | ||
encoded = setCharAt(encoded, 3, '='); | ||
break; | ||
} | ||
} | ||
return encoded; | ||
} | ||
/** | ||
* decode function() - public method | ||
*/ | ||
module.exports.decode = function (str){ | ||
// auto detect MIME type | ||
if(str.match(/\n/) !== null){ | ||
str = decodeMIME(str); | ||
} | ||
// reverse process | ||
var inputSliced = []; | ||
var stringLength = str.length; | ||
var decodedString = ''; | ||
* decodingBlock - private function | ||
* This method receive 4-byte encoded slice and decode to 3 char string. | ||
* Update: Changed binary masks into Hexadecimal | ||
*/ | ||
function decodingBlock(slice) { | ||
var decodeSlice = '', | ||
j, | ||
length, | ||
charNumberOne, | ||
charNumberTwo, | ||
charNumberThree; | ||
//slice string into 4 byte slices | ||
for(i = 0; i < stringLength; i = i + 4){ | ||
inputSliced.push(str.slice(i,i+4)); | ||
} | ||
//decoding every slice except the last one | ||
for(i = 0, sliceLength = inputSliced.length; i < sliceLength; i++){ | ||
decodedString += decodingBlock(inputSliced[i]); | ||
} | ||
//last slice | ||
if(str.slice(-1) === '='){ | ||
decodedString = decodedString.slice(0,decodedString.length-1); | ||
} | ||
if(str.slice(-2) === '=='){ | ||
decodedString = decodedString.slice(0,decodedString.length-1); | ||
} | ||
//result | ||
return decodedString; | ||
for (j = 0, length = slice.length; j < length; j++) { | ||
switch (j) { | ||
case 0: | ||
charNumberOne = encodeDictionary.indexOf(slice[j]) << 2; | ||
charNumberOne += (encodeDictionary.indexOf(slice[j + 1]) & 0x30) >> 4; | ||
decodeSlice += String.fromCharCode(charNumberOne); | ||
break; | ||
case 1: | ||
charNumberTwo = (encodeDictionary.indexOf(slice[j]) & 0xF) << 4; | ||
charNumberTwo += (encodeDictionary.indexOf(slice[j + 1]) & 0x3C) >> 2; | ||
decodeSlice += String.fromCharCode(charNumberTwo); | ||
break; | ||
case 2: | ||
charNumberThree = (encodeDictionary.indexOf(slice[j]) & 0x3) << 6; | ||
charNumberThree += (encodeDictionary.indexOf(slice[j + 1]) & 0x3F); | ||
decodeSlice += String.fromCharCode(charNumberThree); | ||
break; | ||
} | ||
} | ||
return decodeSlice; | ||
} | ||
}; | ||
function convertToMIME(str) { | ||
var length, | ||
result = '', | ||
lastPosition = 0, | ||
i; | ||
for (i = 0, length = str.length; i < length; ++i) { | ||
if (i % 76 === 0 && i !== 0) { | ||
result += str.slice(lastPosition, i) + '\n'; | ||
lastPosition = i; | ||
if (lastPosition + 76 > length) { | ||
result += str.slice(lastPosition, length); | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
/** | ||
* decodeWithKey | ||
* decode Messages with one key XOR | ||
*/ | ||
module.exports.decodeWithKey = function(str, key){ | ||
var b64Decoded = this.decode(str); | ||
var keyLength = key.length; | ||
var decodedString = ''; | ||
for(i = 0, length = b64Decoded.length; i < length; i ++ ){ | ||
decodedString += String.fromCharCode( b64Decoded.charCodeAt(i) ^ key.charCodeAt(i%keyLength)); | ||
} | ||
return decodedString; | ||
function decodeMIME(str) { | ||
str = str.replace(/\r\n|\r|\n/g, ''); | ||
return str; | ||
} | ||
/** | ||
* decodeFile function() - public method | ||
* Only tested with the PNG Files | ||
*/ | ||
module.exports.decodeToFile = function (str, filePath){ | ||
// reverse process | ||
var data = this.decode(str); | ||
fs.writeFileSync(filePath,data,'binary'); | ||
}; | ||
// public functions | ||
//Functions Helpers (Private functions) | ||
module.exports = { | ||
encode: function (str, type) { | ||
var encodedString = '', | ||
inputSliced = [], | ||
i, | ||
length; | ||
/** | ||
* encodingSystem - private function | ||
*/ | ||
function encodingSystem(slice){ | ||
var sliceLength = slice.length; | ||
var encoded = ''; | ||
//if this slice/block have 3 byetes | ||
if(sliceLength === 3){ | ||
return encodingBlock(slice); | ||
} else { // if slice/block doesn't have 3 bytes | ||
switch(sliceLength){ | ||
case 1: | ||
slice += '\0\0'; | ||
encoded = encodingBlock(slice); | ||
//add '=''=' | ||
encoded = setCharAt(encoded,2,'='); | ||
encoded = setCharAt(encoded,3,'='); | ||
break; | ||
case 2: | ||
slice += '\0'; | ||
encoded = encodingBlock(slice); | ||
// add = | ||
encoded = setCharAt(encoded,3,'='); | ||
break; | ||
} | ||
} | ||
return encoded; | ||
} | ||
for (i = 0, length = str.length; i < length; i = i + 3) { // First step, divid the input bytes streams into blocks of 3 bytes. | ||
inputSliced.push(str.slice(i, i + 3)); | ||
} | ||
//encode all 3 byte blocks | ||
for (i = 0, length = inputSliced.length; i < length; ++i) { | ||
encodedString += encodingSystem(inputSliced[i]); | ||
} | ||
/** | ||
* encoding block - private function | ||
* Update: Changed binary masks into Hexadecimal | ||
*/ | ||
function encodingBlock(slice){ | ||
var encodedSlice = ''; | ||
for(j = 0, sliceLength = slice.length; j < sliceLength; j++){ | ||
switch(j){ | ||
case 0: | ||
var charOne = (slice.charCodeAt(j)) >> 2; | ||
encodedSlice += encodeDictionary[charOne]; | ||
break; | ||
case 1: | ||
var charTwo = (slice.charCodeAt(j-1) & 0x3) << 4; | ||
charTwo += (slice.charCodeAt(j) & 0xF0) >> 4 ; | ||
encodedSlice += encodeDictionary[charTwo]; | ||
var charThree = (slice.charCodeAt(j) & 0xF) << 2; | ||
charThree += (slice.charCodeAt(j+1) & 0xC0) >> 6; | ||
encodedSlice += encodeDictionary[charThree]; | ||
break; | ||
case 2: | ||
var charFour = slice.charCodeAt(j) & 0x3F; | ||
encodedSlice += encodeDictionary[charFour]; | ||
break; | ||
} | ||
} | ||
return encodedSlice; | ||
} | ||
// encoding type | ||
switch (type) { | ||
case 'MIME': | ||
encodedString = convertToMIME(encodedString); | ||
break; | ||
default: | ||
break; | ||
} | ||
// return encoded string | ||
return encodedString; | ||
}, | ||
/** | ||
* decodingBlock - private function | ||
* This method receive 4-byte encoded slice and decode to 3 char string. | ||
* Update: Changed binary masks into Hexadecimal | ||
*/ | ||
function decodingBlock(slice){ | ||
var decodeSlice = ''; | ||
for(j = 0, length = slice.length; j < length; j++){ | ||
switch(j){ | ||
case 0: | ||
var charNumberOne = encodeDictionary.indexOf(slice[j]) << 2; | ||
charNumberOne += (encodeDictionary.indexOf(slice[j+1]) & 0x30) >> 4; | ||
decodeSlice += String.fromCharCode(charNumberOne); | ||
break; | ||
case 1: | ||
var charNumberTwo = (encodeDictionary.indexOf(slice[j]) & 0xF) << 4; | ||
charNumberTwo += (encodeDictionary.indexOf(slice[j+1]) & 0x3C) >> 2; | ||
decodeSlice += String.fromCharCode(charNumberTwo); | ||
break; | ||
case 2: | ||
var charNumberThree = (encodeDictionary.indexOf(slice[j]) & 0x3) << 6; | ||
charNumberThree += (encodeDictionary.indexOf(slice[j+1]) & 0x3F); | ||
decodeSlice += String.fromCharCode(charNumberThree); | ||
break; | ||
} | ||
} | ||
return decodeSlice; | ||
} | ||
encodeWithKey: function (str, key) { | ||
var strLength = str.length, | ||
keyLength = key.length, | ||
keyMsg = '', | ||
i; | ||
/** | ||
* setCharAt - private function | ||
*/ | ||
function setCharAt(str,index,chr) { | ||
if(index > str.length-1) return str; | ||
return str.substr(0,index) + chr + str.substr(index+1); | ||
} | ||
for (i = 0; i < strLength; ++i) { | ||
keyMsg += String.fromCharCode(str.charCodeAt(i) ^ key.charCodeAt(i % keyLength)); | ||
} | ||
/** | ||
* convertToMIME - private function | ||
*/ | ||
function convertToMIME(str){ | ||
var length = str.length; | ||
var result = ''; | ||
var lastPosition = 0; | ||
for(i = 0, length = str.length; i < length ; i++){ | ||
if( i%76 === 0 && i != 0){ | ||
result += str.slice(lastPosition, i) + '\n'; | ||
lastPosition = i; | ||
if(lastPosition + 76 > length){ | ||
result += str.slice(lastPosition, length); | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
return this.encode(keyMsg); | ||
}, | ||
encodeFile: function (file) { | ||
var inputData = fs.readFileSync(file), | ||
fileToStr = '', | ||
i, | ||
length, | ||
inputSliced = [], | ||
encodedString = ''; | ||
function decodeMIME(str){ | ||
str = str.replace(/\r\n|\r|\n/g, ''); | ||
return str; | ||
} | ||
for (i = 0; i < inputData.length; ++i) { | ||
fileToStr += String.fromCharCode(inputData[i]); | ||
} | ||
for (i = 0, length = fileToStr.length; i < length; i = i + 3) { | ||
inputSliced.push(fileToStr.slice(i, i + 3)); | ||
} | ||
for (i = 0, length = inputSliced.length; i < length; ++i) { | ||
encodedString += encodingSystem(inputSliced[i]); | ||
} | ||
return encodedString; | ||
}, | ||
decode: function (str) { | ||
// auto detect MIME type | ||
if (str.match(/\n/) !== null) { | ||
str = decodeMIME(str); | ||
} | ||
// reverse process | ||
var inputSliced = []; | ||
var stringLength = str.length; | ||
var decodedString = '', | ||
i, | ||
sliceLength; | ||
//slice string into 4 byte slices | ||
for (i = 0; i < stringLength; i = i + 4) { | ||
inputSliced.push(str.slice(i, i + 4)); | ||
} | ||
//decoding every slice except the last one | ||
for (i = 0, sliceLength = inputSliced.length; i < sliceLength; ++i) { | ||
decodedString += decodingBlock(inputSliced[i]); | ||
} | ||
//last slice | ||
if (str.slice(-1) === '=') { | ||
decodedString = decodedString.slice(0, decodedString.length - 1); | ||
} | ||
if (str.slice(-2) === '==') { | ||
decodedString = decodedString.slice(0, decodedString.length - 1); | ||
} | ||
//result | ||
return decodedString; | ||
}, | ||
decodeWithKey: function (str, key) { | ||
var b64Decoded = this.decode(str); | ||
var keyLength = key.length; | ||
var decodedString = '', | ||
i, | ||
length; | ||
for (i = 0, length = b64Decoded.length; i < length; ++i) { | ||
decodedString += String.fromCharCode(b64Decoded.charCodeAt(i) ^ key.charCodeAt(i % keyLength)); | ||
} | ||
return decodedString; | ||
}, | ||
decodeToFile: function (str, filePath) { | ||
// reverse process | ||
var data = this.decode(str); | ||
fs.writeFileSync(filePath, data, 'binary'); | ||
} | ||
}; |
{ | ||
"name": "base64-min", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Minimalist package, focused in best performance to encode and decode base64.", | ||
@@ -5,0 +5,0 @@ "main": "base64.js", |
@@ -8,4 +8,4 @@ # base64 (npm base64-min) | ||
I'm trying to add new features based in other packages to get an AIO npm module. | ||
Actually **base64-min** can encode and decode: **strings**, **files** and **strings with XOR encryptation**. | ||
I'm trying to add new features based on other packages to get an all in one npm module. | ||
Actually **base64-min** can encode and decode: **strings**, **files** and **strings with XOR encrytion**. | ||
@@ -15,3 +15,3 @@ <a href="https://nodei.co/npm/base64-min/"><img src="https://nodei.co/npm/base64-min.png?downloads=true&downloadRank=true&stars=true"></a> | ||
## Why use this module? | ||
This module contains private and public methods, it's minimalist, and focused in the best javascript performance. | ||
It's minimalist and focused in the best javascript performance. | ||
@@ -27,3 +27,3 @@ | ||
``` | ||
$ npm install -g base64-min | ||
$ npm install -g base64-min | ||
``` | ||
@@ -36,3 +36,3 @@ | ||
$ npm install | ||
$ npm test | ||
$ npm test | ||
``` | ||
@@ -53,3 +53,3 @@ <img src="http://i.imgur.com/U7rayiT.png"/> | ||
Example: | ||
Example: | ||
```javascript | ||
@@ -125,6 +125,9 @@ var base64 = require('base64-min'); | ||
### Changelog | ||
**v1.0.0 | ||
**v1.2.0** | ||
- Added more unit tests. Now the test coverage is 100% :) | ||
- JSlinted all application. | ||
**v1.1.0** | ||
- Full integration with Travis-CI and coveralls. | ||
### Changelog | ||
**v0.5.3 & v0.5.4** | ||
@@ -160,6 +163,6 @@ - Update documentation (README.md file) | ||
**v0.2.0 :** | ||
- Encode process more modular (added one more private function). | ||
- Encode process more modular (added one more private function). | ||
- Added more two new functions: **encodeFile** and **decodeSaveFile** (only tested with PNG Files). | ||
## Future updates: | ||
- Add compatibility with MIME, and other standards (RFC ****, etc). | ||
- Add compatibility with MIME, and other standards (RFC ****, etc). |
@@ -0,1 +1,5 @@ | ||
/*jslint node:true, vars: true*/ | ||
/*global describe, it*/ | ||
'use strict'; | ||
var should = require('chai').should(), | ||
@@ -6,50 +10,50 @@ base64 = require('../base64'), | ||
// For more information: http://en.wikipedia.org/wiki/Base64 | ||
describe('# Decoding Tests', function(){ | ||
describe('# Decoding Tests', function () { | ||
describe('> Decode strings', function(){ | ||
describe('> Decode strings', function () { | ||
it('Decode a Wikipedia Leviathan Quote Example', function() { | ||
//Wikipedia first example: A quote from Thomas Hobbes' Leviathan: | ||
var leviathanQuote = '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.'; | ||
var leviathanQuote64Based = 'TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4='; | ||
decode(leviathanQuote64Based).should.equal(leviathanQuote); | ||
}); | ||
it('Decoding Base64 Padding Wikipedia examples', function() { | ||
//Wikipedia Padding examples | ||
decode('YW55IGNhcm5hbCBwbGVhc3VyZS4=').should.equal('any carnal pleasure.'); | ||
decode('YW55IGNhcm5hbCBwbGVhc3VyZQ==').should.equal('any carnal pleasure'); | ||
decode('YW55IGNhcm5hbCBwbGVhc3Vy').should.equal('any carnal pleasur'); | ||
decode('YW55IGNhcm5hbCBwbGVhc3U=').should.equal('any carnal pleasu'); | ||
decode('YW55IGNhcm5hbCBwbGVhcw==').should.equal('any carnal pleas'); | ||
}); | ||
it('Decode a Wikipedia Leviathan Quote Example', function () { | ||
//Wikipedia first example: A quote from Thomas Hobbes' Leviathan: | ||
var leviathanQuote = '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.'; | ||
var leviathanQuote64Based = 'TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4='; | ||
decode(leviathanQuote64Based).should.equal(leviathanQuote); | ||
}); | ||
it('Decoding Base64 Padding Wikipedia examples', function () { | ||
//Wikipedia Padding examples | ||
decode('YW55IGNhcm5hbCBwbGVhc3VyZS4=').should.equal('any carnal pleasure.'); | ||
decode('YW55IGNhcm5hbCBwbGVhc3VyZQ==').should.equal('any carnal pleasure'); | ||
decode('YW55IGNhcm5hbCBwbGVhc3Vy').should.equal('any carnal pleasur'); | ||
decode('YW55IGNhcm5hbCBwbGVhc3U=').should.equal('any carnal pleasu'); | ||
decode('YW55IGNhcm5hbCBwbGVhcw==').should.equal('any carnal pleas'); | ||
}); | ||
it('More deconding tests, Base64 with different Paddings', function() { | ||
//More Wikipedia Padding examples | ||
decode('cGxlYXN1cmUu').should.equal('pleasure.'); | ||
decode('bGVhc3VyZS4=').should.equal('leasure.'); | ||
decode('ZWFzdXJlLg==').should.equal('easure.'); | ||
decode('YXN1cmUu').should.equal('asure.'); | ||
decode('c3VyZS4=').should.equal('sure.'); | ||
}); | ||
it('More deconding tests, Base64 with different Paddings', function () { | ||
//More Wikipedia Padding examples | ||
decode('cGxlYXN1cmUu').should.equal('pleasure.'); | ||
decode('bGVhc3VyZS4=').should.equal('leasure.'); | ||
decode('ZWFzdXJlLg==').should.equal('easure.'); | ||
decode('YXN1cmUu').should.equal('asure.'); | ||
decode('c3VyZS4=').should.equal('sure.'); | ||
}); | ||
it('Random tests founded on internet', function() { | ||
//http://examples.javacodegeeks.com/core-java/apache/commons/codec/encode-base64/ | ||
decode('SmF2YWNvZGVnZWVrcw==').should.equal('Javacodegeeks'); | ||
//http://pymotw.com/2/base64/ | ||
decode('VGhpcyBpcyB0aGUgZGF0YSwgaW4gdGhlIGNsZWFyLg==').should.equal('This is the data, in the clear.'); | ||
//http://stackoverflow.com/questions/7360403/base-64-encode-and-decode-example-code | ||
decode('dGVjaFBhc3M=').should.equal('techPass'); | ||
}); | ||
it('Random tests founded on internet', function () { | ||
//http://examples.javacodegeeks.com/core-java/apache/commons/codec/encode-base64/ | ||
decode('SmF2YWNvZGVnZWVrcw==').should.equal('Javacodegeeks'); | ||
//http://pymotw.com/2/base64/ | ||
decode('VGhpcyBpcyB0aGUgZGF0YSwgaW4gdGhlIGNsZWFyLg==').should.equal('This is the data, in the clear.'); | ||
//http://stackoverflow.com/questions/7360403/base-64-encode-and-decode-example-code | ||
decode('dGVjaFBhc3M=').should.equal('techPass'); | ||
}); | ||
it('Decode Long Texts', function() { | ||
//Based in http://www.motobit.com/util/base64-decoder-encoder.asp | ||
var WikipediaText1 = 'Não se pode pensar em heresia porque não fazia sentido, em tempos de Contra-Reforma, acreditar nos deuses do panteão greco-romano, e a prova é a não censura dos inquisidores aos «Deoses dos Gentios». No episódio da Máquina do Mundo (estrofe 82 do Canto X), é o próprio personagem da deusa Tétis que afirma: «eu, Saturno e Jano, Júpiter, Juno, fomos fabulosos, Fingidos de mortal e cego engano. Só pera fazer versos deleitosos Servimos». No entanto, críticos defendem que esta fala de Tétis foi introduzida a pedido dos Censores, e que várias outras Oitavas foram ou alteradas, ou mesmo cortadas, para que o Poema pudesse ser publicado.'; | ||
var WikipediaText1based64 = 'TuNvIHNlIHBvZGUgcGVuc2FyIGVtIGhlcmVzaWEgcG9ycXVlIG7jbyBmYXppYSBzZW50aWRvLCBlbSB0ZW1wb3MgZGUgQ29udHJhLVJlZm9ybWEsIGFjcmVkaXRhciBub3MgZGV1c2VzIGRvIHBhbnRl428gZ3JlY28tcm9tYW5vLCBlIGEgcHJvdmEg6SBhIG7jbyBjZW5zdXJhIGRvcyBpbnF1aXNpZG9yZXMgYW9zIKtEZW9zZXMgZG9zIEdlbnRpb3O7LiBObyBlcGlz82RpbyBkYSBN4XF1aW5hIGRvIE11bmRvIChlc3Ryb2ZlIDgyIGRvIENhbnRvIFgpLCDpIG8gcHLzcHJpbyBwZXJzb25hZ2VtIGRhIGRldXNhIFTpdGlzIHF1ZSBhZmlybWE6IKtldSwgU2F0dXJubyBlIEphbm8sIEr6cGl0ZXIsIEp1bm8sIGZvbW9zIGZhYnVsb3NvcywgRmluZ2lkb3MgZGUgbW9ydGFsIGUgY2VnbyBlbmdhbm8uIFPzIHBlcmEgZmF6ZXIgdmVyc29zIGRlbGVpdG9zb3MgU2Vydmltb3O7LiBObyBlbnRhbnRvLCBjcu10aWNvcyBkZWZlbmRlbSBxdWUgZXN0YSBmYWxhIGRlIFTpdGlzIGZvaSBpbnRyb2R1emlkYSBhIHBlZGlkbyBkb3MgQ2Vuc29yZXMsIGUgcXVlIHbhcmlhcyBvdXRyYXMgT2l0YXZhcyBmb3JhbSBvdSBhbHRlcmFkYXMsIG91IG1lc21vIGNvcnRhZGFzLCBwYXJhIHF1ZSBvIFBvZW1hIHB1ZGVzc2Ugc2VyIHB1YmxpY2Fkby4='; | ||
decode(WikipediaText1based64).should.equal(WikipediaText1); | ||
var WikipediaText2 = "Fanny Bullock Workman was an American geographer, cartographer, explorer, travel writer, and mountaineer, notably in the Himalaya. She was one of the first female professional mountaineers; she not only explored but also wrote about her adventures. She set several women's altitude records, published eight travel books with her husband, and championed women's rights and women's suffrage. Educated in the finest schools available to women, she was introduced to climbing in New Hampshire. She married William Hunter Workman, and traveled the world with him. The couple had two children, but left them in schools and with nurses. Workman saw herself as a New Woman who could equal any man. The Workmans wrote books about each trip and Workman frequently commented on the state of the lives of women that she saw. They explored several glaciers and conquered several mountains of the Himalaya, eventually reaching 23,000 feet (7,000 m), a women's altitude record at the time. Workman became the first woman to lecture at the Sorbonne and the second to speak at the Royal Geographical Society. She received many medals of honor and was recognized as one of the foremost climbers of her day."; | ||
var WikipediaText2based64 = 'RmFubnkgQnVsbG9jayBXb3JrbWFuIHdhcyBhbiBBbWVyaWNhbiBnZW9ncmFwaGVyLCBjYXJ0b2dyYXBoZXIsIGV4cGxvcmVyLCB0cmF2ZWwgd3JpdGVyLCBhbmQgbW91bnRhaW5lZXIsIG5vdGFibHkgaW4gdGhlIEhpbWFsYXlhLiBTaGUgd2FzIG9uZSBvZiB0aGUgZmlyc3QgZmVtYWxlIHByb2Zlc3Npb25hbCBtb3VudGFpbmVlcnM7IHNoZSBub3Qgb25seSBleHBsb3JlZCBidXQgYWxzbyB3cm90ZSBhYm91dCBoZXIgYWR2ZW50dXJlcy4gU2hlIHNldCBzZXZlcmFsIHdvbWVuJ3MgYWx0aXR1ZGUgcmVjb3JkcywgcHVibGlzaGVkIGVpZ2h0IHRyYXZlbCBib29rcyB3aXRoIGhlciBodXNiYW5kLCBhbmQgY2hhbXBpb25lZCB3b21lbidzIHJpZ2h0cyBhbmQgd29tZW4ncyBzdWZmcmFnZS4gRWR1Y2F0ZWQgaW4gdGhlIGZpbmVzdCBzY2hvb2xzIGF2YWlsYWJsZSB0byB3b21lbiwgc2hlIHdhcyBpbnRyb2R1Y2VkIHRvIGNsaW1iaW5nIGluIE5ldyBIYW1wc2hpcmUuIFNoZSBtYXJyaWVkIFdpbGxpYW0gSHVudGVyIFdvcmttYW4sIGFuZCB0cmF2ZWxlZCB0aGUgd29ybGQgd2l0aCBoaW0uIFRoZSBjb3VwbGUgaGFkIHR3byBjaGlsZHJlbiwgYnV0IGxlZnQgdGhlbSBpbiBzY2hvb2xzIGFuZCB3aXRoIG51cnNlcy4gV29ya21hbiBzYXcgaGVyc2VsZiBhcyBhIE5ldyBXb21hbiB3aG8gY291bGQgZXF1YWwgYW55IG1hbi4gVGhlIFdvcmttYW5zIHdyb3RlIGJvb2tzIGFib3V0IGVhY2ggdHJpcCBhbmQgV29ya21hbiBmcmVxdWVudGx5IGNvbW1lbnRlZCBvbiB0aGUgc3RhdGUgb2YgdGhlIGxpdmVzIG9mIHdvbWVuIHRoYXQgc2hlIHNhdy4gVGhleSBleHBsb3JlZCBzZXZlcmFsIGdsYWNpZXJzIGFuZCBjb25xdWVyZWQgc2V2ZXJhbCBtb3VudGFpbnMgb2YgdGhlIEhpbWFsYXlhLCBldmVudHVhbGx5IHJlYWNoaW5nIDIzLDAwMCBmZWV0ICg3LDAwMCBtKSwgYSB3b21lbidzIGFsdGl0dWRlIHJlY29yZCBhdCB0aGUgdGltZS4gV29ya21hbiBiZWNhbWUgdGhlIGZpcnN0IHdvbWFuIHRvIGxlY3R1cmUgYXQgdGhlIFNvcmJvbm5lIGFuZCB0aGUgc2Vjb25kIHRvIHNwZWFrIGF0IHRoZSBSb3lhbCBHZW9ncmFwaGljYWwgU29jaWV0eS4gU2hlIHJlY2VpdmVkIG1hbnkgbWVkYWxzIG9mIGhvbm9yIGFuZCB3YXMgcmVjb2duaXplZCBhcyBvbmUgb2YgdGhlIGZvcmVtb3N0IGNsaW1iZXJzIG9mIGhlciBkYXku'; | ||
decode(WikipediaText2based64).should.equal(WikipediaText2); | ||
decode('SXN0byDpIGFwZW5hcyB1bSBncmFuZGUgVEVTVEU=').should.equal('Isto é apenas um grande TESTE'); | ||
it('Decode Long Texts', function () { | ||
//Based in http://www.motobit.com/util/base64-decoder-encoder.asp | ||
var WikipediaText1 = 'Não se pode pensar em heresia porque não fazia sentido, em tempos de Contra-Reforma, acreditar nos deuses do panteão greco-romano, e a prova é a não censura dos inquisidores aos «Deoses dos Gentios». No episódio da Máquina do Mundo (estrofe 82 do Canto X), é o próprio personagem da deusa Tétis que afirma: «eu, Saturno e Jano, Júpiter, Juno, fomos fabulosos, Fingidos de mortal e cego engano. Só pera fazer versos deleitosos Servimos». No entanto, críticos defendem que esta fala de Tétis foi introduzida a pedido dos Censores, e que várias outras Oitavas foram ou alteradas, ou mesmo cortadas, para que o Poema pudesse ser publicado.'; | ||
var WikipediaText1based64 = 'TuNvIHNlIHBvZGUgcGVuc2FyIGVtIGhlcmVzaWEgcG9ycXVlIG7jbyBmYXppYSBzZW50aWRvLCBlbSB0ZW1wb3MgZGUgQ29udHJhLVJlZm9ybWEsIGFjcmVkaXRhciBub3MgZGV1c2VzIGRvIHBhbnRl428gZ3JlY28tcm9tYW5vLCBlIGEgcHJvdmEg6SBhIG7jbyBjZW5zdXJhIGRvcyBpbnF1aXNpZG9yZXMgYW9zIKtEZW9zZXMgZG9zIEdlbnRpb3O7LiBObyBlcGlz82RpbyBkYSBN4XF1aW5hIGRvIE11bmRvIChlc3Ryb2ZlIDgyIGRvIENhbnRvIFgpLCDpIG8gcHLzcHJpbyBwZXJzb25hZ2VtIGRhIGRldXNhIFTpdGlzIHF1ZSBhZmlybWE6IKtldSwgU2F0dXJubyBlIEphbm8sIEr6cGl0ZXIsIEp1bm8sIGZvbW9zIGZhYnVsb3NvcywgRmluZ2lkb3MgZGUgbW9ydGFsIGUgY2VnbyBlbmdhbm8uIFPzIHBlcmEgZmF6ZXIgdmVyc29zIGRlbGVpdG9zb3MgU2Vydmltb3O7LiBObyBlbnRhbnRvLCBjcu10aWNvcyBkZWZlbmRlbSBxdWUgZXN0YSBmYWxhIGRlIFTpdGlzIGZvaSBpbnRyb2R1emlkYSBhIHBlZGlkbyBkb3MgQ2Vuc29yZXMsIGUgcXVlIHbhcmlhcyBvdXRyYXMgT2l0YXZhcyBmb3JhbSBvdSBhbHRlcmFkYXMsIG91IG1lc21vIGNvcnRhZGFzLCBwYXJhIHF1ZSBvIFBvZW1hIHB1ZGVzc2Ugc2VyIHB1YmxpY2Fkby4='; | ||
decode(WikipediaText1based64).should.equal(WikipediaText1); | ||
var WikipediaText2 = "Fanny Bullock Workman was an American geographer, cartographer, explorer, travel writer, and mountaineer, notably in the Himalaya. She was one of the first female professional mountaineers; she not only explored but also wrote about her adventures. She set several women's altitude records, published eight travel books with her husband, and championed women's rights and women's suffrage. Educated in the finest schools available to women, she was introduced to climbing in New Hampshire. She married William Hunter Workman, and traveled the world with him. The couple had two children, but left them in schools and with nurses. Workman saw herself as a New Woman who could equal any man. The Workmans wrote books about each trip and Workman frequently commented on the state of the lives of women that she saw. They explored several glaciers and conquered several mountains of the Himalaya, eventually reaching 23,000 feet (7,000 m), a women's altitude record at the time. Workman became the first woman to lecture at the Sorbonne and the second to speak at the Royal Geographical Society. She received many medals of honor and was recognized as one of the foremost climbers of her day."; | ||
var WikipediaText2based64 = 'RmFubnkgQnVsbG9jayBXb3JrbWFuIHdhcyBhbiBBbWVyaWNhbiBnZW9ncmFwaGVyLCBjYXJ0b2dyYXBoZXIsIGV4cGxvcmVyLCB0cmF2ZWwgd3JpdGVyLCBhbmQgbW91bnRhaW5lZXIsIG5vdGFibHkgaW4gdGhlIEhpbWFsYXlhLiBTaGUgd2FzIG9uZSBvZiB0aGUgZmlyc3QgZmVtYWxlIHByb2Zlc3Npb25hbCBtb3VudGFpbmVlcnM7IHNoZSBub3Qgb25seSBleHBsb3JlZCBidXQgYWxzbyB3cm90ZSBhYm91dCBoZXIgYWR2ZW50dXJlcy4gU2hlIHNldCBzZXZlcmFsIHdvbWVuJ3MgYWx0aXR1ZGUgcmVjb3JkcywgcHVibGlzaGVkIGVpZ2h0IHRyYXZlbCBib29rcyB3aXRoIGhlciBodXNiYW5kLCBhbmQgY2hhbXBpb25lZCB3b21lbidzIHJpZ2h0cyBhbmQgd29tZW4ncyBzdWZmcmFnZS4gRWR1Y2F0ZWQgaW4gdGhlIGZpbmVzdCBzY2hvb2xzIGF2YWlsYWJsZSB0byB3b21lbiwgc2hlIHdhcyBpbnRyb2R1Y2VkIHRvIGNsaW1iaW5nIGluIE5ldyBIYW1wc2hpcmUuIFNoZSBtYXJyaWVkIFdpbGxpYW0gSHVudGVyIFdvcmttYW4sIGFuZCB0cmF2ZWxlZCB0aGUgd29ybGQgd2l0aCBoaW0uIFRoZSBjb3VwbGUgaGFkIHR3byBjaGlsZHJlbiwgYnV0IGxlZnQgdGhlbSBpbiBzY2hvb2xzIGFuZCB3aXRoIG51cnNlcy4gV29ya21hbiBzYXcgaGVyc2VsZiBhcyBhIE5ldyBXb21hbiB3aG8gY291bGQgZXF1YWwgYW55IG1hbi4gVGhlIFdvcmttYW5zIHdyb3RlIGJvb2tzIGFib3V0IGVhY2ggdHJpcCBhbmQgV29ya21hbiBmcmVxdWVudGx5IGNvbW1lbnRlZCBvbiB0aGUgc3RhdGUgb2YgdGhlIGxpdmVzIG9mIHdvbWVuIHRoYXQgc2hlIHNhdy4gVGhleSBleHBsb3JlZCBzZXZlcmFsIGdsYWNpZXJzIGFuZCBjb25xdWVyZWQgc2V2ZXJhbCBtb3VudGFpbnMgb2YgdGhlIEhpbWFsYXlhLCBldmVudHVhbGx5IHJlYWNoaW5nIDIzLDAwMCBmZWV0ICg3LDAwMCBtKSwgYSB3b21lbidzIGFsdGl0dWRlIHJlY29yZCBhdCB0aGUgdGltZS4gV29ya21hbiBiZWNhbWUgdGhlIGZpcnN0IHdvbWFuIHRvIGxlY3R1cmUgYXQgdGhlIFNvcmJvbm5lIGFuZCB0aGUgc2Vjb25kIHRvIHNwZWFrIGF0IHRoZSBSb3lhbCBHZW9ncmFwaGljYWwgU29jaWV0eS4gU2hlIHJlY2VpdmVkIG1hbnkgbWVkYWxzIG9mIGhvbm9yIGFuZCB3YXMgcmVjb2duaXplZCBhcyBvbmUgb2YgdGhlIGZvcmVtb3N0IGNsaW1iZXJzIG9mIGhlciBkYXku'; | ||
decode(WikipediaText2based64).should.equal(WikipediaText2); | ||
decode('SXN0byDpIGFwZW5hcyB1bSBncmFuZGUgVEVTVEU=').should.equal('Isto é apenas um grande TESTE'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -0,1 +1,5 @@ | ||
/*jslint node: true*/ | ||
/*global describe, it*/ | ||
'use strict'; | ||
var should = require('chai').should(), | ||
@@ -6,50 +10,50 @@ base64 = require('../base64'), | ||
// For more information: http://en.wikipedia.org/wiki/Base64 | ||
describe('# Encoding Tests', function(){ | ||
describe('# Encoding Tests', function () { | ||
describe('> Encode strings', function(){ | ||
describe('> Encode strings', function () { | ||
it('Encode a Wikipedia Leviathan Quote Example', function() { | ||
//Wikipedia first example: A quote from Thomas Hobbes' Leviathan: | ||
var leviathanQuote = '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.'; | ||
var leviathanQuote64Based = 'TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4='; | ||
encode(leviathanQuote).should.equal(leviathanQuote64Based); | ||
}); | ||
it('Base64 Padding, testing final indicators == and =', function() { | ||
//Wikipedia Padding examples | ||
encode('any carnal pleasure.').should.equal('YW55IGNhcm5hbCBwbGVhc3VyZS4='); | ||
encode('any carnal pleasure').should.equal('YW55IGNhcm5hbCBwbGVhc3VyZQ=='); | ||
encode('any carnal pleasur').should.equal('YW55IGNhcm5hbCBwbGVhc3Vy'); | ||
encode('any carnal pleasu').should.equal('YW55IGNhcm5hbCBwbGVhc3U='); | ||
encode('any carnal pleas').should.equal('YW55IGNhcm5hbCBwbGVhcw=='); | ||
}); | ||
it('Encode a Wikipedia Leviathan Quote Example', function () { | ||
//Wikipedia first example: A quote from Thomas Hobbes' Leviathan: | ||
var leviathanQuote = '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.', | ||
leviathanQuote64Based = 'TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4='; | ||
encode(leviathanQuote).should.equal(leviathanQuote64Based); | ||
}); | ||
it('Base64 Padding, testing final indicators == and =', function () { | ||
//Wikipedia Padding examples | ||
encode('any carnal pleasure.').should.equal('YW55IGNhcm5hbCBwbGVhc3VyZS4='); | ||
encode('any carnal pleasure').should.equal('YW55IGNhcm5hbCBwbGVhc3VyZQ=='); | ||
encode('any carnal pleasur').should.equal('YW55IGNhcm5hbCBwbGVhc3Vy'); | ||
encode('any carnal pleasu').should.equal('YW55IGNhcm5hbCBwbGVhc3U='); | ||
encode('any carnal pleas').should.equal('YW55IGNhcm5hbCBwbGVhcw=='); | ||
}); | ||
it('More Base64 Padding tests', function() { | ||
//Wikipedia Padding examples | ||
encode('pleasure.').should.equal('cGxlYXN1cmUu'); | ||
encode('leasure.').should.equal('bGVhc3VyZS4='); | ||
encode('easure.').should.equal('ZWFzdXJlLg=='); | ||
encode('asure.').should.equal('YXN1cmUu'); | ||
encode('sure.').should.equal('c3VyZS4='); | ||
}); | ||
it('More Base64 Padding tests', function () { | ||
//Wikipedia Padding examples | ||
encode('pleasure.').should.equal('cGxlYXN1cmUu'); | ||
encode('leasure.').should.equal('bGVhc3VyZS4='); | ||
encode('easure.').should.equal('ZWFzdXJlLg=='); | ||
encode('asure.').should.equal('YXN1cmUu'); | ||
encode('sure.').should.equal('c3VyZS4='); | ||
}); | ||
it('Random tests founded on internet', function() { | ||
//http://examples.javacodegeeks.com/core-java/apache/commons/codec/encode-base64/ | ||
encode('Javacodegeeks').should.equal('SmF2YWNvZGVnZWVrcw=='); | ||
//http://pymotw.com/2/base64/ | ||
encode('This is the data, in the clear.').should.equal('VGhpcyBpcyB0aGUgZGF0YSwgaW4gdGhlIGNsZWFyLg=='); | ||
//http://stackoverflow.com/questions/7360403/base-64-encode-and-decode-example-code | ||
encode('techPass').should.equal('dGVjaFBhc3M='); | ||
}); | ||
it('Random tests founded on internet', function () { | ||
//http://examples.javacodegeeks.com/core-java/apache/commons/codec/encode-base64/ | ||
encode('Javacodegeeks').should.equal('SmF2YWNvZGVnZWVrcw=='); | ||
//http://pymotw.com/2/base64/ | ||
encode('This is the data, in the clear.').should.equal('VGhpcyBpcyB0aGUgZGF0YSwgaW4gdGhlIGNsZWFyLg=='); | ||
//http://stackoverflow.com/questions/7360403/base-64-encode-and-decode-example-code | ||
encode('techPass').should.equal('dGVjaFBhc3M='); | ||
}); | ||
it('Encode Long Texts', function() { | ||
//Based in http://www.motobit.com/util/base64-decoder-encoder.asp | ||
var WikipediaText1 = 'Não se pode pensar em heresia porque não fazia sentido, em tempos de Contra-Reforma, acreditar nos deuses do panteão greco-romano, e a prova é a não censura dos inquisidores aos «Deoses dos Gentios». No episódio da Máquina do Mundo (estrofe 82 do Canto X), é o próprio personagem da deusa Tétis que afirma: «eu, Saturno e Jano, Júpiter, Juno, fomos fabulosos, Fingidos de mortal e cego engano. Só pera fazer versos deleitosos Servimos». No entanto, críticos defendem que esta fala de Tétis foi introduzida a pedido dos Censores, e que várias outras Oitavas foram ou alteradas, ou mesmo cortadas, para que o Poema pudesse ser publicado.'; | ||
var WikipediaText1based64 = 'TuNvIHNlIHBvZGUgcGVuc2FyIGVtIGhlcmVzaWEgcG9ycXVlIG7jbyBmYXppYSBzZW50aWRvLCBlbSB0ZW1wb3MgZGUgQ29udHJhLVJlZm9ybWEsIGFjcmVkaXRhciBub3MgZGV1c2VzIGRvIHBhbnRl428gZ3JlY28tcm9tYW5vLCBlIGEgcHJvdmEg6SBhIG7jbyBjZW5zdXJhIGRvcyBpbnF1aXNpZG9yZXMgYW9zIKtEZW9zZXMgZG9zIEdlbnRpb3O7LiBObyBlcGlz82RpbyBkYSBN4XF1aW5hIGRvIE11bmRvIChlc3Ryb2ZlIDgyIGRvIENhbnRvIFgpLCDpIG8gcHLzcHJpbyBwZXJzb25hZ2VtIGRhIGRldXNhIFTpdGlzIHF1ZSBhZmlybWE6IKtldSwgU2F0dXJubyBlIEphbm8sIEr6cGl0ZXIsIEp1bm8sIGZvbW9zIGZhYnVsb3NvcywgRmluZ2lkb3MgZGUgbW9ydGFsIGUgY2VnbyBlbmdhbm8uIFPzIHBlcmEgZmF6ZXIgdmVyc29zIGRlbGVpdG9zb3MgU2Vydmltb3O7LiBObyBlbnRhbnRvLCBjcu10aWNvcyBkZWZlbmRlbSBxdWUgZXN0YSBmYWxhIGRlIFTpdGlzIGZvaSBpbnRyb2R1emlkYSBhIHBlZGlkbyBkb3MgQ2Vuc29yZXMsIGUgcXVlIHbhcmlhcyBvdXRyYXMgT2l0YXZhcyBmb3JhbSBvdSBhbHRlcmFkYXMsIG91IG1lc21vIGNvcnRhZGFzLCBwYXJhIHF1ZSBvIFBvZW1hIHB1ZGVzc2Ugc2VyIHB1YmxpY2Fkby4='; | ||
encode(WikipediaText1).should.equal(WikipediaText1based64); | ||
var WikipediaText2 = "Fanny Bullock Workman was an American geographer, cartographer, explorer, travel writer, and mountaineer, notably in the Himalaya. She was one of the first female professional mountaineers; she not only explored but also wrote about her adventures. She set several women's altitude records, published eight travel books with her husband, and championed women's rights and women's suffrage. Educated in the finest schools available to women, she was introduced to climbing in New Hampshire. She married William Hunter Workman, and traveled the world with him. The couple had two children, but left them in schools and with nurses. Workman saw herself as a New Woman who could equal any man. The Workmans wrote books about each trip and Workman frequently commented on the state of the lives of women that she saw. They explored several glaciers and conquered several mountains of the Himalaya, eventually reaching 23,000 feet (7,000 m), a women's altitude record at the time. Workman became the first woman to lecture at the Sorbonne and the second to speak at the Royal Geographical Society. She received many medals of honor and was recognized as one of the foremost climbers of her day."; | ||
var WikipediaText2based64 = 'RmFubnkgQnVsbG9jayBXb3JrbWFuIHdhcyBhbiBBbWVyaWNhbiBnZW9ncmFwaGVyLCBjYXJ0b2dyYXBoZXIsIGV4cGxvcmVyLCB0cmF2ZWwgd3JpdGVyLCBhbmQgbW91bnRhaW5lZXIsIG5vdGFibHkgaW4gdGhlIEhpbWFsYXlhLiBTaGUgd2FzIG9uZSBvZiB0aGUgZmlyc3QgZmVtYWxlIHByb2Zlc3Npb25hbCBtb3VudGFpbmVlcnM7IHNoZSBub3Qgb25seSBleHBsb3JlZCBidXQgYWxzbyB3cm90ZSBhYm91dCBoZXIgYWR2ZW50dXJlcy4gU2hlIHNldCBzZXZlcmFsIHdvbWVuJ3MgYWx0aXR1ZGUgcmVjb3JkcywgcHVibGlzaGVkIGVpZ2h0IHRyYXZlbCBib29rcyB3aXRoIGhlciBodXNiYW5kLCBhbmQgY2hhbXBpb25lZCB3b21lbidzIHJpZ2h0cyBhbmQgd29tZW4ncyBzdWZmcmFnZS4gRWR1Y2F0ZWQgaW4gdGhlIGZpbmVzdCBzY2hvb2xzIGF2YWlsYWJsZSB0byB3b21lbiwgc2hlIHdhcyBpbnRyb2R1Y2VkIHRvIGNsaW1iaW5nIGluIE5ldyBIYW1wc2hpcmUuIFNoZSBtYXJyaWVkIFdpbGxpYW0gSHVudGVyIFdvcmttYW4sIGFuZCB0cmF2ZWxlZCB0aGUgd29ybGQgd2l0aCBoaW0uIFRoZSBjb3VwbGUgaGFkIHR3byBjaGlsZHJlbiwgYnV0IGxlZnQgdGhlbSBpbiBzY2hvb2xzIGFuZCB3aXRoIG51cnNlcy4gV29ya21hbiBzYXcgaGVyc2VsZiBhcyBhIE5ldyBXb21hbiB3aG8gY291bGQgZXF1YWwgYW55IG1hbi4gVGhlIFdvcmttYW5zIHdyb3RlIGJvb2tzIGFib3V0IGVhY2ggdHJpcCBhbmQgV29ya21hbiBmcmVxdWVudGx5IGNvbW1lbnRlZCBvbiB0aGUgc3RhdGUgb2YgdGhlIGxpdmVzIG9mIHdvbWVuIHRoYXQgc2hlIHNhdy4gVGhleSBleHBsb3JlZCBzZXZlcmFsIGdsYWNpZXJzIGFuZCBjb25xdWVyZWQgc2V2ZXJhbCBtb3VudGFpbnMgb2YgdGhlIEhpbWFsYXlhLCBldmVudHVhbGx5IHJlYWNoaW5nIDIzLDAwMCBmZWV0ICg3LDAwMCBtKSwgYSB3b21lbidzIGFsdGl0dWRlIHJlY29yZCBhdCB0aGUgdGltZS4gV29ya21hbiBiZWNhbWUgdGhlIGZpcnN0IHdvbWFuIHRvIGxlY3R1cmUgYXQgdGhlIFNvcmJvbm5lIGFuZCB0aGUgc2Vjb25kIHRvIHNwZWFrIGF0IHRoZSBSb3lhbCBHZW9ncmFwaGljYWwgU29jaWV0eS4gU2hlIHJlY2VpdmVkIG1hbnkgbWVkYWxzIG9mIGhvbm9yIGFuZCB3YXMgcmVjb2duaXplZCBhcyBvbmUgb2YgdGhlIGZvcmVtb3N0IGNsaW1iZXJzIG9mIGhlciBkYXku'; | ||
encode(WikipediaText2).should.equal(WikipediaText2based64); | ||
encode('Isto é apenas um grande TESTE').should.equal('SXN0byDpIGFwZW5hcyB1bSBncmFuZGUgVEVTVEU='); | ||
it('Encode Long Texts', function () { | ||
//Based in http://www.motobit.com/util/base64-decoder-encoder.asp | ||
var WikipediaText1 = 'Não se pode pensar em heresia porque não fazia sentido, em tempos de Contra-Reforma, acreditar nos deuses do panteão greco-romano, e a prova é a não censura dos inquisidores aos «Deoses dos Gentios». No episódio da Máquina do Mundo (estrofe 82 do Canto X), é o próprio personagem da deusa Tétis que afirma: «eu, Saturno e Jano, Júpiter, Juno, fomos fabulosos, Fingidos de mortal e cego engano. Só pera fazer versos deleitosos Servimos». No entanto, críticos defendem que esta fala de Tétis foi introduzida a pedido dos Censores, e que várias outras Oitavas foram ou alteradas, ou mesmo cortadas, para que o Poema pudesse ser publicado.', | ||
WikipediaText1based64 = 'TuNvIHNlIHBvZGUgcGVuc2FyIGVtIGhlcmVzaWEgcG9ycXVlIG7jbyBmYXppYSBzZW50aWRvLCBlbSB0ZW1wb3MgZGUgQ29udHJhLVJlZm9ybWEsIGFjcmVkaXRhciBub3MgZGV1c2VzIGRvIHBhbnRl428gZ3JlY28tcm9tYW5vLCBlIGEgcHJvdmEg6SBhIG7jbyBjZW5zdXJhIGRvcyBpbnF1aXNpZG9yZXMgYW9zIKtEZW9zZXMgZG9zIEdlbnRpb3O7LiBObyBlcGlz82RpbyBkYSBN4XF1aW5hIGRvIE11bmRvIChlc3Ryb2ZlIDgyIGRvIENhbnRvIFgpLCDpIG8gcHLzcHJpbyBwZXJzb25hZ2VtIGRhIGRldXNhIFTpdGlzIHF1ZSBhZmlybWE6IKtldSwgU2F0dXJubyBlIEphbm8sIEr6cGl0ZXIsIEp1bm8sIGZvbW9zIGZhYnVsb3NvcywgRmluZ2lkb3MgZGUgbW9ydGFsIGUgY2VnbyBlbmdhbm8uIFPzIHBlcmEgZmF6ZXIgdmVyc29zIGRlbGVpdG9zb3MgU2Vydmltb3O7LiBObyBlbnRhbnRvLCBjcu10aWNvcyBkZWZlbmRlbSBxdWUgZXN0YSBmYWxhIGRlIFTpdGlzIGZvaSBpbnRyb2R1emlkYSBhIHBlZGlkbyBkb3MgQ2Vuc29yZXMsIGUgcXVlIHbhcmlhcyBvdXRyYXMgT2l0YXZhcyBmb3JhbSBvdSBhbHRlcmFkYXMsIG91IG1lc21vIGNvcnRhZGFzLCBwYXJhIHF1ZSBvIFBvZW1hIHB1ZGVzc2Ugc2VyIHB1YmxpY2Fkby4=', | ||
WikipediaText2 = "Fanny Bullock Workman was an American geographer, cartographer, explorer, travel writer, and mountaineer, notably in the Himalaya. She was one of the first female professional mountaineers; she not only explored but also wrote about her adventures. She set several women's altitude records, published eight travel books with her husband, and championed women's rights and women's suffrage. Educated in the finest schools available to women, she was introduced to climbing in New Hampshire. She married William Hunter Workman, and traveled the world with him. The couple had two children, but left them in schools and with nurses. Workman saw herself as a New Woman who could equal any man. The Workmans wrote books about each trip and Workman frequently commented on the state of the lives of women that she saw. They explored several glaciers and conquered several mountains of the Himalaya, eventually reaching 23,000 feet (7,000 m), a women's altitude record at the time. Workman became the first woman to lecture at the Sorbonne and the second to speak at the Royal Geographical Society. She received many medals of honor and was recognized as one of the foremost climbers of her day.", | ||
WikipediaText2based64 = 'RmFubnkgQnVsbG9jayBXb3JrbWFuIHdhcyBhbiBBbWVyaWNhbiBnZW9ncmFwaGVyLCBjYXJ0b2dyYXBoZXIsIGV4cGxvcmVyLCB0cmF2ZWwgd3JpdGVyLCBhbmQgbW91bnRhaW5lZXIsIG5vdGFibHkgaW4gdGhlIEhpbWFsYXlhLiBTaGUgd2FzIG9uZSBvZiB0aGUgZmlyc3QgZmVtYWxlIHByb2Zlc3Npb25hbCBtb3VudGFpbmVlcnM7IHNoZSBub3Qgb25seSBleHBsb3JlZCBidXQgYWxzbyB3cm90ZSBhYm91dCBoZXIgYWR2ZW50dXJlcy4gU2hlIHNldCBzZXZlcmFsIHdvbWVuJ3MgYWx0aXR1ZGUgcmVjb3JkcywgcHVibGlzaGVkIGVpZ2h0IHRyYXZlbCBib29rcyB3aXRoIGhlciBodXNiYW5kLCBhbmQgY2hhbXBpb25lZCB3b21lbidzIHJpZ2h0cyBhbmQgd29tZW4ncyBzdWZmcmFnZS4gRWR1Y2F0ZWQgaW4gdGhlIGZpbmVzdCBzY2hvb2xzIGF2YWlsYWJsZSB0byB3b21lbiwgc2hlIHdhcyBpbnRyb2R1Y2VkIHRvIGNsaW1iaW5nIGluIE5ldyBIYW1wc2hpcmUuIFNoZSBtYXJyaWVkIFdpbGxpYW0gSHVudGVyIFdvcmttYW4sIGFuZCB0cmF2ZWxlZCB0aGUgd29ybGQgd2l0aCBoaW0uIFRoZSBjb3VwbGUgaGFkIHR3byBjaGlsZHJlbiwgYnV0IGxlZnQgdGhlbSBpbiBzY2hvb2xzIGFuZCB3aXRoIG51cnNlcy4gV29ya21hbiBzYXcgaGVyc2VsZiBhcyBhIE5ldyBXb21hbiB3aG8gY291bGQgZXF1YWwgYW55IG1hbi4gVGhlIFdvcmttYW5zIHdyb3RlIGJvb2tzIGFib3V0IGVhY2ggdHJpcCBhbmQgV29ya21hbiBmcmVxdWVudGx5IGNvbW1lbnRlZCBvbiB0aGUgc3RhdGUgb2YgdGhlIGxpdmVzIG9mIHdvbWVuIHRoYXQgc2hlIHNhdy4gVGhleSBleHBsb3JlZCBzZXZlcmFsIGdsYWNpZXJzIGFuZCBjb25xdWVyZWQgc2V2ZXJhbCBtb3VudGFpbnMgb2YgdGhlIEhpbWFsYXlhLCBldmVudHVhbGx5IHJlYWNoaW5nIDIzLDAwMCBmZWV0ICg3LDAwMCBtKSwgYSB3b21lbidzIGFsdGl0dWRlIHJlY29yZCBhdCB0aGUgdGltZS4gV29ya21hbiBiZWNhbWUgdGhlIGZpcnN0IHdvbWFuIHRvIGxlY3R1cmUgYXQgdGhlIFNvcmJvbm5lIGFuZCB0aGUgc2Vjb25kIHRvIHNwZWFrIGF0IHRoZSBSb3lhbCBHZW9ncmFwaGljYWwgU29jaWV0eS4gU2hlIHJlY2VpdmVkIG1hbnkgbWVkYWxzIG9mIGhvbm9yIGFuZCB3YXMgcmVjb2duaXplZCBhcyBvbmUgb2YgdGhlIGZvcmVtb3N0IGNsaW1iZXJzIG9mIGhlciBkYXku'; | ||
encode(WikipediaText1).should.equal(WikipediaText1based64); | ||
encode(WikipediaText2).should.equal(WikipediaText2based64); | ||
encode('Isto é apenas um grande TESTE').should.equal('SXN0byDpIGFwZW5hcyB1bSBncmFuZGUgVEVTVEU='); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -0,35 +1,39 @@ | ||
/*jslint node:true*/ | ||
/*global describe, it*/ | ||
'use strict'; | ||
var should = require('chai').should(), | ||
base64 = require('../base64'); | ||
describe('# Encrypted Tests (Encoding and Decoding)', function(){ | ||
describe('# Encrypted Tests (Encoding and Decoding)', function () { | ||
var msg = '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.'; | ||
var msg = '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.'; | ||
it('Decode with correct Key', function() { | ||
var encodedKey = base64.encodeWithKey(msg, '--victor@fern91--'); | ||
base64.decodeWithKey(encodedKey, '--victor@fern91--').should.equal(msg); | ||
encodedKey = base64.encodeWithKey(msg, 'sjhakshdqio/%&/$%$&/""(!)=#=?)?="89087283'); | ||
base64.decodeWithKey(encodedKey, 'sjhakshdqio/%&/$%$&/""(!)=#=?)?="89087283').should.equal(msg); | ||
encodedKey = base64.encodeWithKey('any carnal pleasur', '12'); | ||
base64.decodeWithKey(encodedKey, '12').should.equal('any carnal pleasur'); | ||
encodedKey = base64.encodeWithKey('any carnal pleasure', '-..-.-.-.-.-...-..-.-as.-a.s-a.s-a.s'); | ||
base64.decodeWithKey(encodedKey, '-..-.-.-.-.-...-..-.-as.-a.s-a.s-a.s').should.equal('any carnal pleasure'); | ||
}); | ||
it('Decode with correct Key', function () { | ||
var encodedKey = base64.encodeWithKey(msg, '--victor@fern91--'); | ||
base64.decodeWithKey(encodedKey, '--victor@fern91--').should.equal(msg); | ||
encodedKey = base64.encodeWithKey(msg, 'sjhakshdqio/%&/$%$&/""(!)=#=?)?="89087283'); | ||
base64.decodeWithKey(encodedKey, 'sjhakshdqio/%&/$%$&/""(!)=#=?)?="89087283').should.equal(msg); | ||
encodedKey = base64.encodeWithKey('any carnal pleasur', '12'); | ||
base64.decodeWithKey(encodedKey, '12').should.equal('any carnal pleasur'); | ||
encodedKey = base64.encodeWithKey('any carnal pleasure', '-..-.-.-.-.-...-..-.-as.-a.s-a.s-a.s'); | ||
base64.decodeWithKey(encodedKey, '-..-.-.-.-.-...-..-.-as.-a.s-a.s-a.s').should.equal('any carnal pleasure'); | ||
}); | ||
it('Trying decode with incorrect Key', function() { | ||
var encodedKey = base64.encodeWithKey(msg, 'victorfern91'); | ||
base64.decodeWithKey(encodedKey, '--victor@fern91--').should.not.equal(msg); | ||
base64.decodeWithKey(encodedKey, 'base64-min').should.not.equal(msg); | ||
base64.decodeWithKey(encodedKey, 'victorfern').should.not.equal(msg); | ||
}); | ||
it('Trying decode with incorrect Key', function () { | ||
var encodedKey = base64.encodeWithKey(msg, 'victorfern91'); | ||
base64.decodeWithKey(encodedKey, '--victor@fern91--').should.not.equal(msg); | ||
base64.decodeWithKey(encodedKey, 'base64-min').should.not.equal(msg); | ||
base64.decodeWithKey(encodedKey, 'victorfern').should.not.equal(msg); | ||
}); | ||
it('Trying decode without key', function() { | ||
var encodedKey = base64.encodeWithKey(msg, '--victor@fern91--'); | ||
base64.decode(encodedKey).should.not.equal(msg); | ||
base64.encodeWithKey(msg, ''); | ||
base64.decode(encodedKey).should.not.equal(msg); | ||
base64.encodeWithKey(msg, 'kalshdjahskdhas'); | ||
base64.decode(encodedKey).should.not.equal(msg); | ||
}); | ||
it('Trying decode without key', function () { | ||
var encodedKey = base64.encodeWithKey(msg, '--victor@fern91--'); | ||
base64.decode(encodedKey).should.not.equal(msg); | ||
base64.encodeWithKey(msg, ''); | ||
base64.decode(encodedKey).should.not.equal(msg); | ||
base64.encodeWithKey(msg, 'kalshdjahskdhas'); | ||
base64.decode(encodedKey).should.not.equal(msg); | ||
}); | ||
}); | ||
}); |
@@ -0,18 +1,22 @@ | ||
/*jslint node:true*/ | ||
/*global describe, it*/ | ||
'use strict'; | ||
var should = require('chai').should(), | ||
base64 = require('../base64'); | ||
describe('# MIME encoding type tests', function(){ | ||
var msg = '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.'; | ||
var encodedMIME = 'TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\nIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg\ndGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu\ndWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo\nZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4='; | ||
// Test found in wikipedia base64 page (http://en.wikipedia.org/wiki/Base64#Examples) | ||
it('Encoding strings (based on http://en.wikipedia.org/wiki/Base64#Examples)', function() { | ||
base64.encode(msg, 'MIME').should.equal(encodedMIME); | ||
}); | ||
describe('# MIME encoding type tests', function () { | ||
var msg = '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.', | ||
encodedMIME = 'TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\nIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg\ndGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu\ndWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo\nZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4='; | ||
// Test found in wikipedia base64 page (http://en.wikipedia.org/wiki/Base64#Examples) | ||
it('Encoding strings (based on http://en.wikipedia.org/wiki/Base64#Examples)', function () { | ||
base64.encode(msg, 'MIME').should.equal(encodedMIME); | ||
}); | ||
it('Decoding strings (based on http://en.wikipedia.org/wiki/Base64#Examples)', function() { | ||
base64.decode(encodedMIME).should.equal(msg); | ||
}); | ||
}); | ||
it('Decoding strings (based on http://en.wikipedia.org/wiki/Base64#Examples)', function () { | ||
base64.decode(encodedMIME).should.equal(msg); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
162
36551
11
444
6