Skip to content
This repository was archived by the owner on Aug 24, 2021. It is now read-only.

Commit 84f79ba

Browse files
Alan Shawbeenotung
authored andcommitted
fix: base64url encodes multiple occurances of + and / (#39)
* fixed bug for base64url * removed error message in src/base64.js * fix: remove unnecessary decode replacements and add tests `Buffer.from('[base64url encoded str]', 'base64')` deals with `-` and `_` in a base64url encoded string so no need to do replacements prior to it. Added tests for `base64url` and `base64urlpad` to ensure multiple instances of `+` and `/` are replaced with `-` and `_` appropriately. closes #35 License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai> Co-Authored-By: beenotung <aabbcc1241@yahoo.com.hk>
1 parent 1ae01a0 commit 84f79ba

File tree

4 files changed

+7
-8
lines changed

4 files changed

+7
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ lib-cov
1818

1919
# Coverage directory used by tools like istanbul
2020
coverage
21+
.nyc_output
2122

2223
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
2324
.grunt

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ lib-cov
1818

1919
# Coverage directory used by tools like istanbul
2020
coverage
21+
.nyc_output
2122

2223
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
2324
.grunt

src/base64.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ module.exports = function base64 (alphabet) {
2020
}
2121

2222
if (url) {
23-
output = output.replace('+', '-')
24-
output = output.replace('/', '_')
23+
output = output.replace(/\+/g, '-').replace(/\//g, '_')
2524
}
2625

2726
const pad = output.indexOf('=')
@@ -32,11 +31,6 @@ module.exports = function base64 (alphabet) {
3231
return output
3332
},
3433
decode (input) {
35-
if (url) {
36-
input = input.replace('+', '-')
37-
input = input.replace('/', '_')
38-
}
39-
4034
for (let char of input) {
4135
if (alphabet.indexOf(char) < 0) {
4236
throw new Error('invalid base64 character')

test/multibase.spec.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ const supportedBases = [
7070
['base64', 'foob', 'mZm9vYg'],
7171
['base64', 'fooba', 'mZm9vYmE'],
7272
['base64', 'foobar', 'mZm9vYmFy'],
73+
['base64', '÷ïÿ🥰÷ïÿ😎🥶🤯', 'mw7fDr8O/8J+lsMO3w6/Dv/CfmI7wn6W28J+krw'],
7374

7475
['base64pad', 'f', 'MZg=='],
7576
['base64pad', 'fo', 'MZm8='],
@@ -79,13 +80,15 @@ const supportedBases = [
7980
['base64pad', 'foobar', 'MZm9vYmFy'],
8081

8182
['base64url', '÷ïÿ', 'uw7fDr8O_'],
83+
['base64url', '÷ïÿ🥰÷ïÿ😎🥶🤯', 'uw7fDr8O_8J-lsMO3w6_Dv_CfmI7wn6W28J-krw'],
8284

8385
['base64urlpad', 'f', 'UZg=='],
8486
['base64urlpad', 'fo', 'UZm8='],
8587
['base64urlpad', 'foo', 'UZm9v'],
8688
['base64urlpad', 'foob', 'UZm9vYg=='],
8789
['base64urlpad', 'fooba', 'UZm9vYmE='],
88-
['base64urlpad', 'foobar', 'UZm9vYmFy']
90+
['base64urlpad', 'foobar', 'UZm9vYmFy'],
91+
['base64urlpad', '÷ïÿ🥰÷ïÿ😎🥶🤯', 'Uw7fDr8O_8J-lsMO3w6_Dv_CfmI7wn6W28J-krw==']
8992
]
9093

9194
describe('multibase', () => {

0 commit comments

Comments
 (0)