Hash functions map an input of any size to a fixed-size value called a hash or digest that acts as a fingerprint for the input value.
Cryptographic hash functions are hash functions that must satisfy more strict requirements that enable them to withstand attacks. They are useful for
You might want to use hashes to make sure a piece of software you are downloading has not been tampered with.
Let's look at the AWS EKS kubectl
CLI which can be downloaded from
here.
First we download the software
curl -O https://s3.us-west-2.amazonaws.com/amazon-eks/1.29.0/2024-01-04/bin/darwin/amd64/kubectl
Then, we download the hash made available by the publisher of the software
curl -O https://s3.us-west-2.amazonaws.com/amazon-eks/1.29.0/2024-01-04/bin/darwin/amd64/kubectl.sha256
The downloaded hash should look like this
08198f2e1c425a7aa4f6c8a9f19772afeaf87d6f84c8f3e8ae7dfbb28291b341
Then, we compute the hash of the software locally using the same algorithm that the publisher used, SHA-256 in this case. You can use a tool such as OpenSSL in the following example.
openssl sha1 -sha256 kubectl
If the downloaded hash and the locally computed hash match, then we can safely assume that the software has not been tampered with.
When including resources such as CSS and script files from an HTML page, you have the option of specifying an integrity value -- a hash value for the file to be included.
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
crossorigin="anonymous"
>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
crossorigin="anonymous"
></script>
When the browser downloads the file, it automatically computes a hash value using the same algorithm and compares the two values. If they do not match, it means that someone has tampered with the file.
https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity
Advanced Intrusion Detection Environment (AIDE) is an open-source intrusion detection tool that maintains a database of hashes of files and directories to figure out if any of them have been tampered with.