A cryptographic hash function is a function that takes a string of bytes of variable length and returns a fixed-length digest such that it is extremely difficult to find two inputs that yield the same output or find the original input given an output. It is also desirable for a small change in input to yield a large change in output. Common hash functions include MD5, SHA-1, SHA-256, SHA-512, and RIPEMD.
A cryptographic hash function is a function that takes a string of bytes of variable length for input and returns a fixed number of bytes (the exact number depending on the function) such that:
- It is extremely difficult to find an input given an output.
- It is extremely difficult to find another input that gives the same output.
- Preferably, a small change in input results in a large change in output. (the avalanche effect)
Common hash functions include:
- MD5 (128-bit output, tag: md5; vulnerabilities found)
- SHA-1 (160-bit output, tag: sha-1; vulnerabilities found)
- SHA-2, including SHA-512 (512-bit output, tag: sha-512)
- RIPEMD-160 (160-bit output, tag: ripemd)
There are also some other notable hash functions which are less mature:
- SHA-3/Keccak, winner of the NIST hash function competition (tag: sha-3)
Applications
While there are many uses for cryptographic hash functions, here are a selected few:
- In combination with a salt, it can be used to store passwords. (tag: saltedhash)
- It can be used as part of an HMAC. (tag: hmac)
Related Questions
- What are the important points about cryptographic hash functions?
- Which cryptographic hash function should I choose?
Usage in Various Languages
In Python, one can use the hashlib
module:
import hashlib
hashlib.md5(b'Stack Overflow').hexdigest() # => fcb3cd0c1efd8afe09d0505e46cefa4c
hashlib.sha1(b'Stack Overflow').hexdigest() # => 47dd9a3d44ce99e3bbd366c9c91a60fc96281e66
hashlib.sha256(b'Stack Overflow').hexdigest() # => 37a04a3fbe1f4424d5ed539c213b8f56ff07dcf9d94a360fa8b4efb69d22c1ede93e1dc4928760bf84ffe5b176babb60344b1a10bda218aaa35eb6388a9db3ec
In Ruby, one can use digest
:
require 'digest'
Digest::MD5.new.update('Stack Overflow').hexdigest # => fcb3cd0c1efd8afe09d0505e46cefa4c
Digest::SHA1.new.update('Stack Overflow').hexdigest # => 47dd9a3d44ce99e3bbd366c9c91a60fc96281e66
Digest::SHA2.new(512).update('Stack Overflow').hexdigest # => 37a04a3fbe1f4424d5ed539c213b8f56ff07dcf9d94a360fa8b4efb69d22c1ede93e1dc4928760bf84ffe5b176babb60344b1a10bda218aaa35eb6388a9db3ec
Digest::RMD160.new.update('Stack Overflow').hexdigest # => 4a275d3d6a77a69a7e0bd06241d35378348b7438
In C#, one can use System.Security.Cryptography.HashAlgorithm
:
using System.Security.Cryptography;
byte[] input = /* ... */;
HashAlgorithm.Create("MD5").ComputeHash(input); // => array of bytes
HashAlgorithm.Create("SHA1").ComputeHash(input); // => array of bytes
HashAlgorithm.Create("SHA512").ComputeHash(input); // => array of bytes
HashAlgorithm.Create("RIPEMD160").ComputeHash(input); // => array of bytes
In Java, one can use java.security.MessageDigest
:
import java.security.MessageDigest;
byte[] input = /* ... */;
// getInstance can throw NoSuchAlgorithmException if the algorithm is unsupported
MessageDigest md = MessageDigest.getInstance("MD5" /* or any other algorithm */);
md.digest(input); // => array of bytes
Many other languages have mechanisms for hashing; see Rosetta Code for more: