3

I am trying to calculate the digests of a file using md5 algorithm. I am asked to format the output to be in binary not hex. So this is my command in terminal (I use mac):

openssl dgst -md5 -binary ~/Documents/msg_Joudi.txt > ~/Documents/hash.txt

this generates hash.txt file, but its content is not in binary format, and I do not know where is the error.

9
  • That looks right to me. I copied your code, ran $ file -I ~/Desktop/hash.txt from terminal to check the file type, and it returned: /Desktop/hash.txt: application/octet-stream; charset=binary What did you get instead of binary? Commented Dec 10, 2015 at 8:17
  • This is what in the file: y«I:yÌ˘m.§6ßı6B Commented Dec 10, 2015 at 8:30
  • @user233531 Your way of creating a binary md5 is correct. Output looks good, as well. Why do you think the file is not binary? And, besides, this is not a programming related question. Commented Dec 10, 2015 at 9:00
  • Binary should be 0s and 1s right? Sorry I frequently see questions in many topics Commented Dec 10, 2015 at 9:06
  • @NikolaiRuhe Why do you believe this is not a programming related question? He's trying to calculate an algorithm programmically and format the output. Which forum would you categorize this under? Commented Dec 10, 2015 at 9:08

2 Answers 2

6

Create MD5 hash of file: msgFile.txt > Convert to Binary and save:

cat msgFile.txt | openssl dgst -md5 -binary > hash.bin.txt

Save binary in Base64 format:

cat msgFile.txt | openssl dgst -md5 -binary | base64 > hash.bin.b64.txt

Save binary in Hexadecimal representation:

cat msgFile.txt | openssl dgst -md5 -binary | xxd -ps -c16 > hash.bin.hex.txt

Use Python to convert binary to Integers:

python -c "print(int('$(cat msgFile.txt | openssl dgst -md5 -binary | xxd -ps -c16)', 16))" > hash.bin.int.txt
Sign up to request clarification or add additional context in comments.

Comments

0

The OP says he wants binary and then says "Binary should be 0s and 1s right?". Although, probably unlikely, if he really wants a binary number output, do this:

$ echo -e "xx\nyy" >in.txt
$ perl -ln0777e 'use Digest::MD5 "md5"; print "0b".unpack("B*",md5($_))' <in.txt
0b001111000101011000010000100101100011010101000101111000101000000011110110011010110011010000!

Meaning of above:

  • slurp in whole file into $_, calculate md5 digest on $_ to create binary data
  • use unpack to convert binary data to binary number string

If he really wants binary data, modify the above:

$ perl -ln0777e 'use Digest::MD5 "md5"; print md5 $_' <in.txt >binout.txt

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.