Skip to content

Commit

Permalink
Reduce try catch statements.
Browse files Browse the repository at this point in the history
  • Loading branch information
evanvosberg committed Feb 11, 2020
1 parent 4d5da7a commit ac28862
Showing 1 changed file with 23 additions and 25 deletions.
48 changes: 23 additions & 25 deletions src/core.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*globals window, global*/
/*globals window, global, require*/

/**
* CryptoJS core components.
Expand All @@ -13,39 +13,37 @@ var CryptoJS = CryptoJS || (function (Math, undefined) {
var cryptoSecureRandomInt = function () {
var crypto;

// Native crypto module in Browser environment
try {
if (typeof window !== 'undefined') {
if (window.crypto) {
// Use global crypto module
crypto = window.crypto;
} else if (window.msCrypto) {
// Support experimental crypto module in IE 11
crypto = window.msCrypto;
}
}
} catch (err) {}

// Native crypto module on NodeJS environment
try {
if (typeof global !== 'undefined' && global.crypto) {
// Native crypto from global
crypto = global.crypto;
} else if (typeof require === 'function') {
// Native crypto import via require
// Native crypto from window (Browser)
if (typeof window !== 'undefined' && window.crypto) {
crypto = window.crypto;
}

// Native (experimental IE 11) crypto from window (Browser)
if (!crypto && typeof window !== 'undefined' && window.msCrypto) {
crypto = window.msCrypto;
}

// Native crypto from global (NodeJS)
if (!crypto && typeof global !== 'undefined' && global.crypto) {
crypto = global.crypto;
}

// Native crypto import via require (NodeJS)
if (!crypto && typeof require === 'function') {
try {
crypto = require('crypto');
}
} catch (err) {}
} catch (err) {}
}

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

// Use randomBytes method
// Use randomBytes method (NodeJS)
if (typeof crypto.randomBytes === 'function') {
try {
return crypto.randomBytes(4).readInt32LE();
Expand Down

0 comments on commit ac28862

Please sign in to comment.