1

So for a school assignment we are working with hashes. However I've encountered a problem where my file.txt containing:

test hashes
test hashes

without quotes doesn't match the hash from HashCalc (for windows) and http://www.md5hashgenerator.com/ both come up with cd7e8e88f33efb42e0a1148e92c5005b while md5sum on my kali linux comes up with f3c5fdf4320346eb9bd2a6b64235248e using

head -c -1 file.txt | md5sum

It works fine with just one line of test hashes, but with the second line I can't make it match.

James
  • 13
  • 1
  • 3
  • 1
    `cd7e8e88f33efb42e0a1148e92c5005b` is the md5 hash for both lines combined with a newline character which seperates the first line from the second. – Ramhound Mar 27 '17 at 20:57
  • You have not told us what MD5 hash you actually are expecting. – Ramhound Mar 27 '17 at 21:02
  • I am expecting the one ending in 5005b to come up in md5sum and I want to know why they don't match. Isn't the algorithm the same? – James Mar 27 '17 at 21:06
  • If you are expecting the 5005b, then your command isn't correct. You currently are only getting a single line, whereas, you would need both lines to generated 5005b. Your current command is only reading 1 byte. You should be using the -n option not the -c option. You should provide us the string that generated 35248e – Ramhound Mar 27 '17 at 21:11
  • [The head Command](http://www.linfo.org/head.html) – Ramhound Mar 27 '17 at 21:11
  • actually that command is reading all but one byte. the -c -1 is to take off the newLine after the file. The command works with one line in the file, but multiple lines don't match. echo -n "test hashes" | md5sum and my previous code match with just one line in the file. – James Mar 27 '17 at 21:15
  • after a little test I think -c1 takes the first byte and -c -1 takes the last. Someone else suggested it to remove the newLine so I'm not sure how it works, but it does with only one line in the text file. Thanks to Kamil's answer I now know why they don't match with the newLines. The problem now is to make it see a \r\n for the newLines in an entire .txt file. – James Mar 27 '17 at 21:33
  • I knew it was something like that. – Ramhound Mar 27 '17 at 22:02

1 Answers1

3
$ echo -ne "test hashes\ntest hashes" | md5sum                                   
f3c5fdf4320346eb9bd2a6b64235248e  -
$ echo -ne "test hashes\r\ntest hashes" | md5sum                                 
cd7e8e88f33efb42e0a1148e92c5005b  -

It's about Unix (\n) vs. DOS line ending (\r\n).

You can convert Unix line endings to DOS ones with unix2dos:

$ echo -ne "test hashes\ntest hashes" | unix2dos | md5sum
cd7e8e88f33efb42e0a1148e92c5005b  -

The reverse command is dos2unix.

Kamil Maciorowski
  • 69,815
  • 22
  • 136
  • 202
  • Now we're getting somewhere. That explains why it isn't working so how would I add the \r into an entire file so the hash is the same for a .txt file windows and linux? EDIT: I mean a file created in windows and a file created in linux – James Mar 27 '17 at 21:19
  • Sorry confusing question. better phrased: What terminal command can I use to get the 5005b hash in linux for a file rather than an echo? – James Mar 27 '17 at 21:28
  • @James I have expanded my answer. – Kamil Maciorowski Mar 27 '17 at 21:40
  • Perfect that unix2dos is exactlywhat i was missing – James Mar 27 '17 at 22:50