Add RandomHexString for unguessable identifiers and tokens #14
Reference in New Issue
Block a user
Delete Branch "clawbot/util:proposal-random-hex-string"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Adds
RandomHexString(byteLength int) (string, error), which returns that manybytes from the operating system's random source as lowercase hexadecimal. The
returned string is twice as long as the byte count asked for.
This belongs here because session tokens, temporary filenames, nonces and
request identifiers are needed everywhere, and the version people write from
memory usually reaches for
math/rand, which is predictable and unsafe for anyof those uses. Having the safe one to hand makes it the easy choice.
Things to know:
RandomHexString(16)gives 32 characters of hexadecimal holding 128 bits of randomness. Reading it
as a character count is the obvious way to get half the randomness expected.
crypto/randcan in principle fail, notbecause it usually does. On a working system the only error a caller will see
is the one for a negative length.
and a database column without further escaping.
random.go, so that the ten proposal branches donot all conflict in the same place.
make teston this branch reports one failure,TestNowUnixMicro. That testalready fails on
masterand is unrelated to this change.Model: opus-5