From e32b768e5c95574555cb29020d414c814f5c4598 Mon Sep 17 00:00:00 2001 From: BluJ Date: Thu, 4 May 2023 17:15:29 -0600 Subject: [PATCH] feat: Add crypto, getRandomString --- lib/types.ts | 10 ++-- lib/util/getDefaultString.ts | 106 +-------------------------------- lib/util/getRandomCharInSet.ts | 98 ++++++++++++++++++++++++++++++ lib/util/getRandomString.ts | 11 ++++ 4 files changed, 117 insertions(+), 108 deletions(-) create mode 100644 lib/util/getRandomCharInSet.ts create mode 100644 lib/util/getRandomString.ts diff --git a/lib/types.ts b/lib/types.ts index 2b7373d..abc120a 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -390,10 +390,12 @@ export type Effects = { stopped(packageId?: string): Promise - vaultList(): Promise - vaultSet(opt: { key: string; value: string }): Promise - vaultMove(opt: { fromKey: string; toKey: string }): Promise - vaultDelete(opt: { key: string }): Promise + vault: { + list(): Promise + set(opt: { key: string; value: string }): Promise + move(opt: { fromKey: string; toKey: string }): Promise + delete(opt: { key: string }): Promise + } } // prettier-ignore diff --git a/lib/util/getDefaultString.ts b/lib/util/getDefaultString.ts index 8b5fa04..fa35b4e 100644 --- a/lib/util/getDefaultString.ts +++ b/lib/util/getDefaultString.ts @@ -1,112 +1,10 @@ import { DefaultString } from "../config/configTypes" +import { getRandomString } from "./getRandomString" export function getDefaultString(defaultSpec: DefaultString): string { if (typeof defaultSpec === "string") { return defaultSpec } else { - let s = "" - for (let i = 0; i < defaultSpec.len; i++) { - s = s + getRandomCharInSet(defaultSpec.charset) - } - - return s + return getRandomString(defaultSpec) } } - -// a,g,h,A-Z,,,,- -export function getRandomCharInSet(charset: string): string { - const set = stringToCharSet(charset) - let charIdx = Math.floor(Math.random() * set.len) - for (let range of set.ranges) { - if (range.len > charIdx) { - return String.fromCharCode(range.start.charCodeAt(0) + charIdx) - } - charIdx -= range.len - } - throw new Error("unreachable") -} - -function stringToCharSet(charset: string): CharSet { - let set: CharSet = { ranges: [], len: 0 } - let start: string | null = null - let end: string | null = null - let in_range = false - for (let char of charset) { - switch (char) { - case ",": - if (start !== null && end !== null) { - if (start!.charCodeAt(0) > end!.charCodeAt(0)) { - throw new Error("start > end of charset") - } - const len = end.charCodeAt(0) - start.charCodeAt(0) + 1 - set.ranges.push({ - start, - end, - len, - }) - set.len += len - start = null - end = null - in_range = false - } else if (start !== null && !in_range) { - set.len += 1 - set.ranges.push({ start, end: start, len: 1 }) - start = null - } else if (start !== null && in_range) { - end = "," - } else if (start === null && end === null && !in_range) { - start = "," - } else { - throw new Error('unexpected ","') - } - break - case "-": - if (start === null) { - start = "-" - } else if (!in_range) { - in_range = true - } else if (in_range && end === null) { - end = "-" - } else { - throw new Error('unexpected "-"') - } - break - default: - if (start === null) { - start = char - } else if (in_range && end === null) { - end = char - } else { - throw new Error(`unexpected "${char}"`) - } - } - } - if (start !== null && end !== null) { - if (start!.charCodeAt(0) > end!.charCodeAt(0)) { - throw new Error("start > end of charset") - } - const len = end.charCodeAt(0) - start.charCodeAt(0) + 1 - set.ranges.push({ - start, - end, - len, - }) - set.len += len - } else if (start !== null) { - set.len += 1 - set.ranges.push({ - start, - end: start, - len: 1, - }) - } - return set -} -type CharSet = { - ranges: { - start: string - end: string - len: number - }[] - len: number -} diff --git a/lib/util/getRandomCharInSet.ts b/lib/util/getRandomCharInSet.ts new file mode 100644 index 0000000..d380e28 --- /dev/null +++ b/lib/util/getRandomCharInSet.ts @@ -0,0 +1,98 @@ +// a,g,h,A-Z,,,,- + +import crypto from "crypto" +export function getRandomCharInSet(charset: string): string { + const set = stringToCharSet(charset) + let charIdx = crypto.randomInt(0, set.len) + for (let range of set.ranges) { + if (range.len > charIdx) { + return String.fromCharCode(range.start.charCodeAt(0) + charIdx) + } + charIdx -= range.len + } + throw new Error("unreachable") +} +function stringToCharSet(charset: string): CharSet { + let set: CharSet = { ranges: [], len: 0 } + let start: string | null = null + let end: string | null = null + let in_range = false + for (let char of charset) { + switch (char) { + case ",": + if (start !== null && end !== null) { + if (start!.charCodeAt(0) > end!.charCodeAt(0)) { + throw new Error("start > end of charset") + } + const len = end.charCodeAt(0) - start.charCodeAt(0) + 1 + set.ranges.push({ + start, + end, + len, + }) + set.len += len + start = null + end = null + in_range = false + } else if (start !== null && !in_range) { + set.len += 1 + set.ranges.push({ start, end: start, len: 1 }) + start = null + } else if (start !== null && in_range) { + end = "," + } else if (start === null && end === null && !in_range) { + start = "," + } else { + throw new Error('unexpected ","') + } + break + case "-": + if (start === null) { + start = "-" + } else if (!in_range) { + in_range = true + } else if (in_range && end === null) { + end = "-" + } else { + throw new Error('unexpected "-"') + } + break + default: + if (start === null) { + start = char + } else if (in_range && end === null) { + end = char + } else { + throw new Error(`unexpected "${char}"`) + } + } + } + if (start !== null && end !== null) { + if (start!.charCodeAt(0) > end!.charCodeAt(0)) { + throw new Error("start > end of charset") + } + const len = end.charCodeAt(0) - start.charCodeAt(0) + 1 + set.ranges.push({ + start, + end, + len, + }) + set.len += len + } else if (start !== null) { + set.len += 1 + set.ranges.push({ + start, + end: start, + len: 1, + }) + } + return set +} +type CharSet = { + ranges: { + start: string + end: string + len: number + }[] + len: number +} diff --git a/lib/util/getRandomString.ts b/lib/util/getRandomString.ts new file mode 100644 index 0000000..ea0989b --- /dev/null +++ b/lib/util/getRandomString.ts @@ -0,0 +1,11 @@ +import { RandomString } from "../config/configTypes" +import { getRandomCharInSet } from "./getRandomCharInSet" + +export function getRandomString(generator: RandomString): string { + let s = "" + for (let i = 0; i < generator.len; i++) { + s = s + getRandomCharInSet(generator.charset) + } + + return s +}