Skip to content

Commit

Permalink
Do not run the detect native crypto module for every cryptoSecureRand…
Browse files Browse the repository at this point in the history
…omInt call.
  • Loading branch information
evanvosberg committed Feb 11, 2020
1 parent ac28862 commit 7f809c9
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,36 @@
*/
var CryptoJS = CryptoJS || (function (Math, undefined) {

var crypto;

// 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) {}
}

/*
* Cryptographically secure pseudorandom number generator
*
* As Math.random() is cryptographically not safe to use
*/
var cryptoSecureRandomInt = function () {
var crypto;

// 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) {}
}

if (crypto) {
// Use getRandomValues method (Browser)
if (typeof crypto.getRandomValues === 'function') {
Expand Down

0 comments on commit 7f809c9

Please sign in to comment.