From dc3af230948e3d99898ec5e5e7e0e9d38dc3cda8 Mon Sep 17 00:00:00 2001 From: ravenscar Date: Fri, 13 Nov 2015 11:28:10 +1100 Subject: [PATCH 1/4] Update angular-base64.js Automatically pad out decode string size with '=' or '==' for compatibility with encoders which do not automatically add padding. See: https://github.com/ninjatronic/angular-base64/issues/13 --- angular-base64.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/angular-base64.js b/angular-base64.js index 4a42f60..0bca2e7 100644 --- a/angular-base64.js +++ b/angular-base64.js @@ -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) === 1) { + s = s + PADCHAR; + } + + if ((s.length % 4) === 2) { + s = s + PADCHAR + PADCHAR; + } + + imax = s.length; + if (imax == 0) { return s; } From 6c784c5522e3250807660f44c7a6c34a8c3e8d1f Mon Sep 17 00:00:00 2001 From: ravenscar Date: Fri, 13 Nov 2015 11:31:22 +1100 Subject: [PATCH 2/4] Update angular-base64.js fix typo of length from 1 to 3 --- angular-base64.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/angular-base64.js b/angular-base64.js index 0bca2e7..162fa4c 100644 --- a/angular-base64.js +++ b/angular-base64.js @@ -73,7 +73,7 @@ s = "" + s; var pads, i, b10, imax; - if ((s.length % 4) === 1) { + if ((s.length % 4) === 3) { s = s + PADCHAR; } From 32692362ead9591854747d8af97a5ee79b0a3425 Mon Sep 17 00:00:00 2001 From: Paul Nilsson Date: Mon, 16 Jan 2017 14:51:52 +1100 Subject: [PATCH 3/4] add tape and basic tests for encode/decode --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 315ec19..bc6d547 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,8 @@ "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", From 11631870af3c10bbab52db2fbaa758949b49026d Mon Sep 17 00:00:00 2001 From: Paul Nilsson Date: Mon, 16 Jan 2017 14:54:57 +1100 Subject: [PATCH 4/4] add tape and basic tests for encode/decode --- package.json | 2 +- tests.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 tests.js diff --git a/package.json b/package.json index bc6d547..d2893d5 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "version": "0.0.2", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "node tests" }, "repository": { "type": "git", diff --git a/tests.js b/tests.js new file mode 100644 index 0000000..4e58c27 --- /dev/null +++ b/tests.js @@ -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)); +})