How to encode md5 sum into base64 in BASH

Rusty Horse picture Rusty Horse · Jan 3, 2011 · Viewed 24.2k times · Source

I need to encode md5 hash to base 64. The problem is that if give output of md5sum command to base64 command, it is considered as a text and not as a hexadecimal data. How to manage it? Base64 command has no option to set it's input as a hexadecimal number.

Thanks for any help.

Answer

plundra picture plundra · Jan 3, 2011

Use openssl dgst -md5 -binary instead of md5sum. If you want, you can use it to base64-encode as well, to only use one program for all uses.

echo -n foo | openssl dgst -md5 -binary | openssl enc -base64

(openssl md5 instead of openssl dgst -md5 works too, but I think it's better to be explicit)