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;
}
June 20 Random alphanumeric string | ||||
---|---|---|---|---|
| ||||
June 20 Re: Random alphanumeric string | ||||
---|---|---|---|---|
| ||||
Posted in reply to greenbyte | On Monday, 20 June 2022 at 11:49:24 UTC, greenbyte wrote: >
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. |
Copyright © 1999-2021 by the D Language Foundation