From 595e2b66a87e0ac40566ab40d131fb930cb9624c Mon Sep 17 00:00:00 2001 From: Simon Vareille Date: Mon, 17 Feb 2020 17:27:17 +0100 Subject: [PATCH] Update base64 encoding to use Buffer class --- src/service/public-key.js | 9 ++-- src/service/util.js | 94 --------------------------------------- 2 files changed, 5 insertions(+), 98 deletions(-) diff --git a/src/service/public-key.js b/src/service/public-key.js index b5f921f..6ef7415 100644 --- a/src/service/public-key.js +++ b/src/service/public-key.js @@ -196,10 +196,11 @@ class PublicKey { async _formatArrays(signatures) { signatures.map(function(sig) { const signature = sig.signature; - signature.signatureData = util.base64EncArr(signature.signatureData); - signature.signedHashValue = util.base64EncArr(signature.signedHashValue); - signature.issuerFingerprint = util.base64EncArr(signature.issuerFingerprint); - signature.signature = util.base64EncArr(signature.signature); + const attributes = ['signatureData', 'unhashedSubpackets', 'signedHashValue', 'issuerFingerprint', 'signature', ]; + for (const attrib of attributes) { + if(signature[attrib] != null) + signature[attrib] = Buffer.from(signature[attrib]).toString('base64'); + } }); } diff --git a/src/service/util.js b/src/service/util.js index fb6beee..3f38a52 100644 --- a/src/service/util.js +++ b/src/service/util.js @@ -125,100 +125,6 @@ exports.equalsUint8Array = function (array1, array2) { return true; }; -/** - * Decode a base64 byte to uint6 - * @param {Integer} nChr base64 byte to decode - * @returns {Integer} the uint6 integer - * Comes from https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding - * All thanks to madmurphy - */ -function b64ToUint6 (nChr) { - return nChr > 64 && nChr < 91 ? - nChr - 65 - : nChr > 96 && nChr < 123 ? - nChr - 71 - : nChr > 47 && nChr < 58 ? - nChr + 4 - : nChr === 43 ? - 62 - : nChr === 47 ? - 63 - : - 0; -}; - -/** - * Decode a base64 String to Uint8Array - * @param {String} sBase64 base64 string to decode - * @returns {Uint8Array} decoded data - * Comes from https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding - * All thanks to madmurphy - */ -exports.base64DecToArr = function (sBase64) { - var sB64Enc = sBase64.replace(/[^A-Za-z0-9\+\/]/g, ""), nInLen = sB64Enc.length, - nOutLen = nInLen * 3 + 1 >> 2, taBytes = new Uint8Array(nOutLen); - - for (var nMod3, nMod4, nUint24 = 0, nOutIdx = 0, nInIdx = 0; nInIdx < nInLen; nInIdx++) { - nMod4 = nInIdx & 3; - nUint24 |= b64ToUint6(sB64Enc.charCodeAt(nInIdx)) << 18 - 6 * nMod4; - if (nMod4 === 3 || nInLen - nInIdx === 1) { - for (nMod3 = 0; nMod3 < 3 && nOutIdx < nOutLen; nMod3++, nOutIdx++) { - taBytes[nOutIdx] = nUint24 >>> (16 >>> nMod3 & 24) & 255; - } - nUint24 = 0; - } - } - return taBytes; -}; - -/** - * Encode a uint6 to base64 byte - * @param {Integer} nUint6 integer to encode - * @returns {Integer} the base64 byte - * Comes from https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding - * All thanks to madmurphy - */ -function uint6ToB64 (nUint6) { - return nUint6 < 26 ? - nUint6 + 65 - : nUint6 < 52 ? - nUint6 + 71 - : nUint6 < 62 ? - nUint6 - 4 - : nUint6 === 62 ? - 43 - : nUint6 === 63 ? - 47 - : - 65; -}; - - -/** - * Encode a Uint8Array to base64 - * @param {Uint8Array} aBytes array to encode - * @returns {String} base64 String - * Comes from https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding - * All thanks to madmurphy - */ -exports.base64EncArr = function (aBytes) { - var eqLen = (3 - (aBytes.length % 3)) % 3, sB64Enc = ""; - for (var nMod3, nLen = aBytes.length, nUint24 = 0, nIdx = 0; nIdx < nLen; nIdx++) { - nMod3 = nIdx % 3; - /* Uncomment the following line in order to split the output in lines 76-character long: */ - /* - if (nIdx > 0 && (nIdx * 4 / 3) % 76 === 0) { sB64Enc += "\r\n"; } - */ - nUint24 |= aBytes[nIdx] << (16 >>> nMod3 & 24); - if (nMod3 === 2 || aBytes.length - nIdx === 1) { - sB64Enc += String.fromCharCode(uint6ToB64(nUint24 >>> 18 & 63), uint6ToB64(nUint24 >>> 12 & 63), uint6ToB64(nUint24 >>> 6 & 63), uint6ToB64(nUint24 & 63)); - nUint24 = 0; - } - } - return eqLen === 0 ? sB64Enc : sB64Enc.substring(0, sB64Enc.length - eqLen) + (eqLen === 1 ? "=" : "=="); -}; - - /** * Create an error with a custom status attribute e.g. for http codes. * @param {number} status The error's http status code