mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-22 22:49:45 +01:00
33 lines
712 B
JavaScript
33 lines
712 B
JavaScript
import SparkMD5 from "spark-md5";
|
|
/**
|
|
*
|
|
* @param {number} size
|
|
* @returns {Buffer}
|
|
*/
|
|
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;
|
|
}
|
|
|
|
function cryptoRandom(size, type) {
|
|
return randomBytes(size).toString(type);
|
|
}
|
|
|
|
export default function () {
|
|
return cryptoRandom(12, "hex");
|
|
}
|
|
|
|
export function makeId(text) {
|
|
return SparkMD5.hash(text);
|
|
}
|