Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to encode md5 sum into base64 in BASH

Tags:

bash

base64

md5

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.

like image 310
Rusty Horse Avatar asked Jan 03 '11 11:01

Rusty Horse


People also ask

What is MD5 base64?

An MD5 hash is a 128-bit value. Every character in a Base64 string contains 6 bits of information, because there are 64 possible values for the character, and it takes 6 powers of 2 to reach 64.

What does MD5 sum do?

md5sum is used to verify the integrity of files, as virtually any change to a file will cause its MD5 hash to change. Most commonly, md5sum is used to verify that a file has not changed as a result of a faulty file transfer, a disk error or non-malicious meddling.

How do I encode a string in bash?

Example#5: Encoding any user-defined textCreate a bash file named encode_user_data.sh with the following code. The following script will take any text data as input, encode the text by using base64 and print the encoded text as output. Run the script.

How do I decode base64 text in Linux?

To decode with base64 you need to use the --decode flag. With encoded string, you can pipe an echo command into base64 as you did to encode it. Using the example encoding shown above, let's decode it back into its original form. Provided your encoding was not corrupted the output should be your original string.


1 Answers

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)

like image 131
plundra Avatar answered Oct 05 '22 21:10

plundra