Random alphanumeric string
Cym13
cpicard at purrfect.fr
Mon Jun 20 12:40:05 UTC 2022
On Monday, 20 June 2022 at 11:49:24 UTC, greenbyte wrote:
> ```d
> string randomAlphanumericString(int length)
> {
> import std.array : array;
> import std.ascii : letters, digits;
> import std.random : choice, Random, unpredictableSeed;
> import std.range : generate, take;
> import std.conv : to;
>
> auto rnd = Random(unpredictableSeed);
> auto symbols = array(letters ~ digits);
>
> return generate!({ return symbols.choice(rnd);
> }).take(length).to!string;
> }
> ```
This works, but just a gentle reminder that this must not be used
to generate secrets (passwords, tokens, IDs whose disclosure
could be problematic...). std.random is not cryptographically
secure and therefore not fit for any security-related purpose. If
on the other hand you're just generating public IDs, delimiters,
that sort of things, then there is absolutely no issue with doing
it this way.
More information about the Digitalmars-d
mailing list