In theory, you should just use Crypto.randomUUID() or Crypto.randomBytes(16).toString('base64'), but due to nuance differences in desires, people end up re-implementing a randomString() function.
Making this simpler may not actually fix that - people like to re-invent well-known wheels.
However, it should help those who subscribe to AjScript to have it ready-at-hand.
Typically the strings people want are small - 32 bytes (256-bit entropy) or less, which points to a synchronous function being the most optimal, and I think even the browser getRandomValues is synchronous.
In theory, you should just use
Crypto.randomUUID()
orCrypto.randomBytes(16).toString('base64')
, but due to nuance differences in desires, people end up re-implementing arandomString()
function.Making this simpler may not actually fix that - people like to re-invent well-known wheels.
However, it should help those who subscribe to AjScript to have it ready-at-hand.
How to pass custom dictionary / charset?
Optional name? or optional dict?
I'm leaning toward this:
char len vs entropy
Hmm...
I'm leaning towards
len
meaning number of string characters, as this seems most intuitive.There may need to be a
Crypto._bitsPerChar(len)
andCrypto._minCharsForBits(256)
or some such.See https://therootcompany.com/blog/how-many-bits-of-entropy-per-character/.
sync vs async?
Typically the strings people want are small - 32 bytes (256-bit entropy) or less, which points to a synchronous function being the most optimal, and I think even the browser
getRandomValues
is synchronous.