How to get a random string of 32 hexadecimal digits through command line?« Back to Questions List

I'd like to put together a command that will print out a string of 32 hexadecimal digits.

I need to stick with more bash'ish tools like sed, awk, /dev/random because I'm on a limited platform. Is there a way to do this?

Posted by Dmytro Yakovenko
Asked on April 2, 2022 4:09 pm
0
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
Posted by darkfire
Answered On April 2, 2022 4:26 pm
0

If you have hexdump then:

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

should do the job. Explanation: -n 16 to consume 16 bytes of input (32 hex digits = 16 bytes). 4/4 ”X” 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.

Posted by darkfire
Answered On April 2, 2022 4:25 pm
1
To generate in the Linux console, I use the following code:
    head -c16 </dev/urandom|xxd -p -u
Posted by darkfire
Answered On April 2, 2022 4:24 pm