Skip to content
Merged
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
20 changes: 20 additions & 0 deletions src/util/base64/__tests__/decode-base64url.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {toBase64} from '../toBase64';
import {fromBase64Url} from '../fromBase64Url';

const generateBlob = (): Uint8Array => {
const length = Math.floor(Math.random() * 100);
const uint8 = new Uint8Array(length);
for (let i = 0; i < length; i++) {
uint8[i] = Math.floor(Math.random() * 256);
}
return uint8;
};

test('works', () => {
for (let i = 0; i < 100; i++) {
const blob = generateBlob();
const encoded = toBase64(blob).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
const decoded2 = fromBase64Url(encoded);
expect(decoded2).toEqual(blob);
}
});
23 changes: 23 additions & 0 deletions src/util/base64/__tests__/encode-base64url.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {toBase64Url} from '../toBase64Url';

const generateBlob = (): Uint8Array => {
const length = Math.floor(Math.random() * 100) + 1;
const uint8 = new Uint8Array(length);
for (let i = 0; i < length; i++) {
uint8[i] = Math.floor(Math.random() * 256);
}
return uint8;
};

test('works', () => {
for (let i = 0; i < 100; i++) {
const blob = generateBlob();
const expected = Buffer.from(blob).toString('base64');
const base64url = toBase64Url(blob, blob.length);
let encoded = base64url.replace(/-/g, '+').replace(/_/g, '/');
const mod = encoded.length % 4;
if (mod === 2) encoded += '==';
else if (mod === 3) encoded += '=';
expect(encoded).toEqual(expected);
}
});
14 changes: 12 additions & 2 deletions src/util/base64/createFromBase64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {alphabet} from './constants';

const E = '=';

export const createFromBase64 = (chars: string = alphabet) => {
export const createFromBase64 = (chars: string = alphabet, noPadding: boolean = false) => {
if (chars.length !== 64) throw new Error('chars must be 64 characters long');
let max = 0;
for (let i = 0; i < chars.length; i++) max = Math.max(max, chars.charCodeAt(i));
Expand All @@ -12,7 +12,17 @@ export const createFromBase64 = (chars: string = alphabet) => {

return (encoded: string): Uint8Array => {
if (!encoded) return new Uint8Array(0);
const length = encoded.length;
let length = encoded.length;
if (noPadding) {
const mod = length % 4;
if (mod === 2) {
encoded += '==';
length += 2;
} else if (mod === 3) {
encoded += '=';
length += 1;
}
}
if (length % 4 !== 0) throw new Error('Base64 string length must be a multiple of 4');
const mainLength = encoded[length - 1] !== E ? length : length - 4;
let bufferLength = (length >> 2) * 3;
Expand Down
5 changes: 1 addition & 4 deletions src/util/base64/createToBase64.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import {alphabet} from './constants';

const E = '=';
const EE = '==';

export const createToBase64 = (chars: string = alphabet) => {
export const createToBase64 = (chars: string = alphabet, E: string = '=', EE: string = '==') => {
if (chars.length !== 64) throw new Error('chars must be 64 characters long');

const table = chars.split('');
Expand Down
3 changes: 3 additions & 0 deletions src/util/base64/fromBase64Url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {createFromBase64} from './createFromBase64';

export const fromBase64Url = createFromBase64('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_', true);
3 changes: 3 additions & 0 deletions src/util/base64/toBase64Url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {createToBase64} from './createToBase64';

export const toBase64Url = createToBase64('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_', '', '');