From: Justin Wind Date: Wed, 6 Apr 2022 17:00:02 +0000 (-0700) Subject: split b64u functions into separate package X-Git-Tag: v1.0.6~1 X-Git-Url: https://git.squeep.com/?a=commitdiff_plain;h=b9782b5dfca21a6da610eebf8bd7207f10256fbf;p=squeep-mystery-box split b64u functions into separate package --- diff --git a/lib/common.js b/lib/common.js index a47226f..4c7e639 100644 --- a/lib/common.js +++ b/lib/common.js @@ -19,56 +19,7 @@ const fileScope = (filename) => { } -/** - * Convert Base64 to Base64URL. - * @param {String} input - * @returns {String} - */ -const base64ToBase64URL = (input) => { - if (!input) { - return input; - } - return input - .replace(/=/g, '') - .replace(/\+/g, '-') - .replace(/\//g, '_'); -}; - - -/** - * Convert Base64URL to normal Base64. - * @param {String} input - * @returns {String} - */ -const base64URLToBase64 = (input) => { - if (!input) { - return input; - } - return base64RePad(input) - .replace(/-/g, '+') - .replace(/_/, '/'); -}; - - -/** - * Add any missing trailing padding which may have been removed from Base64URL encoding. - * @param {String} input - */ -const base64RePad = (input) => { - const blockSize = 4; - const lastBlockSize = input.length % blockSize; - if (lastBlockSize) { - const missing = blockSize - lastBlockSize; - return input + '='.repeat(missing); - } - return input; -}; - - module.exports = { - base64ToBase64URL, - base64URLToBase64, - base64RePad, fileScope, randomBytesAsync, }; \ No newline at end of file diff --git a/lib/mystery-box.js b/lib/mystery-box.js index 6198cc8..2343c5e 100644 --- a/lib/mystery-box.js +++ b/lib/mystery-box.js @@ -3,6 +3,7 @@ const crypto = require('crypto'); const zlib = require('zlib'); const { promisify } = require('util'); +const { base64ToBase64URL, base64URLToBase64 } = require('@squeep/base64url'); const common = require('./common'); const allVersions = require('./version-parameters'); const { performance } = require('perf_hooks'); @@ -183,7 +184,7 @@ class MysteryBox { const tag = cipher.getAuthTag(); const merged = Buffer.concat([versionBuffer, flagsBuffer, iv, salt, tag, encrypted, final]).toString('base64'); - const result = common.base64ToBase64URL(merged); + const result = base64ToBase64URL(merged); timingsMs.end = timingsMs.postCrypt = performance.now(); this.logger.debug(_scope, 'statistics', { version, flags: this._prettyFlags(flags), serialized: contents.length, compressed: payload.length, encoded: result.length, ...MysteryBox._timingsLog(timingsMs) }); @@ -212,7 +213,7 @@ class MysteryBox { throw new RangeError('nothing to unpack'); } - const raw = Buffer.from(common.base64URLToBase64(box), 'base64'); + const raw = Buffer.from(base64URLToBase64(box), 'base64'); let offset = 0; const version = raw.slice(offset, 1).readUInt8(0); diff --git a/package-lock.json b/package-lock.json index ad26d98..f2716f8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -428,6 +428,10 @@ "integrity": "sha512-+iTbntw2IZPb/anVDbypzfQa+ay64MW0Zo8aJ8gZPWMMK6/OubMVb6lUPMagqjOPnmtauXnFCACVl3O7ogjeqQ==", "dev": true }, + "@squeep/base64url": { + "version": "git+https://git.squeep.com/squeep-base64url#5788dd9a225eb1898d624291e50a86f9800866a7", + "from": "git+https://git.squeep.com/squeep-base64url#v1.0.0" + }, "@ungap/promise-all-settled": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/@ungap/promise-all-settled/-/promise-all-settled-1.1.2.tgz", @@ -604,9 +608,9 @@ "dev": true }, "caniuse-lite": { - "version": "1.0.30001265", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001265.tgz", - "integrity": "sha512-YzBnspggWV5hep1m9Z6sZVLOt7vrju8xWooFAgN6BA5qvy98qPAPb7vNUzypFaoh2pb3vlfzbDO8tB57UPGbtw==", + "version": "1.0.30001325", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001325.tgz", + "integrity": "sha512-sB1bZHjseSjDtijV1Hb7PB2Zd58Kyx+n/9EotvZ4Qcz2K3d0lWB8dB4nb8wN/TsOGFq3UuAm0zQZNQ4SoR7TrQ==", "dev": true }, "chalk": { @@ -962,9 +966,9 @@ } }, "eslint-plugin-sonarjs": { - "version": "0.12.0", - "resolved": "https://registry.npmjs.org/eslint-plugin-sonarjs/-/eslint-plugin-sonarjs-0.12.0.tgz", - "integrity": "sha512-soxjK67hoYxO8hesKqXWN50GSM+oG2r35N5WnAMehetahO6zoVpv3HZbdziP0jYWNopEe6te/BFUZOYAZI+qhg==", + "version": "0.13.0", + "resolved": "https://registry.npmjs.org/eslint-plugin-sonarjs/-/eslint-plugin-sonarjs-0.13.0.tgz", + "integrity": "sha512-t3m7ta0EspzDxSOZh3cEOJIJVZgN/TlJYaBGnQlK6W/PZNbWep8q4RQskkJkA7/zwNpX0BaoEOSUUrqaADVoqA==", "dev": true }, "eslint-scope": { diff --git a/package.json b/package.json index 34a9188..0bc9798 100644 --- a/package.json +++ b/package.json @@ -29,10 +29,13 @@ "eslint": "^8.12.0", "eslint-plugin-node": "^11.1.0", "eslint-plugin-security": "^1.4.0", - "eslint-plugin-sonarjs": "^0.12.0", + "eslint-plugin-sonarjs": "^0.13.0", "mocha": "^9.2.2", "nyc": "^15.1.0", "pre-commit": "^1.2.2", "sinon": "^13.0.1" + }, + "dependencies": { + "@squeep/base64url": "git+https://git.squeep.com/squeep-base64url#v1.0.0" } } diff --git a/test/lib/common.js b/test/lib/common.js index f5df10c..344e162 100644 --- a/test/lib/common.js +++ b/test/lib/common.js @@ -19,45 +19,4 @@ describe('Common', function () { }); }); // fileScope - describe('base64ToBase64URL', function () { - it('covers', function () { - const b64 = '/+=='; - const expected = '_-'; - 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 () { - it('covers', function () { - const b64url = '_-'; - const expected = '/+=='; - 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 () { - it('covers', function () { - const b64short = 'af'; - const expected = 'af=='; - const result = common.base64RePad(b64short); - assert.strictEqual(result, expected); - }); - it('covers padded', function () { - const b64 = 'afd4'; - const expected = b64; - const result = common.base64RePad(b64); - assert.strictEqual(result, expected); - }); - }); // base64RePad - }); // Common