2019-05-20 16:06:20 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
import crypto from './crypto_async.mjs';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates cryptographically secure random ids.
|
|
|
|
* @return {string} A crypto-secure random id as a string.
|
|
|
|
*/
|
|
|
|
async function get_id_string(length) {
|
|
|
|
return (await crypto.randomBytes(length / 1.3333333333333))
|
|
|
|
.toString("base64")
|
|
|
|
.replace(/\+/g, "-").replace(/\//g, "_")
|
|
|
|
.replace(/=/g, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a crypto-secure random id.
|
|
|
|
* @return {number} A crypto-secure random integer, suitable for use as a random id.
|
|
|
|
*/
|
|
|
|
async function get_id_number() {
|
|
|
|
return parseInt(
|
|
|
|
(await crypto.randomBytes(4)).toString("hex"),
|
|
|
|
16
|
|
|
|
);
|
|
|
|
}
|
2019-05-22 11:28:57 +00:00
|
|
|
|
|
|
|
export { get_id_string, get_id_number };
|