2022-11-17 17:19:32 +02:00
|
|
|
import { v4 as uuidv4 } from 'uuid';
|
2022-04-11 17:49:32 +02:00
|
|
|
import { customAlphabet } from 'nanoid/non-secure';
|
2023-12-20 21:05:59 +02:00
|
|
|
import { nanoid as nanoidSecure, customAlphabet as customAlphabetSecure } from 'nanoid';
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2020-12-28 13:48:47 +02:00
|
|
|
// https://zelark.github.io/nano-id-cc/
|
|
|
|
// https://security.stackexchange.com/a/41749/1873
|
|
|
|
// > On the other hand, 128 bits (between 21 and 22 characters
|
|
|
|
// > alphanumeric) is beyond the reach of brute-force attacks pretty much
|
|
|
|
// > indefinitely
|
2020-10-09 19:35:46 +02:00
|
|
|
const nanoid = customAlphabet('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', 22);
|
|
|
|
|
2023-12-13 21:25:52 +02:00
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
export default {
|
2021-06-22 20:57:04 +02:00
|
|
|
create: function(): string {
|
2022-11-17 17:19:32 +02:00
|
|
|
return uuidv4().replace(/-/g, '');
|
2020-10-09 19:35:46 +02:00
|
|
|
},
|
2021-06-22 20:57:04 +02:00
|
|
|
createNano: function(): string {
|
2020-10-09 19:35:46 +02:00
|
|
|
return nanoid();
|
|
|
|
},
|
|
|
|
};
|
2023-12-13 21:25:52 +02:00
|
|
|
|
2023-12-14 17:28:41 +02:00
|
|
|
export const createSecureRandom = (size = 32) => {
|
|
|
|
return nanoidSecure(size);
|
|
|
|
};
|
|
|
|
|
2023-12-13 21:25:52 +02:00
|
|
|
type FuncUiidGen = (length?: number)=> string;
|
|
|
|
|
|
|
|
const cachedUuidgen: Record<number, FuncUiidGen> = {};
|
|
|
|
const createUuidgenCustomAlphabet = (length: number) => customAlphabet('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', length);
|
|
|
|
|
|
|
|
const getCachedUuidgen = (length: number) => {
|
|
|
|
if (cachedUuidgen[length]) return cachedUuidgen[length];
|
|
|
|
|
|
|
|
cachedUuidgen[length] = createUuidgenCustomAlphabet(length);
|
|
|
|
return cachedUuidgen[length];
|
|
|
|
};
|
|
|
|
|
|
|
|
export const uuidgen = (length = 22) => {
|
|
|
|
const cachedUuidgen = getCachedUuidgen(length);
|
|
|
|
return cachedUuidgen();
|
|
|
|
};
|
2023-12-20 21:05:59 +02:00
|
|
|
|
|
|
|
export const createNanoForInboxEmail = (): string => {
|
|
|
|
return customAlphabet('0123456789abcdefghijklmnopqrstuvwxyz', 8)();
|
|
|
|
};
|
|
|
|
|
|
|
|
export { customAlphabetSecure };
|