From 75aa7ba2c7514211612a9f85d9fe7003c1584817 Mon Sep 17 00:00:00 2001 From: Jaco Date: Sat, 27 Nov 2021 07:05:34 +0100 Subject: [PATCH] Tiny base64 decoder (no Buffer dep) (#1251) * Tiny base64 decoder (no Buffer dep) * CHANGELOG --- CHANGELOG.md | 7 +++ .../util-crypto/src/base64/decode.spec.ts | 18 ++++++- packages/x-randomvalues/src/base64.spec.ts | 48 +++++++++++++++++++ packages/x-randomvalues/src/base64.ts | 26 ++++++++++ packages/x-randomvalues/src/react-native.ts | 5 +- 5 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 packages/x-randomvalues/src/base64.spec.ts create mode 100644 packages/x-randomvalues/src/base64.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 3bf5c4763..7ef4afa0c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # CHANGELOG +## master + +Changes: + +- Remove `Buffer` usage in `x-randomvalues` + + ## 8.0.2 Changes: diff --git a/packages/util-crypto/src/base64/decode.spec.ts b/packages/util-crypto/src/base64/decode.spec.ts index 8535eb2a9..7d273968b 100644 --- a/packages/util-crypto/src/base64/decode.spec.ts +++ b/packages/util-crypto/src/base64/decode.spec.ts @@ -6,11 +6,27 @@ import { stringToU8a } from '@polkadot/util'; import { base64Decode } from '.'; describe('base64Decode', (): void => { - it('decodes a mixed base64 utf8 string', (): void => { + it('decodes a mixed base64 utf8 string (1)', (): void => { expect( base64Decode('aGVsbG8gd29ybGQg0J/RgNC40LLQtdGC0YHRgtCy0YPRjiDQvNC4IOS9oOWlvQ==') ).toEqual( stringToU8a('hello world Приветствую ми 你好') ); }); + + it('decodes a mixed base64 utf8 string (2)', (): void => { + expect( + base64Decode('4pyTIMOgIGxhIG1vZGU=') + ).toEqual( + stringToU8a('✓ à la mode') + ); + }); + + it('decodes a mixed base64 utf8 string (3)', (): void => { + expect( + base64Decode('SGVsbG8gV29ybGQh') + ).toEqual( + stringToU8a('Hello World!') + ); + }); }); diff --git a/packages/x-randomvalues/src/base64.spec.ts b/packages/x-randomvalues/src/base64.spec.ts new file mode 100644 index 000000000..e21ba8f13 --- /dev/null +++ b/packages/x-randomvalues/src/base64.spec.ts @@ -0,0 +1,48 @@ +// Copyright 2017-2021 @polkadot/util-crypto authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import { base64Decode } from './base64'; + +describe('base64Decode', (): void => { + it('decodes a mixed base64 utf8 string (1)', (): void => { + expect( + base64Decode('aGVsbG8gd29ybGQg0J/RgNC40LLQtdGC0YHRgtCy0YPRjiDQvNC4IOS9oOWlvQ==') + ).toEqual( + // hello world Приветствую ми 你好 + new Uint8Array([ + 104, 101, 108, 108, 111, 32, 119, 111, + 114, 108, 100, 32, 208, 159, 209, 128, + 208, 184, 208, 178, 208, 181, 209, 130, + 209, 129, 209, 130, 208, 178, 209, 131, + 209, 142, 32, 208, 188, 208, 184, 32, + 228, 189, 160, 229, 165, 189 + ]) + ); + }); + + it('decodes a mixed base64 utf8 string (2)', (): void => { + expect( + base64Decode('4pyTIMOgIGxhIG1vZGU=') + ).toEqual( + // ✓ à la mode + new Uint8Array([ + 226, 156, 147, 32, 195, + 160, 32, 108, 97, 32, + 109, 111, 100, 101 + ]) + ); + }); + + it('decodes a mixed base64 utf8 string (3)', (): void => { + expect( + base64Decode('SGVsbG8gV29ybGQh') + ).toEqual( + // Hello World! + new Uint8Array([ + 72, 101, 108, 108, 111, + 32, 87, 111, 114, 108, + 100, 33 + ]) + ); + }); +}); diff --git a/packages/x-randomvalues/src/base64.ts b/packages/x-randomvalues/src/base64.ts new file mode 100644 index 000000000..6b4fdf3ca --- /dev/null +++ b/packages/x-randomvalues/src/base64.ts @@ -0,0 +1,26 @@ +// Copyright 2017-2021 @polkadot/x-randomvalues authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +// a tiny base64 decoder for RN usage + +const ALPHABET = +'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; + +export function base64Decode (data: string): Uint8Array { + const bytes = []; + let byte = 0; + let bits = 0; + + for (let i = 0; i < data.length && data[i] !== '='; i++) { + // each character represents 6 bits + bits += 6; + byte = (byte << 6) + ALPHABET.indexOf(data[i]); + + // each byte needs to contain 8 bits + if (bits >= 8) { + bytes.push((byte >>> (bits -= 8)) & 0xff); + } + } + + return Uint8Array.from(bytes); +} diff --git a/packages/x-randomvalues/src/react-native.ts b/packages/x-randomvalues/src/react-native.ts index c111b2a99..f91998ec8 100644 --- a/packages/x-randomvalues/src/react-native.ts +++ b/packages/x-randomvalues/src/react-native.ts @@ -9,6 +9,7 @@ import { NativeModules } from 'react-native'; import { xglobal } from '@polkadot/x-global'; +import { base64Decode } from './base64'; import { insecureRandomValues } from './fallback'; export { packageInfo } from './packageInfo'; @@ -20,7 +21,9 @@ interface RNExt { } function getRandomValuesNative (output: T): T { - const bytes = Buffer.from((NativeModules as RNExt).RNGetRandomValues.getRandomBase64(output.length), 'base64'); + const bytes = base64Decode( + (NativeModules as RNExt).RNGetRandomValues.getRandomBase64(output.length) + ); for (let i = 0; i < bytes.length; i++) { output[i] = bytes[i];