Skip to content

Commit

Permalink
fix (amazon-cognito-identity-js): use Node randomBytes (aws-amplify#7093
Browse files Browse the repository at this point in the history
)
  • Loading branch information
amhinson authored and CryogenicPlanet committed Jan 20, 2021
1 parent f7a3f4e commit 7767eaa
Showing 1 changed file with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 7767eaa

Please sign in to comment.