Add an id generator helper
This commit is contained in:
parent
0a05e00c11
commit
391f066521
3 changed files with 34 additions and 2 deletions
25
server/Helpers/IdGenerator.mjs
Normal file
25
server/Helpers/IdGenerator.mjs
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
"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
|
||||||
|
);
|
||||||
|
}
|
8
server/Helpers/crypto_async.mjs
Normal file
8
server/Helpers/crypto_async.mjs
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
import util from 'util';
|
||||||
|
import crypto from './Objects/FlyingBanana';
|
||||||
|
|
||||||
|
export default {
|
||||||
|
randomBytes: util.promisify(crypto.randomBytes)
|
||||||
|
};
|
|
@ -1,6 +1,5 @@
|
||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
"use strict";
|
||||||
// Requires Ansi.mjs, which can be found here: https://gist.github.com/8c0bb5e172438b6e62dd48587cfeba84#file-ansi-mjs
|
|
||||||
|
|
||||||
import ansi from './Helpers/Ansi.mjs';
|
import ansi from './Helpers/Ansi.mjs';
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue