Adjust encoder/decoder fallbacks (#750)
* Adjust encoder/decoder fallbacks * Dep-dup is functions * Complexity cleanups
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
// Copyright 2017-2020 @polkadot/x-randomvalues authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import fallback from './fallback';
|
||||
|
||||
describe('fallback (insecure)', (): void => {
|
||||
it('subsequent results does not match', (): void => {
|
||||
expect(
|
||||
fallback(new Uint8Array(32)).toString()
|
||||
).not.toEqual(
|
||||
fallback(new Uint8Array(32)).toString()
|
||||
);
|
||||
});
|
||||
|
||||
it('generates with the supplied length', (): void => {
|
||||
expect(
|
||||
fallback(new Uint8Array(128))
|
||||
).toHaveLength(128);
|
||||
});
|
||||
});
|
||||
@@ -16,11 +16,11 @@ export default function insecureRandomValues <T extends Uint8Array> (arr: T): T
|
||||
let r = 0;
|
||||
|
||||
for (let i = 0; i < arr.length; i++) {
|
||||
if ((i & 0x03) === 0) {
|
||||
if ((i & 0b11) === 0) {
|
||||
r = Math.random() * 0x100000000;
|
||||
}
|
||||
|
||||
arr[i] = (r >>> ((i & 0x03) << 3)) & 0xff;
|
||||
arr[i] = (r >>> ((i & 0b11) << 3)) & 0xff;
|
||||
}
|
||||
|
||||
return arr;
|
||||
|
||||
@@ -16,9 +16,6 @@ interface RNExt {
|
||||
}
|
||||
|
||||
function getRandomValuesNative <T extends Uint8Array> (arr: T): T {
|
||||
// We may want to use a base64 decoder here, but we certainly have an issue where we
|
||||
// want/need this part as lean as possible. Since Buffer is already needed elsewhere
|
||||
// (an generally prevalent in JS), we rather use it as-is.
|
||||
return Buffer
|
||||
.from((NativeModules as RNExt).RNGetRandomValues.getRandomBase64(arr.length), 'base64')
|
||||
.reduce((arr, byte, index): T => {
|
||||
|
||||
Reference in New Issue
Block a user