Browse Source

cryptographically secure RNG

pull/1/head
Tim Glasgow 2 years ago
parent
commit
d52b82f7d9
  1. 11
      changeme/src/libs/Random.js

11
changeme/src/libs/Random.js

@ -1,11 +1,20 @@
import 'react-native-get-random-values';
export function generateKey() {
let result = [];
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let charactersLength = characters.length;
let uintArray = new Uint8Array(64);
let newKey = crypto.getRandomValues(uintArray);
for (let i = 0; i < 64; i++) {
let newCharIndex = newKey[i];
while(newCharIndex > charactersLength){
newCharIndex -= charactersLength;
}
result.push(
characters.charAt(Math.floor(Math.random() * charactersLength)),
characters.charAt(newCharIndex),
);
}

Loading…
Cancel
Save