* @returns {String}
*/
const base64ToBase64URL = (input) => {
+ if (!input) {
+ return input;
+ }
return input
.replace(/=/g, '')
.replace(/\+/g, '-')
* @returns {String}
*/
const base64URLToBase64 = (input) => {
+ if (!input) {
+ return input;
+ }
return base64RePad(input)
.replace(/-/g, '+')
.replace(/_/, '/');
end: 0,
};
+ if (!box) {
+ throw new RangeError('nothing to unpack');
+ }
+
const raw = Buffer.from(common.base64URLToBase64(box), 'base64');
let offset = 0;
const v = this.versionParameters[version];
offset += v.versionBytes;
+ const minBytes = v.versionBytes + v.flagsBytes + v.ivBytes + v.saltBytes + v.tagBytes;
+ if (raw.length < minBytes) {
+ throw new RangeError('not enough to unpack');
+ }
+
const flags = raw.slice(offset, offset + v.flagsBytes).readUInt8(0);
offset += v.flagsBytes;
const result = common.base64ToBase64URL(b64);
assert.strictEqual(result, expected);
});
+ it('covers empty', function () {
+ const result = common.base64ToBase64URL(undefined);
+ assert.strictEqual(result, undefined);
+ });
}); // base64ToBase64URL
describe('base64URLToBase64', function () {
const result = common.base64URLToBase64(b64url);
assert.strictEqual(result, expected);
});
+ it('covers empty', function () {
+ const result = common.base64URLToBase64(undefined);
+ assert.strictEqual(result, undefined);
+ });
}); // base64URLToBase64
describe('base64RePad', function () {
const decryptedResult = await mb.unpack(encryptedResult);
assert.deepStrictEqual(decryptedResult, object);
});
+
+ it('handles undefined', async function () {
+ try {
+ await mb.unpack();
+ assert.fail(noExpectedException);
+ } catch (e) {
+ assert(e instanceof RangeError, noExpectedException);
+ }
+ });
+
+ it('handles incomplete', async function () {
+ const encryptedResult = await mb.pack({ foo: 'bar' });
+ try {
+ await mb.unpack(encryptedResult.slice(0, 6));
+ assert.fail(noExpectedException);
+ } catch (e) {
+ assert(e instanceof RangeError, noExpectedException);
+ }
+ });
+
}); // pack, unpack
}); // MysteryBox
\ No newline at end of file