How SHA-1 compresses a padded message

Short answer: SHA-1 maps a message to a 20-byte digest. It pads like SHA-256, with a 1 bit, zeros, and the length, then runs eighty rounds on five 32-bit words. The digest of abc is a9993e364706816aba3e25717850c26c9cd0d89d. Distinct messages can share a SHA-1 digest. A collision is practical, so the function is not a collision-resistant hash.


What the problem actually asks

SHA-1 answers the same padding-and-compression question as SHA-256, with a shorter state and a different round function. The eighty rounds use four Boolean functions and four constants. The message schedule rotates a xor of four earlier words. The output is 160 bits. Collision resistance is the property a signature needs from a hash, and SHA-1 does not provide it. The fragment computes the digest. It does not construct a colliding pair.


What the algorithm guarantees

The padding includes the length, and the compression is completely determined by the message. Two inputs that collide produce one digest, and such inputs exist and can be built. A checksum that only has to notice an accidental flipped bit still changes when the bit flips. That is a weaker job than collision resistance. A signature, a certificate, and a password store need the stronger job, which SHA-1 does not do.


The working steps

  1. Start from the five fixed initial words.
  2. Pad with 0x80, zeros, and the 64-bit length.
  3. Expand each block to eighty words and run the four groups of twenty rounds.
  4. Add the working words back onto the five-word state.

Approaches that look simpler and fail

Approach Why it fails when a collision must be infeasible
Use SHA-1 inside a new signature A collision becomes two messages with one signature. A new signature hashes with a collision-resistant function.
Shorten SHA-256 and call the prefix SHA-1 SHA-1 is its own eighty-round function. It is not a prefix of SHA-256.
Keep SHA-1 because a random edit still changes the digest Accidental damage and a chosen collision are different events. The chosen collision is the one that is practical.
Store passwords as SHA-1 digests The function is fast, and it is not collision-resistant. A password store uses a salt and a cost.

SHA-256 is the 256-bit hash to use in this family. SHA-1 is the retired 160-bit compression.


A working example

This Python fragment runs the SHA-1 compression on the bytes abc. It prints a9993e364706816aba3e25717850c26c9cd0d89d.

import struct

def _rotr(x: int, n: int) -> int:
    return ((x >> n) | (x << (32 - n))) & 0xFFFFFFFF

def _rotl(x: int, n: int) -> int:
    return ((x << n) | (x >> (32 - n))) & 0xFFFFFFFF

def sha1(data: bytes) -> bytes:
    h0, h1, h2, h3, h4 = 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0
    bit_len = len(data) * 8
    msg = data + b"\x80"
    msg += b"\x00" * ((56 - len(msg) % 64) % 64)
    msg += struct.pack(">Q", bit_len)
    for off in range(0, len(msg), 64):
        w = list(struct.unpack(">16I", msg[off : off + 64]))
        for i in range(16, 80):
            w.append(_rotr(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 31))
        a, b, c, d, e = h0, h1, h2, h3, h4
        for i in range(80):
            if i < 20:
                f, k = (b & c) | (~b & d), 0x5A827999
            elif i < 40:
                f, k = b ^ c ^ d, 0x6ED9EBA1
            elif i < 60:
                f, k = (b & c) | (b & d) | (c & d), 0x8F1BBCDC
            else:
                f, k = b ^ c ^ d, 0xCA62C1D6
            a, b, c, d, e = (_rotl(a, 5) + f + e + k + w[i]) & 0xFFFFFFFF, a, _rotl(b, 30), c, d
        h0 = (h0 + a) & 0xFFFFFFFF
        h1 = (h1 + b) & 0xFFFFFFFF
        h2 = (h2 + c) & 0xFFFFFFFF
        h3 = (h3 + d) & 0xFFFFFFFF
        h4 = (h4 + e) & 0xFFFFFFFF
    return struct.pack(">5I", h0, h1, h2, h3, h4)

digest = sha1(b"abc")
assert digest.hex() == "a9993e364706816aba3e25717850c26c9cd0d89d"
print(digest.hex())

Where it is used

Git still names most objects by SHA-1, and the project is moving those ids to SHA-256. Old TLS certificates used SHA-1 as well. A collision is practical, so a new signature does not use this digest.


When you can drop this algorithm

Drop SHA-1 wherever a collision would matter, including signatures, certificates, and password storage. The compression function remains a historical example.


Why this stays published

We publish this so a search for SHA-1 returns the eighty-round compression and the reason a collision matters. SHA-1 compresses a padded message to 20 bytes in eighty rounds, and a collision on that digest is practical.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *