SHA256 generation different for file and content of this file

Viewed 1792

I use online SHA256 converters to calculate a hash for a given file. There, I have seen an effect I don't understand.

For testing purposes, I wanted to calculate the hash for a very simple file. I named it "test.txt", and its only content is the string "abc", followed by a new line (I just pressed enter).

Now, when I put "abc" and newline into a SHA256 generator, I get the hash

edeaaff3f1774ad2888673770c6d64097e391bc362d7d6fb34982ddf0efd18cb

But when I put the complete file into the same generator, I get the hash

552bab6864c7a7b69a502ed1854b9245c0e1a30f008aaa0b281da62585fdb025

Where does the difference come from? I used this generator (in fact, I tried several ones, and they always yield the same result):

https://emn178.github.io/online-tools/sha256_checksum.html

Note that this different does not arise without newlines. If the file just contains the string "abc", the hash is

ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad

for the file as well as just for the content.

3 Answers

As noted in my comment, the difference is caused by how newline characters are represented across different operating systems (see details here):

  • On UNIX and UNIX-like systems, newlines are represented by a line feed character (\n).
  • On DOS and Windows systems, newlines are represented by a carriage return followed by a line feed character (\r\n).

Compare the following two commands and their output, corresponding to the SHA256 values in your question:

echo -en "abc\n" | sha256sum
edeaaff3f1774ad2888673770c6d64097e391bc362d7d6fb34982ddf0efd18cb

echo -en "abc\r\n" | sha256sum
552bab6864c7a7b69a502ed1854b9245c0e1a30f008aaa0b281da62585fdb025

Even i faced same issue. but providing the data in hex mode helped to understand the actual behavior. enter image description here

Canonicalization of data needs to be performed before SHA calculations which will eliminate such issues. Canonicalization needs to be performed both at Generation side and also at verification side.

Related