Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix "Cannot decode base64" when encoded without padding #16

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions angular-base64.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,18 @@
function decode(s) {
// convert to string
s = "" + s;
var pads, i, b10;
var imax = s.length;
var pads, i, b10, imax;

if ((s.length % 4) === 3) {
s = s + PADCHAR;
}

if ((s.length % 4) === 2) {
s = s + PADCHAR + PADCHAR;
}

imax = s.length;

if (imax === 0) {
return s;
}
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
"grunt-contrib-uglify": "",
"grunt-notify": "",
"grunt-strip": "",
"grunt-text-replace": ""
"grunt-text-replace": "",
"tape": "^4.6.3"
},
"name": "@ninjatronic/angular-base64",
"description": "Encapsulation of Nick Galbreath's base64.js library for AngularJS",
"version": "0.0.2",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "node tests"
},
"repository": {
"type": "git",
Expand Down
50 changes: 50 additions & 0 deletions tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
var test = require('tape');

var constants = {};

global.angular = {
module: function (moduleName, deps) {
return {
constant: function (constantName, value) { constants[constantName] = value; }
}
}
}

var b64 = require('./angular-base64.js');

test('constant registered', function (t) {
t.plan(1);

t.notEqual(undefined, constants.$base64);
})

test('can encode and decode non-padded', function (t) {
t.plan(2);

var text = "The quick brown fox jumps over the lazy doges";
var b64 = "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZ2Vz";

t.equal(b64, constants.$base64.encode(text));
t.equal(text, constants.$base64.decode(b64));
})

test('can encode and decode padded', function (t) {
t.plan(2);

var text = "The quick brown fox jumps over the lazy dog";
var b64 = "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw==";

console.log(constants.$base64.encode(text))

t.equal(b64, constants.$base64.encode(text));
t.equal(text, constants.$base64.decode(b64));
})

test('can decode with padding missing', function (t) {
t.plan(1);

var text = "The quick brown fox jumps over the lazy dog";
var b64 = "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw";

t.equal(text, constants.$base64.decode(b64));
})