Generating a Byte Array Key with Bash and Haskell

I know I’m going to forget how to do this in the future and it’s probably something I’ll re-use to help me generate 32 byte keys in the future.

To generate the random key use the following commands from Bash.

date +%s | sha256sum | head -c 64 ; echo

Then take the resulting string (we’ll call it secret), which will 64 hexadecimal characters long, and use it in the following Haskell list comprehension (I evaluate it with GHCi) to turn it into an array of properly formatted hexadecimal literals like so.

Prelude> import qualified Data.Text as T
Prelude T> ["0x" ++ (T.unpack i) | i <- T.chunksOf 2 (T.pack "secret")]

Which will output something in the following format.

["0xHH", "0xHH", ...]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s