Tiny base64 decoder (no Buffer dep) (#1251)
* Tiny base64 decoder (no Buffer dep) * CHANGELOG
This commit is contained in:
@@ -1,5 +1,12 @@
|
||||
# CHANGELOG
|
||||
|
||||
## master
|
||||
|
||||
Changes:
|
||||
|
||||
- Remove `Buffer` usage in `x-randomvalues`
|
||||
|
||||
|
||||
## 8.0.2
|
||||
|
||||
Changes:
|
||||
|
||||
@@ -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!')
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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
|
||||
])
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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 <T extends Uint8Array> (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];
|
||||
|
||||
Reference in New Issue
Block a user