How to get a random string of 32 hexadecimal digits through command line?

If you are looking for a single command and have openssl installed, see below. Generate random 16 bytes (32 hex symbols) and encode in hex (also -base64 is supported).

openssl rand -hex 16

If you have hexdump then:

hexdump -vn16 -e'4/4 "%08X" 1 "\n"' /dev/urandom

should do the job.

Explanation:

  • -v to print all data (by default hexdump replaces repetition by *).
  • -n16 to consume 16 bytes of input (32 hex digits = 16 bytes).
  • 4/4 "%08X" to iterate four times, consume 4 bytes per iteration and print the corresponding 32 bits value as 8 hex digits, with leading zeros, if needed.
  • 1 "\n" to end with a single newline.

Tags:

Bash

Random