Adds RandomHexString(byteLength int) (string, error), which returns that many
bytes 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 any
of those uses. Having the safe one to hand makes it the easy choice.
Things to know:
The argument is a number of bytes, not a number of characters. 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.
The error is returned because crypto/rand can in principle fail, not
because it usually does. On a working system the only error a caller will see
is the one for a negative length.
Hexadecimal was chosen over base64 so the result is safe in a URL, a filename
and a database column without further escaping.
The code is in a new file, random.go, so that the ten proposal branches do
not all conflict in the same place.
make test on this branch reports one failure, TestNowUnixMicro. That test
already fails on master and is unrelated to this change.
Model: opus-5
Adds `RandomHexString(byteLength int) (string, error)`, which returns that many
bytes 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 any
of those uses. Having the safe one to hand makes it the easy choice.
Things to know:
- The argument is a number of bytes, not a number of characters. `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.
- The error is returned because `crypto/rand` can in principle fail, not
because it usually does. On a working system the only error a caller will see
is the one for a negative length.
- Hexadecimal was chosen over base64 so the result is safe in a URL, a filename
and a database column without further escaping.
- The code is in a new file, `random.go`, so that the ten proposal branches do
not all conflict in the same place.
- `make test` on this branch reports one failure, `TestNowUnixMicro`. That test
already fails on `master` and is unrelated to this change.
Model: opus-5
clawbot
self-assigned this 2026-09-05 05:28:28 +02:00
RandomHexString returns the requested number of bytes from crypto/rand as
lowercase hexadecimal, so callers reaching for a token do not end up using the
predictable math/rand instead. Comes with a doc comment and table-driven tests.
(closes#13)
Model: opus-5
sneak
merged commit 5bf829aefb into master2026-09-05 05:44:24 +02:00
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
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