mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-22 14:39:34 +01:00
feat: use bson-objectid for id generation
This commit is contained in:
26
packages/core/utils/random.js
Normal file
26
packages/core/utils/random.js
Normal file
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
*
|
||||
* @param {number} size
|
||||
* @returns {Buffer}
|
||||
*/
|
||||
module.exports.randomBytes = function randomBytes(size) {
|
||||
if (!global.crypto || !crypto)
|
||||
throw new Error("Crypto is not supported on this platform.");
|
||||
if (crypto.randomBytes) return crypto.randomBytes(size);
|
||||
|
||||
if (!crypto.getRandomValues)
|
||||
throw new Error(
|
||||
"Crypto.getRandomValues is not available on this platform."
|
||||
);
|
||||
|
||||
const buffer = Buffer.allocUnsafe(size);
|
||||
crypto.getRandomValues(buffer);
|
||||
return buffer;
|
||||
};
|
||||
|
||||
module.exports.randomInt = function () {
|
||||
const randomBuffer = module.exports.randomBytes(1);
|
||||
let randomNumber = randomBuffer[0] / 0xff; // / (0xffffffff + 1);
|
||||
|
||||
return Math.floor(randomNumber * 0xffffff);
|
||||
};
|
||||
Reference in New Issue
Block a user