Hash Function

Hash function can map bytes to another ONE WAY only but not the other way around. Common hash functions are SHA2, SHA3, MD5, Blake2… Modern hash functions such as SHA2, SHA3, Blake2 are consider secure hash functions. Old funtions such as MD5 and SHA1 are not secure since collisions found, and should avoid using them.

Hash functions are commonly used to proof the content not tampered, for example if you download an executable file form internet, you should compare the hash provided by the site and the one caclulated locally. Collisions found will indicate the function is not secure anymore, for example if someone hijack the content and replace with another malware which can calculate to the same hash.

ghci> import Crypto.Hash
ghci> hash ("hello world"::ByteString) :: Digest SHA1
ghci> hash ("hello world"::ByteString) :: Digest MD5
ghci> hash ("hello world"::ByteString) :: Digest SHA256
ghci> hash ("hello world"::ByteString) :: Digest SHA3_256
ghci> hash ("hello world"::ByteString) :: Digest Blake2s_256
ghci> hash ("hello world"::ByteString) :: Digest Blake2b_256

The number 256 in SHA and Blake indicates the output bits length, usually more bits means higher collisions resistance.

Hashing is NOT encryption!!! DO NOT store hash of password in database. Although hash function is not reversible, if I have a large enough dictionary, I can definitly tell from database the password 5eb63bbbe01eeed093cb22bb8f5acdc3 is hello world

There is example of Blake2b of “abc” and C implementation in rfc7693 1

Links to this note