From 08a3455365d30cb65129841afd57444b27347e2e Mon Sep 17 00:00:00 2001 From: Alex Hinson Date: Sat, 31 Oct 2020 04:53:48 -0700 Subject: [PATCH] fix (amazon-cognito-identity-js): use Node randomBytes --- .../src/utils/cryptoSecureRandomInt.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/packages/amazon-cognito-identity-js/src/utils/cryptoSecureRandomInt.js b/packages/amazon-cognito-identity-js/src/utils/cryptoSecureRandomInt.js index c5dbbb90d82..35103184ac5 100644 --- a/packages/amazon-cognito-identity-js/src/utils/cryptoSecureRandomInt.js +++ b/packages/amazon-cognito-identity-js/src/utils/cryptoSecureRandomInt.js @@ -20,10 +20,20 @@ if (!crypto && typeof global !== 'undefined' && global.crypto) { * As Math.random() is cryptographically not safe to use */ export default function cryptoSecureRandomInt() { - if (crypto && typeof crypto.getRandomValues === 'function') { - try { - return crypto.getRandomValues(new Uint32Array(1))[0]; - } catch (err) {} + if (crypto) { + // Use getRandomValues method (Browser) + if (typeof crypto.getRandomValues === 'function') { + try { + return crypto.getRandomValues(new Uint32Array(1))[0]; + } catch (err) {} + } + + // Use randomBytes method (NodeJS) + if (typeof crypto.randomBytes === 'function') { + try { + return crypto.randomBytes(4).readInt32LE(); + } catch (err) {} + } } throw new Error(