From cf354d58800c8e3ab2c2b65efa289ef2f5acb69f Mon Sep 17 00:00:00 2001 From: Tim Glasgow Date: Sun, 31 Jul 2022 23:27:37 -0500 Subject: [PATCH] cryptographically secure RNG --- squarenotsquare/src/libs/Random.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/squarenotsquare/src/libs/Random.js b/squarenotsquare/src/libs/Random.js index aeea885..ceec7c6 100644 --- a/squarenotsquare/src/libs/Random.js +++ b/squarenotsquare/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), ); }