Add RandomHexString for unguessable identifiers and tokens #14

Merged
sneak merged 1 commits from clawbot/util:proposal-random-hex-string into master 2026-09-05 05:44:24 +02:00
Contributor

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
clawbot added 1 commit 2026-09-05 05:28:29 +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 master 2026-09-05 05:44:24 +02:00
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: sneak/util#14