Skip to content

Instantly share code, notes, and snippets.

@rietta
Created September 5, 2012 17:01
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save rietta/3640002 to your computer and use it in GitHub Desktop.
Save rietta/3640002 to your computer and use it in GitHub Desktop.
Easy command line random hash generator for Mac OS X, Linux, and FreeBSD.
#!/bin/sh
head -n 4096 /dev/urandom | openssl sha1
@rietta
Copy link
Author

rietta commented Sep 5, 2012

I use this script to quickly generate random hash values. This is convenient for picking impossible to guess random file names.

@rietta
Copy link
Author

rietta commented Sep 5, 2012

Example Invocation

frank@ptolemy ~ $ random_hash
(stdin)= 50d1d9827971b3d3689f6ac3b7e864c64556546034599113a89bbeb9dc5f9091

@rietta
Copy link
Author

rietta commented Sep 5, 2012

I just noticed that openssl sha256 is not supported on my MacBook Air - the pro must have an alternate version of openssl installed from Brew or something. In any case, I updated the script to use sha1 which works on both. Here is an example invocation:

frank@Callisto ~/bin $ random_hash
43a4dacee1bc9ee1855efb24d8fcc8d39bb47391

@gonzopancho
Copy link

sha224 was invented for exactly this “shorter hash” purpose. This is a hash function with a 224-bit output, which NIST defined in order to “match” (administratively speaking) the proclaimed 112-bit security of 3DES. They defined SHA-224 by taking SHA-256, changing the conventional IV, and truncating the output. For the truncation, they took the first 224 bits. See also: https://csrc.nist.gov/publications/detail/fips/180/4/final

$ head -n 4096 /dev/urandom | openssl sha224
05cac32284221f1a2f9218d12c121f298dbc18f8e589da5023c69a43

@crmpicco
Copy link

This is a really useful snippet. Thank you for sharing 👍

@soniah
Copy link

soniah commented Nov 16, 2023

Even fancier:

% head -n 4096 /dev/urandom | openssl sha1 | awk '{print $2}' | cut -c1-6
a87a72

In your ~/.zshrc:

alias rsha='head -n 4096 /dev/urandom | openssl sha1 | awk "{print \$2}" | cut -c1-6'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment