How to encode md5 sum into base64 in BASH
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)
In busybox you might not be able to use for loop syntax. Below unhex() is implemented with a while loop instead:
unhex (){ b=0; while [ $b -lt ${#1} ]; do printf "\\x${1:$b:2}"; b=$((b += 2)); done}md5sum2bytes (){ while read -r md5sum file; do unhex $md5sum; done}md5sum inputfile | md5sum2bytes | base64