How is the hash for a file calculated?

Is the file just read bit by bit to calculate the hash with those values or is there more to it?

Are there any resources to read a bit more about file hashing?

• The question is only loosely related to cryptography. Crypto algorithms work on binary data, how the data is stored before it's submitted as input to a hash algo - in a file, in a memory buffer or otherwise - is irrelevant. Programming questions are better suited for StackOverflow. – tum_ Apr 26 '19 at 21:27
• @tum_ Idk, I think it doesn't really fit into either of them but i felt like this here was a little bit better suited... – b3nj4m1n Apr 26 '19 at 21:30
• Well, to be honest, it is still not clear what exactly you are looking for. So, now that you have the feedback, you might consider editing your question and be more specific, – tum_ Apr 26 '19 at 21:34
• What is unclear about it? What do i need to give to the alg to get the file hash. – b3nj4m1n Apr 26 '19 at 21:35
• Yeah, now it's finally clear what you were after. I can't name any off the top of my head but I can imagine certain crypto applications built on top of SHA-2 where the metadata would be involved. But sha-1,2,3 on its own just processes a bit string. – tum_ Apr 26 '19 at 22:02

Hash functions operate on blocks like SHA-256 has 512-bit input block. In SHA-256, your data $$m$$ is divided into 512-bits blocks $$m_1,\ldots,m_n$$ where each entered into the hash function. The final block, $$m_n$$ is padded as

$$k=(447−l) \bmod 512$$ where data size is $$l$$ and $$k$$ is the number of zeros following the 1 after $$m$$ and finally the message size in 64 bits. The message size can also 128 which allows larger files to be hashed. So the input to SHA-256 is

$$m\|1\|0\ldots0\|\text{size_of(m)}.$$

Each other hash function defined in a similar way, an example from FRC4634

The hash functions specified herein are used to compute a message digest for a message or data file that is provided as input. The message or data file should be considered to be a bit string. The length of the message is the number of bits in the message (the empty message has length 0). If the number of bits in a message is a multiple of 8, for compactness we can represent the message in hex. The purpose of message padding is to make the total length of a padded message a multiple of 512 for SHA-224 and SHA-256 or a multiple of 1024 for SHA-384 and SHA-512.

Note: The 64-bit (or 128-bit) message size also determines the maximum input size for a SHA-256 hash.

• So it's just the file in binary? – b3nj4m1n Apr 26 '19 at 21:05
• Always. What you else you expect? – kelalaka Apr 26 '19 at 21:06
• Idk that's why I asked the question. – b3nj4m1n Apr 26 '19 at 21:07
• if you can verify the answer how come you didn't discover the answer yourself? :-) – kodlu Apr 26 '19 at 21:17
• The RFC4634 explains in details, and you can find test vector at Nist – kelalaka Apr 26 '19 at 21:23

Not quite. File hashing is a way of creating a unique identifier for a specific input with incredibly low chance of collision. A collision happens when two inputs map to the same output. So far we have not seen any cases of collision for SHA256 or SHA3; two of the most prominent hashing algorithms. In terms of what goes on inside a hashing algorithm, its not just mapping one bit to another bit. There can be different methods including (but not limited to) bit substitution, logical operations, and many other bitwise operations. A good hashing algorithm should be complex enough that two very similar inputs map to very different outputs, again making it a unique identifier.

For example: SHA256(0000000) = ABD4562B6AEE9F SHA256(0000001) = 255FBA9C5C33ED

• Just to clearify, I know how sha256 works. I just need to know what to put inside of it to get the hash for a file. Also, isn't SHA-3 a family of algorithms? Anyways, i'm sorry but I don't really see an answer to my question in your answer.... Thanks anyways. – b3nj4m1n Apr 26 '19 at 21:22
• It's "collision", not "collusion" – Ella Rose Apr 26 '19 at 22:52
• @b3nj4m1n Yes, SHA-3 is a family of algorithms, but Ronald is still correct in saying that we haven't seen any collisions for any of the SHA-3 hashes. – forest Apr 27 '19 at 6:28