Skip to content

Instantly share code, notes, and snippets.

@nicholashoule
Last active March 12, 2026 19:18
Show Gist options
  • Select an option

  • Save nicholashoule/d2c88f56d90de2d833cbdc6e2bbc5f33 to your computer and use it in GitHub Desktop.

Select an option

Save nicholashoule/d2c88f56d90de2d833cbdc6e2bbc5f33 to your computer and use it in GitHub Desktop.

Random

pkg: haveged

GitHub - jirka-h/haveged

Starting from Linux kernel v5.4, the HAVEGED inspired algorithm has been included in the Linux kernel.

DESCRIPTION

Linux provides cryptographic randomness through /dev/random and /dev/urandom, both backed by the kernel’s central entropy pool. Modern kernels seed and maintain this pool using diverse sources—interrupt timing, CPU jitter, hardware RNGs, and virtualization-provided entropy—allowing the CRNG to initialize early and stay well‑seeded under normal workloads. On current systems (kernel 5.4+), these built‑in mechanisms are typically sufficient without additional entropy services.

Random commands

Linux

# Random number between 0 and 32767
echo "$RANDOM"
shuf -i 1-10000000 -n 1
< /dev/urandom tr -dc 0-9 | head -c 16
< /dev/urandom tr -dc 0-9 | head -c 32
< /dev/urandom tr -dc A-Za-z0-9 | sha256sum | head -c 32
< /dev/urandom tr -dc A-Za-z0-9_- | head -c 32

MacOS

# Random number between 0 and 32767
echo "$RANDOM"
jot -r 1 1 10000000
# brew install coreutils
shuf -i 1-10000000 -n 1
tr -dc A-Za-z0-9 < /dev/urandom | shasum -a 256 | head -c 16
tr -dc A-Za-z0-9 < /dev/urandom | shasum -a 256 | head -c 32
tr -dc A-Za-z0-9 < /dev/urandom | shasum -a 512 | head -c 32
tr -dc A-Za-z0-9_- < /dev/urandom | head -c 32

Windows

Get-Random -Minimum 1 -Maximum 1000000000000000000
$bytes=[byte[]]::new(32);[System.Security.Cryptography.RandomNumberGenerator]::Create().GetBytes($bytes);([BitConverter]::ToString([System.Security.Cryptography.SHA256]::Create().ComputeHash($bytes)) -replace '-','').Substring(0,16)

Other

Python
import random; print(random.randint(1, 100000))
python3 -c "import os,hashlib;print(hashlib.sha256(os.urandom(32)).hexdigest()[:16])"
Perl
perl -e 'print 1 + int(rand(10000)), "\n"'
perl -MDigest::SHA=sha256_hex -e 'print substr(sha256_hex(rand() . $$ . time . {}), 0, 16), "\n"'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment