Technology

What a hash is (MD5, SHA-1, SHA-256), what it is for and why it is not encryption

What a hash function does, how MD5, SHA-1 and SHA-256 differ, how to verify a download in the terminal and in Node.js, and why it is not for passwords.

A hash is the result of running any data (a text, a file, a whole disk) through a hash function: a mathematical operation that always returns a fixed-length string, such as b221d9db… for the word hola with SHA-256. It is deterministic (the same input always gives the same hash), any tiny change in the input changes the result completely and, with current algorithms, it is not feasible to recover the input from the hash or to craft two inputs with the same one. That is why it is used to verify the integrity of a download, detect duplicates or sign data, and why it is not encryption: there is no key and no way to “undo” it.

This article explains how it works, how MD5, SHA-1 and SHA-256 differ, how to compute and compare hashes in the terminal, in Node.js and in the browser, and the two most common mistakes: using it as encryption and using it as-is for passwords.

What properties a hash has

A cryptographic hash function satisfies, according to the NIST Secure Hash Standard, three properties:

  1. Preimage resistance: given a hash, it is not feasible to find an input that produces it.
  2. Second-preimage resistance: given an input, it is not feasible to find a different one with the same hash.
  3. Collision resistance: it is not feasible to find any two inputs with the same hash.

It also has an avalanche effect: changing one letter changes the whole hash. Compare hola and Hola with SHA-256:

hola  → b221d9dbb083a7f33428d7c2a3c3198ae925614d70210e28716ccaa7cd4ddb79
Hola  → e633f4fc79badea1dc5db970cf397c8248bac47cc3acf9915ba60b5d76b0e88f

And the empty string has a hash too (worth recognising, because it shows up when something fails and “nothing” gets hashed):

""    → e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855

MD5, SHA-1, SHA-256: which to use

Algorithm Length Status What it is still good for
MD5 128 bits (32 hex) Broken for collisions since 2004; RFC 6151 advises against it wherever collision resistance matters Detecting duplicates or accidental file corruption; legacy checksums
SHA-1 160 bits (40 hex) Broken: Google and CWI published the first practical collision in 2017; NIST is retiring it for signatures Git’s internal identifiers (already migrating to SHA-256)
SHA-256 256 bits (64 hex) Recommended (SHA-2 family) Download verification, signatures, TLS certificates, blockchains
SHA-512 512 bits (128 hex) Recommended (SHA-2) Same as SHA-256; faster on 64-bit CPUs
SHA-3 variable Recommended (alternative standard, 2015) When independence from the SHA-2 construction is wanted

Rule of thumb: for any new use, SHA-256. MD5 and SHA-1 only when comparing against a hash you are given in that format (many download sites still publish MD5) and the threat is accidental corruption, not an attacker.

The hashes of hola with the three algorithms, so you can check your tool:

MD5     4d186321c1a7f0f354b297e8914ab240
SHA-1   99800b85d3383e3a2fb45eb7d0066a4879a9dad0
SHA-256 b221d9dbb083a7f33428d7c2a3c3198ae925614d70210e28716ccaa7cd4ddb79

What a hash is used for

  • Verifying a download. The provider publishes the file’s SHA-256; you compute it on your machine and compare. If it matches, the file arrived intact and is the one they published (as long as you obtained the hash through a trustworthy channel, usually HTTPS or a signature).
  • Detecting changes and duplicates. Backup, deduplication and sync systems compare hashes instead of whole files.
  • Identifying content. Git identifies every commit and file by its hash; CDNs put hashes in file names (app.3f9a1c.js) to invalidate the cache only when the content changes.
  • Digital signatures and certificates. What gets signed is not the document but its hash.
  • HMAC and tokens. A JWT with HS256 is signed with an HMAC-SHA256: a hash combined with a secret key.

How to compute a hash

In the terminal

# Linux (coreutils)
sha256sum file.iso
md5sum file.iso

# macOS
shasum -a 256 file.iso
md5 file.iso

# Windows (PowerShell)
Get-FileHash file.iso -Algorithm SHA256

To compare against the published hash without reading 64 characters by eye, save the official one to a file and let the tool compare:

echo "b221d9db…  file.iso" > file.iso.sha256
sha256sum -c file.iso.sha256
# file.iso: OK

In Node.js

import { createHash } from 'node:crypto';
import { createReadStream } from 'node:fs';

// Text
createHash('sha256').update('hola').digest('hex');
// 'b221d9dbb083a7f33428d7c2a3c3198ae925614d70210e28716ccaa7cd4ddb79'

// Large file, without loading it whole into memory
const hash = createHash('sha256');
for await (const chunk of createReadStream('file.iso')) hash.update(chunk);
console.log(hash.digest('hex'));

In the browser

The SubtleCrypto API computes SHA-256 without libraries (it deliberately leaves out MD5, because it is broken):

async function sha256(text) {
  const bytes = new TextEncoder().encode(text);
  const buffer = await crypto.subtle.digest('SHA-256', bytes);
  return [...new Uint8Array(buffer)].map((b) => b.toString(16).padStart(2, '0')).join('');
}
await sha256('hola'); // 'b221d9db…'

If you only need to check a file or a text once, the hash generator on AIMRAN Tools computes MD5, SHA-1, SHA-256 and SHA-512 in the browser, including dragged-in files, without uploading anything to any server.

The two most common mistakes

1. Confusing hashing with encryption

Encrypting is reversible with a key; hashing is irreversible by design. A hash does not “protect” a value: if the value has few possibilities (an ID number, a phone number, a short password), anyone can hash every option and compare. Hashing an email address does not anonymise it.

2. Storing passwords with a fast hash

SHA-256 is designed to be fast, which is exactly the opposite of what you want when storing passwords: a GPU computes billions of SHA-256 per second. The OWASP guide recommends functions designed to be slow and tunable: Argon2id (first choice), scrypt or bcrypt, always with a unique salt per user (all three build it in). A bare sha256(password), even with a salt, is considered inadequate today.

Conclusion

A hash is a fixed-length fingerprint: deterministic, sensitive to any change and irreversible. Use SHA-256 to verify downloads, detect changes and identify content; always compare against a hash obtained through a trustworthy channel; remember that MD5 and SHA-1 only hold up against accidental errors, not attackers; and for passwords, forget fast hashes and use Argon2id, scrypt or bcrypt.

Sources and references

  1. FIPS 180-4: Secure Hash Standard (NIST) csrc.nist.gov
  2. NIST: Transitioning the Use of Cryptographic Algorithms and Key Lengths (SP 800-131A Rev. 2) csrc.nist.gov
  3. RFC 6151: Updated Security Considerations for the MD5 Message-Digest and the HMAC-MD5 Algorithms rfc-editor.org
  4. Google Security Blog: Announcing the first SHA1 collision (2017) security.googleblog.com
  5. OWASP Cheat Sheet: Password Storage cheatsheetseries.owasp.org
  6. MDN: SubtleCrypto.digest() developer.mozilla.org

Related tools

Herramientas gratuitas de AIMRAN Tools que funcionan en tu navegador, sin registro.

Ver todas las herramientas
  • Hash generator

    Computes MD5, SHA-1, SHA-256 and SHA-512 of a text or a file in the browser, without uploading anything.

  • Base64, URL and HTML encoder

    Encode and decode Base64, URL and HTML entities in the browser.

Articles in Technology