DevGizmo
Back to Blog
cryptography·

SHA-256 and SHA-512 Hashing Explained: How Cryptographic Hash Functions Work

Cryptographic hash functions like SHA-256 and SHA-512 produce a fixed-size fingerprint of any data. Learn how they work, why they are one-way, and how they are used in password storage, data integrity, and digital signatures.

sha256hashingcryptographysecurity

What Is a Cryptographic Hash Function?

A cryptographic hash function takes an input of any size and produces a fixed-size output called a digest or hash. Three properties make a hash function "cryptographic":

  1. Deterministic — the same input always produces the same output
  2. One-way (pre-image resistant) — given a hash, it is computationally infeasible to find the original input
  3. Collision resistant — it is computationally infeasible to find two different inputs that produce the same hash

A fourth useful property is the avalanche effect: a small change in the input (even a single bit) produces a completely different hash. This makes tampering evident.

SHA-2: The Industry Standard

SHA-2 (Secure Hash Algorithm 2) is a family of hash functions designed by the NSA and published in 2001. The two most widely used variants are:

AlgorithmOutput sizeSpeedUse cases
SHA-256256 bits (64 hex chars)FastChecksums, certificates, Bitcoin proof-of-work
SHA-512512 bits (128 hex chars)Faster on 64-bit CPUsPassword hashing intermediaries, JWTs

Example SHA-256 hash of "Hello, World!":

dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986d

Why SHA-256 Is Not Suitable for Password Hashing

While SHA-256 is excellent for data integrity checks, it is unsuitable for directly hashing passwords. The problem: SHA-256 is designed to be fast. An attacker with modern hardware can compute billions of SHA-256 hashes per second, enabling brute-force and rainbow table attacks.

For passwords, use a deliberately slow algorithm:

  • bcrypt — widely supported, built-in work factor
  • Argon2 — winner of the 2015 Password Hashing Competition, recommended for new projects
  • scrypt — memory-hard, good against FPGAs/ASICs

These algorithms have a configurable cost factor that can be increased as hardware improves.

HMAC: Keyed Hashing

A plain hash does not prove who computed it. HMAC (Hash-based Message Authentication Code) combines a secret key with the hash algorithm to produce an authentication code:

HMAC-SHA256(key, message) = SHA256((key XOR opad) || SHA256((key XOR ipad) || message))

HMAC is used in:

  • API request signing — the server and client share a secret key; the client signs each request
  • JWT signatures — the HS256 algorithm in JSON Web Tokens is HMAC-SHA256
  • Cookie integrity — Django and other frameworks use HMAC to sign session cookies

Using SHA-256 in JavaScript

The Web Crypto API provides native SHA hashing without any library:

async function sha256(message) {
  const msgBuffer = new TextEncoder().encode(message);
  const hashBuffer = await crypto.subtle.digest("SHA-256", msgBuffer);
  const hashArray = Array.from(new Uint8Array(hashBuffer));
  return hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
}

const hash = await sha256("Hello, World!");
// "dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986d"

In Node.js:

const { createHash } = require("crypto");

createHash("sha256").update("Hello, World!").digest("hex");

File Integrity with SHA-256

SHA-256 checksums are widely used to verify downloaded files:

# Generate a checksum
sha256sum file.zip

# Verify against a published checksum
echo "dffd6021...  file.zip" | sha256sum --check

When you download software from a trusted source, the publisher often provides a .sha256 file. If the computed hash of your download matches the published hash, the file was not corrupted or tampered with in transit.

SHA-1 and MD5: Deprecated

SHA-1 (160-bit output) and MD5 (128-bit output) are older hash algorithms that are now broken for security purposes — practical collision attacks exist for both. SHA-1 was deprecated by browsers in TLS certificates in 2017. MD5 should only be used for legacy compatibility or non-security-critical checksums.

Always use SHA-256 or SHA-512 for new security-sensitive code.

Try it yourself

Put these concepts into practice with the free online tool on DevGizmo.