Skip to content

Commit

Permalink
Just one if to check whether crypto is defined.
Browse files Browse the repository at this point in the history
  • Loading branch information
evanvosberg committed Feb 11, 2020
1 parent 3cbd6c1 commit 4d5da7a
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,20 @@ var CryptoJS = CryptoJS || (function (Math, undefined) {
}
} catch (err) {}

// Use getRandomValues method
if (crypto && typeof crypto.getRandomValues === 'function') {
try {
return crypto.getRandomValues(new Uint32Array(1))[0];
} catch (err) {}
}
if (crypto) {
// Use getRandomValues method
if (typeof crypto.getRandomValues === 'function') {
try {
return crypto.getRandomValues(new Uint32Array(1))[0];
} catch (err) {}
}

// Use randomBytes method
if (crypto && typeof crypto.randomBytes === 'function') {
try {
return crypto.randomBytes(4).readInt32LE();
} catch (err) {}
// Use randomBytes method
if (typeof crypto.randomBytes === 'function') {
try {
return crypto.randomBytes(4).readInt32LE();
} catch (err) {}
}
}

throw new Error('Native crypto module could not be used to get secure random number.');
Expand Down

0 comments on commit 4d5da7a

Please sign in to comment.