Files
notesnook/packages/core/utils/id.js
2021-02-20 11:31:44 +05:00

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);
}