-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
26 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,26 @@ | ||
module.exports = require('nanoid/random'); | ||
if (typeof navigator !== 'undefined' && navigator.product === 'ReactNative') { | ||
module.exports = require('react-native-randombytes').randomBytes; | ||
} else { | ||
/*jshint -W061 */ | ||
var crypto = eval("require('crypto');"); //Required for React Native to work | ||
if (crypto.randomFillSync) { | ||
// We reuse buffers with the same size to avoid memory fragmentations | ||
// for better performance | ||
var buffers = {}; | ||
module.exports = function (bytes) { | ||
var buffer = buffers[bytes]; | ||
if (!buffer) { | ||
// `Buffer.allocUnsafe()` faster because it don’t clean memory. | ||
// We do not need it, since we will fill memory with new bytes anyway. | ||
buffer = Buffer.allocUnsafe(bytes); | ||
if (bytes <= 255) { | ||
buffers[bytes] = buffer; | ||
} | ||
} | ||
return crypto.randomFillSync(buffer); | ||
}; | ||
} else { | ||
module.exports = crypto.randomBytes; | ||
} | ||
} | ||
|