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(