Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marioapardo/27fa8b382d0e5507d055139b4d547531 to your computer and use it in GitHub Desktop.
Save marioapardo/27fa8b382d0e5507d055139b4d547531 to your computer and use it in GitHub Desktop.
List of one-line random string generators

One-line Random String Generator

Python

CSPRNG (Cryptographically Secure Pseudo-Random Number Generator) functions:

  • os.urandom(n): return a string of n random bytes.
  • random.SystemRandom(): provides random functions that uses os.urandom().

Note: Don't use random module for PRNG for security purposes.

Random Bytes

python -c "import os; print(os.urandom(20))"

Random Hex

python -c "import os, binascii; print(binascii.hexlify(os.urandom(20)))"

Random Integer

 python -c "import random; print(random.SystemRandom().randint(0, 10 ** 10))"

Random Alphanumeric

python -c "import random, string; print(''.join(random.SystemRandom().choice(string.ascii_lowercase + string.ascii_uppercase + string.digits) for _ in range(20)))"

Ruby

Random Bytes

ruby -rsecurerandom -e "puts SecureRandom.random_bytes(20)"

Random Hex

ruby -rsecurerandom -e 'puts SecureRandom.hex(20)'
ruby -rsecurerandom -e "puts SecureRandom.random_bytes(20).unpack('H*')"

Random Integer

ruby -rsecurerandom -e 'puts SecureRandom.random_number(10 ** 10)'

Random Alphanumeric

ruby -e "range = [*'0'..'9',*'A'..'Z',*'a'..'z']; puts Array.new(20){ range.sample }.join"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment