diff --git a/CHANGELOG.fr.md b/CHANGELOG.fr.md index 1478b214cfbac11a0a57da152a214ee12c83ee9a..3d38e64950392c844444cf5cc7f276733dfad674 100644 --- a/CHANGELOG.fr.md +++ b/CHANGELOG.fr.md @@ -13,9 +13,12 @@ et ce projet adhère au [versionnage sémantique](https://semver.org/spec/v2.0.0 ## [Non-publié/Non-Stabilisé] (par [1000i100]) +## [Version 3.2.0] - 2022-01-31 (par [1000i100]) ### Ajouté - crypto.checkKey(pubKeyWithChecksum) -- crypto.pubKey2checksum(b58pubKey, optionalBool:b58viewDependant, optionalBool:checksumWithoutLeadingZero) +- EXPERIMENTAL : crypto.pubKey2checksum(b58pubKey, optionalBool:b58viewDependant, optionalBool:checksumWithoutLeadingZero) ATTENTION, la syntaxe pourrait changer selon ce qui est choisi comme algo de checksum recommandé dans les spec. +### Corrections +- correction des encodage et décodage base64 désormais conforme à la RFC 4648 §4 (standard le plus répandu) ## [Version 3.1.0] - 2021-04-01 (par [1000i100] & [Hugo]) ### Ajouté @@ -71,8 +74,9 @@ et ce projet adhère au [versionnage sémantique](https://semver.org/spec/v2.0.0 - intégration des librairies de crypto nécessaires - calcul de la clef publique correspondant à chaque combinaison de secrets saisie, et comparaison à la clef publique de référence. -[Non-publié/Non-Stabilisé]: https://git.duniter.org/libs/g1lib.js/-/compare/v3.1.0...master +[Non-publié/Non-Stabilisé]: https://git.duniter.org/libs/g1lib.js/-/compare/v3.2.0...master +[Version 3.2.0]: https://git.duniter.org/libs/g1lib.js/-/compare/v3.1.0...v3.2.0 [Version 3.1.0]: https://git.duniter.org/libs/g1lib.js/-/compare/v3.0.3...v3.1.0 [Version 3.0.2]: https://git.duniter.org/libs/g1lib.js/-/compare/v3.0.1...v3.0.2 [Version 3.0.1]: https://git.duniter.org/libs/g1lib.js/-/compare/v3.0.0...v3.0.1 diff --git a/npm/package.json b/npm/package.json index e8f817dca4f77dcdf8dc4e30951674e6731ca194..e0b0e9dbb88ed89c4646fdbf6db20fb8f8f14022 100644 --- a/npm/package.json +++ b/npm/package.json @@ -1,6 +1,6 @@ { "name": "g1lib", - "version": "3.1.0", + "version": "3.1.1", "description": "An ubiquitous static javascript toolbox lib for Ǧ1 / Duniter ecosystem with reliability in mind.", "main": "nodejs/all.mjs", "browser": "browser/all.mjs", diff --git a/src/basex.mjs b/src/basex.mjs index 0d082390549c045a7472ab39004a5b3b6d4641eb..6569c9ffda95f130787222325158bc5152c4930e 100644 --- a/src/basex.mjs +++ b/src/basex.mjs @@ -9,20 +9,22 @@ export default basex; const _b64 = basex(B64_ALPHABET); export const b64 = { encode: source => { - const size = Math.ceil(source.length / 3) * 3; - const sizedArray = new Uint8Array(size); - - if (typeof source === 'string') sizedArray.set((new TextEncoder()).encode(source)); - else sizedArray.set(source); - + const bSource = (typeof source === 'string') ? (new TextEncoder()).encode(source) : new Uint8Array(source); + const paddedSize = Math.ceil(bSource.length / 3) * 3; + const sizedArray = new Uint8Array(paddedSize); + sizedArray.set(bSource); const b64str = _b64.encode(sizedArray).split(''); - for (let i = 0; i < size - source.length; i++) b64str[b64str.length - 1 - i] = '='; + const b64PaddedSize = paddedSize*4/3; + while (b64str.length < b64PaddedSize) b64str.unshift(B64_ALPHABET[0]); + for (let i = 0; i < paddedSize - bSource.length; i++) b64str[b64str.length - 1 - i] = '='; return b64str.join(''); }, decode: b64str => { const rawArray = _b64.decode(b64str.replace(/=/g, 'A')); - const targetSize = Math.trunc(3 * b64str.length / 4 - (b64str.length - b64str.replace(/=/g, '').length)); - return rawArray.slice(0, targetSize); + const targetSize = Math.ceil(3 * b64str.length / 4); + const postCut = (b64str.match(/=/g)||[]).length; + const preCut = rawArray.length - targetSize; + return rawArray.slice(preCut, rawArray.length-postCut); } }; diff --git a/src/basex.test.mjs b/src/basex.test.mjs index 9befbb437e2fd33bcf5e291e6ab249b3088680e9..3e40014f950f11d89eb0a86bee49b472fbb159e1 100644 --- a/src/basex.test.mjs +++ b/src/basex.test.mjs @@ -22,3 +22,14 @@ test('b64 should decode TWE= as Ma', t => t.is((new TextDecoder()).decode(app.b6 // Won't fix test('b64 should decode TWE as Ma', t => t.is((new TextDecoder()).decode(app.b64.decode('TWE')), 'Ma')); test('b64 should decode TQ== as M', t => t.is((new TextDecoder()).decode(app.b64.decode('TQ==')), 'M')); // Won't fix test('b64 should decode TQ as M', t => t.is((new TextDecoder()).decode(app.b64.decode('TQ')), 'M')); + + +test('[0,5,5] should be encoded as AAUF', t => t.is(app.b64.encode([0,5,5]), 'AAUF')); +test('AAUF should be decoded as [0,5,5]', t => t.deepEqual(app.b64.decode('AAUF'), new Uint8Array([0,5,5]))); +test('[0,1] should be encoded as AAE=', t => t.is(app.b64.encode([0,1]), 'AAE=')); +test('AAE= should be decoded as [0,1]', t => t.deepEqual(app.b64.decode('AAE='), new Uint8Array([0,1]))); + + +//const unstableExample = 'AUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBVCmFAmx3dAyXpsWtwDnGel3LAcACxvXeG6QfGU9IEld9WwlvkDttnDTPgQL6TQkiMl0SCo='; +const unstableExample = 'AAUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBVCmFAmx3dAyXpsWtwDnGel3LAcACxvXeG6QfGU9IEld9WwlvkDttnDTPgQL6TQkiMl0SCo='; +test('b64 should be stable in decoding encoding process even on long string', t => t.is(app.b64.encode(app.b64.decode(unstableExample)), unstableExample));