Sunday, November 22, 2026
HomeC ProgrammingExamined Code in C, C++, Java, Python, C

Examined Code in C, C++, Java, Python, C


Two completely different 128-byte inputs, differing in simply six bytes, produce the identical MD5 digest: 79054025255fb1a26e4bc422aef54eb4. The identical two inputs produce completely different SHA-256 digests. Each details are printed by the packages on this web page, and collectively they clarify why MD5 left safety work years in the past whereas SHA-256 stayed.

This information builds MD5, SHA-1 and SHA-256 from scratch in C and C++, reveals the identical digests from the usual libraries of Java, Python and C#, after which breaks them on objective: an actual collision, a length-extension forgery and three bugs that silently produce incorrect hashes. Each program was compiled and run for this text on Ubuntu 24.04 with GCC 13.3 and Clang 18 (-std=c11, -std=c17, -std=c++17, -std=c++20, warnings enabled), OpenJDK 21, Python 3.13 and the .NET 8 SDK, and all 5 languages print the identical digests. The exams verify them towards RFC and FIPS vectors, and the C code additionally runs clear below AddressSanitizer and UndefinedBehaviorSanitizer.

The Quick Reply

Which hash ought to I exploit? SHA-256 for fingerprints and integrity checks, HMAC-SHA-256 to authenticate a message with a secret, and Argon2id for passwords. MD5 and SHA-1 solely belong in code that should match an present format and doesn’t defend towards an attacker.

Is MD5 damaged? For collision resistance, sure: anybody can produce two inputs with the identical MD5 (this system within the collisions part does it with 128 bytes). For catching unintentional corruption it nonetheless works.

Ought to I write my very own? Write one to find out how hashing works, and use OpenSSL or your platform’s library for something actual. The testing part reveals the way to show your individual model right, and the efficiency desk reveals what it prices.

What Is a Message Digest?

A message digest is a fixed-size fingerprint {that a} hash perform computes from knowledge of any size. The identical enter all the time provides the identical digest, a tiny change to the enter modifications the digest fully, and a safe hash perform makes it infeasible to recuperate the enter or to search out two inputs that share a digest.

A digest is just not encryption and never encoding. Base64 is reversible by design; a digest throws info away, so there’s nothing to decode. RFC 1321 defines MD5 and the FIPS 180-4 customary defines SHA-1 and SHA-256.

Facet MD5 SHA-1 SHA-256
Digest dimension 128 bits (16 bytes) 160 bits (20 bytes) 256 bits (32 bytes)
Block dimension 64 bytes 64 bytes 64 bytes
Steps per block 64 80 64
Printed 1992 (RFC 1321) 1995 2002 (FIPS 180-2)
Collisions Sensible since 2004 Sensible since 2017 None recognized
Size extension Susceptible Susceptible Susceptible
Standing Non-security checksums solely NIST retires it by 31 December 2030 Present

All three share one design, the Merkle–Damgård development, and that shared design is why one perform within the code beneath serves all three.

How the Three Hashes Work

Every hash cuts the message into 64-byte blocks and runs each block by way of a compression perform that updates a small inner state. The digest is the ultimate state. The final block wants padding so the message fills entire blocks, and the padding additionally data the message size.

The padding rule is identical for all three: append one 0x80 byte, then zeros till the size is 56 modulo 64, then the message size in bits as a 64-bit quantity. MD5 writes that quantity little-endian; SHA-1 and SHA-256 write it big-endian. Fifty-five bytes slot in one block with their padding. Fifty-six bytes want two, and that boundary is the place most hand-written implementations fail.

The C implementation places every part the three share in a single place. This excerpt from digest.c buffers enter into blocks, builds the padding and finishes the hash:

/* ---- The half all three share: buffer into 64-byte blocks, pad, end. ---- */

typedef void (*block_fn)(uint32_t h[8], const uint8_t block[64]);

static void md_update(digest_ctx *c, block_fn compress, const void *knowledge, size_t len)
{
    const uint8_t *p = knowledge;
    size_t used = (size_t)(c->bytes & 63);
    c->bytes += len;
    if (used != 0) {                       /* prime up a partly stuffed block first */
        size_t take = 64 - used;
        if (take > len)
            take = len;
        memcpy(c->buf + used, p, take);
        p += take;
        len -= take;
        if (used + take < 64)
            return;
        compress(c->h, c->buf);
    }
    for (; len >= 64; p += 64, len -= 64)  /* entire blocks straight from the enter */
        compress(c->h, p);
    memcpy(c->buf, p, len);                /* preserve the tail for subsequent time */
}

size_t digest_padding(uint64_t msg_len, int big_endian_length, uint8_t out[72])
{
    size_t zeros = (size_t)((55 - (msg_len & 63)) & 63);   /* in order that size + 1 + zeros = 56 mod 64 */
    size_t n = 1 + zeros;
    uint64_t bits = msg_len * 8;
    out[0] = 0x80;
    memset(out + 1, 0, zeros);
    for (int i = 0; i < 8; ++i)
        out[n + (size_t)i] = (uint8_t)(bits >> (big_endian_length ? 56 - 8 * i : 8 * i));
    return n + 8;
}

static void md_final(digest_ctx *c, block_fn compress, int big_endian, size_t phrases,
                     uint8_t *out)
{
    uint8_t pad[72];
    uint64_t msg_len = c->bytes;
    size_t n = digest_padding(msg_len, big_endian, pad);
    md_update(c, compress, pad, n);        /* the padding completes the final block */
    for (size_t i = 0; i < phrases; ++i) {
        if (big_endian)
            store_be32(out + 4 * i, c->h[i]);
        else
            store_le32(out + 4 * i, c->h[i]);
    }
    memset(c, 0, sizeof *c);
}

Every hash then provides solely its personal compression perform. MD5 runs 64 steps over 4 spherical capabilities, SHA-1 runs 80 steps over a message schedule, and SHA-256 runs 64 steps with the Ch, Maj and sigma capabilities. The MD5 sine constants and the SHA-256 spherical constants had been computed by gen/tables.py within the C repository (from sines and prime roots) and pasted into digest.c quite than typed from a e-book, and a mutation check that modifications one fixed confirms the exams discover.

Hashing in Every Language

The 5 packages beneath hash the identical three messages. C and C++ use the code on this repository. Java, Python and C# name their customary libraries, which is what it’s best to do in actual tasks. All 5 print an identical output.

C

/* digest_demo.c - MD5, SHA-1 and SHA-256 of three quick messages.
 * Construct: gcc -std=c11 -Wall -Wextra -pedantic digest_demo.c digest.c -o digest_demo
 */
#embrace <stdio.h>
#embrace <string.h>

#embrace "digest.h"

static void present(const char *label, const char *textual content)
{
    uint8_t d[SHA256_DIGEST_SIZE];
    char h[2 * SHA256_DIGEST_SIZE + 1];
    size_t n = strlen(textual content);

    printf("message: "%s" (%zu bytes)n", label, n);
    md5(textual content, n, d);
    digest_to_hex(d, MD5_DIGEST_SIZE, h);
    printf("  MD5     %sn", h);
    sha1(textual content, n, d);
    digest_to_hex(d, SHA1_DIGEST_SIZE, h);
    printf("  SHA-1   %sn", h);
    sha256(textual content, n, d);
    digest_to_hex(d, SHA256_DIGEST_SIZE, h);
    printf("  SHA-256 %sn", h);
}

int fundamental(void)
{
    present("", "");
    present("abc", "abc");
    present("The short brown fox jumps over the lazy canine", "The short brown fox jumps over the lazy canine");
    return 0;
}

C++

The C++17 model wraps the three hashes in a single class template, so hash<Sha256>(textual content) and hash<Md5>(textual content) share a single code path.

// digest_demo.cpp - MD5, SHA-1 and SHA-256 of three quick messages.
// Construct: g++ -std=c++17 -Wall -Wextra -Iinclude src/digest_demo.cpp -o digest_demo
#embrace <cstdio>
#embrace <string_view>

#embrace "digest.hpp"

static void present(std::string_view textual content)
{
    std::printf("message: "%.*s" (%zu bytes)n", static_cast<int>(textual content.dimension()), textual content.knowledge(), textual content.dimension());
    std::printf("  MD5     %sn", mycplus::to_hex(mycplus::hash<mycplus::Md5>(textual content)).c_str());
    std::printf("  SHA-1   %sn", mycplus::to_hex(mycplus::hash<mycplus::Sha1>(textual content)).c_str());
    std::printf("  SHA-256 %sn", mycplus::to_hex(mycplus::hash<mycplus::Sha256>(textual content)).c_str());
}

int fundamental()
{
    present("");
    present("abc");
    present("The short brown fox jumps over the lazy canine");
}

Java

import java.nio.charset.StandardCharsets;
import java.safety.MessageDigest;
import java.safety.NoSuchAlgorithmException;
import java.util.HexFormat;

/**
 * MD5, SHA-1 and SHA-256 of three quick messages, with the digests that java.safety
 * gives. Construct: javac -d construct src/*.java
 */
public last class DigestDemo {
    non-public DigestDemo() { }

    /** Hex digest of the UTF-8 bytes of textual content, for an algorithm identify reminiscent of "SHA-256". */
    static String digestHex(String algorithm, byte[] knowledge) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance(algorithm);
        return HexFormat.of().formatHex(md.digest(knowledge));
    }

    non-public static void present(String textual content) throws NoSuchAlgorithmException {
        byte[] knowledge = textual content.getBytes(StandardCharsets.UTF_8);
        System.out.print("message: "" + textual content + "" (" + knowledge.size + " bytes)n");
        System.out.print("  MD5     " + digestHex("MD5", knowledge) + "n");
        System.out.print("  SHA-1   " + digestHex("SHA-1", knowledge) + "n");
        System.out.print("  SHA-256 " + digestHex("SHA-256", knowledge) + "n");
    }

    public static void fundamental(String[] args) throws NoSuchAlgorithmException {
        present("");
        present("abc");
        present("The short brown fox jumps over the lazy canine");
    }
}

Python

"""MD5, SHA-1 and SHA-256 of three quick messages, with hashlib.

Run: python digest_demo.py
"""
import hashlib
import hmac


def digest_hex(algorithm: str, knowledge: bytes) -> str:
    """Hex digest of knowledge for a hashlib algorithm identify reminiscent of "sha256"."""
    return hashlib.new(algorithm, knowledge).hexdigest()


def tags_match(anticipated: str, obtained: str) -> bool:
    """Examine two hex digests in fixed time, as an actual verifier should."""
    return hmac.compare_digest(anticipated, obtained)


def present(textual content: str) -> None:
    knowledge = textual content.encode("utf-8")
    print(f'message: "{textual content}" ({len(knowledge)} bytes)')
    print(f"  MD5     {digest_hex('md5', knowledge)}")
    print(f"  SHA-1   {digest_hex('sha1', knowledge)}")
    print(f"  SHA-256 {digest_hex('sha256', knowledge)}")


def fundamental() -> None:
    present("")
    present("abc")
    present("The short brown fox jumps over the lazy canine")


if __name__ == "__main__":
    fundamental()

C#

The .NET analyzers flag MD5 and SHA-1 as damaged algorithms (guidelines CA5351 and CA5350) and the repository builds with analyzers as errors. The code suppresses every warning on the name with a purpose, which can also be good apply in your individual code: the suppression paperwork that the weak hash is deliberate.

utilizing System;
utilizing System.Safety.Cryptography;
utilizing System.Textual content;

namespace MyCPlus.Cryptography;

/// <abstract>MD5, SHA-1 and SHA-256 of three quick messages, with System.Safety.Cryptography.</abstract>
public static class Program
{
    // The analyzers flag MD5 and SHA-1 (CA5351, CA5350). They're used right here as a result of the web page is
    // about them, and every name says so with a suppression and a purpose.
    non-public static string Md5Hex(byte[] knowledge) =>
#pragma warning disable CA5351 // Don't use damaged cryptographic algorithms: proven on objective
        Convert.ToHexString(MD5.HashData(knowledge)).ToLowerInvariant();
#pragma warning restore CA5351

    non-public static string Sha1Hex(byte[] knowledge) =>
#pragma warning disable CA5350 // Don't use weak cryptographic algorithms: proven on objective
        Convert.ToHexString(SHA1.HashData(knowledge)).ToLowerInvariant();
#pragma warning restore CA5350

    non-public static string Sha256Hex(byte[] knowledge) =>
        Convert.ToHexString(SHA256.HashData(knowledge)).ToLowerInvariant();

    non-public static void Present(string textual content)
    {
        byte[] knowledge = Encoding.UTF8.GetBytes(textual content);
        Console.Out.Write($"message: "{textual content}" ({knowledge.Size} bytes)n");
        Console.Out.Write($"  MD5     {Md5Hex(knowledge)}n");
        Console.Out.Write($"  SHA-1   {Sha1Hex(knowledge)}n");
        Console.Out.Write($"  SHA-256 {Sha256Hex(knowledge)}n");
    }

    public static void Predominant()
    {
        Present("");
        Present("abc");
        Present("The short brown fox jumps over the lazy canine");
    }
}

Output (an identical in all 5 languages):

message: "" (0 bytes)
  MD5     d41d8cd98f00b204e9800998ecf8427e
  SHA-1   da39a3ee5e6b4b0d3255bfef95601890afd80709
  SHA-256 e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
message: "abc" (3 bytes)
  MD5     900150983cd24fb0d6963f7d28e17f72
  SHA-1   a9993e364706816aba3e25717850c26c9cd0d89d
  SHA-256 ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
message: "The short brown fox jumps over the lazy canine" (43 bytes)
  MD5     9e107d9d372bb6826bd81d3542a419d6
  SHA-1   2fd4e1c67a2d28fced849ee1bb76e7391b93eb12
  SHA-256 d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592

The empty-message row for every algorithm and the abc row come straight from RFC 1321 and FIPS 180-4. The third message is the basic pangram utilized in numerous paperwork.

How one can Check a Hash Perform

A hash with one incorrect fixed fails on virtually each enter, which makes hashing code simple to check and simple to belief too early. The check suite in every repository layers 4 checks:

  1. Printed recognized solutions. Sixteen digests from RFC 1321 and FIPS 180-4, together with the million-a message.
  2. Impartial vectors. 306 message lengths, from 0 to 305 bytes, hashed by Python’s hashlib and saved in exams/vectors/lengths.txt. The byte at place i is (i*31+7) & 0xFF, so the information are reproducible. The C and C++ exams additionally hash each message in 7-byte chunks and break up it at each offset as much as 130 bytes, which workout routines the buffering code.
  3. HMAC vectors. Ten circumstances from RFC 4231 and RFC 2202.
  4. A cross-check towards OpenSSL. On Linux, the C check compares 5,000 random messages and keys towards OpenSSL’s output for all three hashes and HMAC, 20,000 comparisons in whole.

The vector information are an identical in all 5 repositories, and gen/make_vectors.py within the C repository asserts each revealed digest towards hashlib earlier than it writes them.

Do the exams catch actual bugs?

A passing suite proves little till you break the code and watch it fail. The C check suite was run towards 27 deliberate mutants of digest.c: a modified spherical fixed, a shifted rotation, a padding size off by one, a dropped tail within the buffering code, a swapped HMAC pad byte. The C++ suite does the identical with 19 mutants. Each mutant was caught.

The extra attention-grabbing result’s which check caught what. Run on their very own, the 16 recognized solutions catch 21 of the 27 mutants, together with each compression-function and padding mutant. The six they miss are all in code the recognized solutions by no means attain: the three HMAC mutants and the three length-extension resume mutants. Including the HMAC vectors and a resume verify brings the depend to 27 of 27, and the 306-length sweep and every-offset splits didn’t catch a single mutant that these didn’t already catch.

That could be a unfavorable consequence value stating. The lengthy sweep nonetheless earns its place, as a result of boundary bugs are what hand-written hashes sometimes have and the sweep covers each size across the 55, 56, 64 and 65-byte boundaries. However the mutation run reveals that the most affordable safety is the revealed vectors, plus a check for each characteristic past plain hashing.

Three Errors That Break Hashing Code

Every of those produces a plausible-looking digest, which is why they survive into manufacturing. Every has a brief program within the pitfalls/ listing with captured output.

Printing by way of a signed char

On x86 and MSVC, plain char is signed. Print a byte above 0x7F by way of it with %02x and the worth sign-extends:

/* hex_sign_extension.c - printing a digest by way of a signed char buffer.
 * On x86, plain `char` is signed, and printf("%02x") of a unfavorable char prints ffffff90.
 */
#embrace <stdio.h>

#embrace "digest.h"

int fundamental(void)
{
    uint8_t digest[MD5_DIGEST_SIZE];
    md5("abc", 3, digest);

    signed char incorrect[MD5_DIGEST_SIZE];       /* what a plain char buffer is on x86 and MSVC */
    for (size_t i = 0; i < sizeof digest; ++i)
        incorrect[i] = (signed char)digest[i];

    printf("printed by way of signed char:n  ");
    for (size_t i = 0; i < 4; ++i)
        printf("%02x ", incorrect[i]);
    printf("...nprinted by way of uint8_t:n  ");
    for (size_t i = 0; i < 4; ++i)
        printf("%02x ", digest[i]);
    printf("...n");
    return 0;
}

Output:

printed by way of signed char:
  ffffff90 01 50 ffffff98 ...
printed by way of uint8_t:
  90 01 50 98 ...

Digest buffers must be uint8_t or unsigned char, all the time.

Calling strlen() on binary knowledge

A zero byte ends a C string, so strlen() stops on the first one. Right here a three-byte file hashes as one byte:

/* strlen_on_binary.c - hashing binary knowledge with strlen(). A zero byte ends the "size". */
#embrace <stdio.h>
#embrace <string.h>

#embrace "digest.h"

static void present(const char *label, const uint8_t *knowledge, size_t len)
{
    uint8_t d[SHA256_DIGEST_SIZE];
    char h[2 * SHA256_DIGEST_SIZE + 1];
    sha256(knowledge, len, d);
    digest_to_hex(d, SHA256_DIGEST_SIZE, h);
    printf("%-24s %2zu bytes  %sn", label, len, h);
}

int fundamental(void)
{
    const uint8_t file[] = { 'a', 0x00, 'b' };   /* three bytes, one in all them zero */
    present("hashed with strlen():", file, strlen((const char *)file));
    present("hashed with sizeof:", file, sizeof file);
    return 0;
}

Output:

hashed with strlen():     1 bytes  ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb
hashed with sizeof:       3 bytes  59b271ae1bbcb1d31d41929817f4b16fb439eb4f31520b5ad1d5ce98920a7138

Go the size explicitly every time the information is just not textual content.

Hashing the newline

fgets() retains the trailing newline, and "abcn" is a unique message from "abc". The pitfall program within the repository hashes each and the stripped line:

"abc":             ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
"abcn" (fgets):   edeaaff3f1774ad2888673770c6d64097e391bc362d7d6fb34982ddf0efd18cb
after stripping:   ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad

That is the commonest purpose a hash computed in code disagrees with the one from sha256sum or a web based software. When two digests differ and you can not see why, evaluate the byte lengths first.

Size Extension: Why H(secret + message) Is Not a Signature

A tempting design for signing a request is tag = SHA-256(secret + message). It fails as a result of the digest of a Merkle–Damgård hash is its last inner state. Anybody who holds the tag and is aware of the message size can load that state again right into a SHA-256 context, append extra knowledge and compute the legitimate tag for the longer message, with out ever seeing the key.

The one unknown is the key’s size, and the attacker merely tries every believable size. This system beneath forges a tag for person=alice&quantity=100 prolonged with &quantity=1000000:

Size extension: a legitimate tag with out the key 1. The server hashes secret + message and publishes the tag secret16 B message21 B padding27 B tag = state after block 1 e7da690a…b2241e83 2. The attacker resumes from that state and appends a suffix block 1: by no means seen, its output state is the tag suffix15 B padding49 B cast tag: ffd77dbc…719ddbf9 3. The server re-hashes secret + message + padding + suffix secret16 B message21 B padding27 B suffix15 B padding49 B tag: ffd77dbc…719ddbf9 (an identical, so the solid message is accepted) HMAC-SHA-256 hashes the important thing twice, so a broadcast tag is just not a resumable state.
Determine 1: The tag is the hash state after block 1. An attacker who is aware of the message and its tag can restart from that state, append a suffix and compute the tag the server will compute for the longer message.

The assault wants two items of code from the repository. sha256_resume() and md5_resume() load a digest again right into a context, and the loop beneath guesses the key size, resumes from the revealed tag and checks the consequence towards what the server would compute:

static void assault(const scheme *s, const char *message, const char *suffix)
{
    uint8_t tag[SHA256_DIGEST_SIZE], forged_tag[SHA256_DIGEST_SIZE];
    char hex_tag[2 * SHA256_DIGEST_SIZE + 1];
    size_t msg_len = strlen(message), suffix_len = strlen(suffix);

    signal(s, (const uint8_t *)message, msg_len, tag);
    digest_to_hex(tag, s->dimension, hex_tag);
    printf("%s: the server signed "%s" and revealed the tagn  %sn", s->identify, message, hex_tag);

    /* The attacker is aware of the message, the tag and the algorithm, however not the key
     * and even its size, so tries each believable size. */
    for (size_t guess = 1; guess <= 32; ++guess) {
        uint8_t glue[72], forged_msg[256];
        size_t glue_len = digest_padding(guess + msg_len, s->big_endian_length, glue);

        digest_ctx c;
        s->resume(&c, tag, guess + msg_len + glue_len);   /* proceed from the revealed tag */
        s->replace(&c, suffix, suffix_len);
        s->last(&c, forged_tag);

        memcpy(forged_msg, message, msg_len);              /* message || padding || suffix */
        memcpy(forged_msg + msg_len, glue, glue_len);
        memcpy(forged_msg + msg_len + glue_len, suffix, suffix_len);
        size_t forged_len = msg_len + glue_len + suffix_len;

        uint8_t anticipated[SHA256_DIGEST_SIZE];
        signal(s, forged_msg, forged_len, anticipated);         /* what the server computes */
        if (digest_equal(anticipated, forged_tag, s->dimension)) {
            digest_to_hex(forged_tag, s->dimension, hex_tag);
            printf("  cast message: ");
            print_escaped(forged_msg, forged_len);
            printf("n  cast tag:     %sn", hex_tag);
            printf("  the server accepts it (the attacker's guess of the key size: %zu)n", guess);
            return;
        }
    }
    printf("  no forgeryn");
}

Output:

SHA-256: the server signed "person=alice&quantity=100" and revealed the tag
  e7da690a0470ca564625a564b244ccb99cf48e787789a58ee5f54b8ab2241e83
  cast message: person=alice&quantity=100x80x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x01(&quantity=1000000
  cast tag:     ffd77dbc478dba778355e4e7e3dadf417de6498f1a3ead93dacc8c68719ddbf9
  the server accepts it (the attacker's guess of the key size: 16)

MD5: the server signed "person=alice&quantity=100" and revealed the tag
  f37ee67cd0ed5443f6c132f1072b1a04
  cast message: person=alice&quantity=100x80x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00(x01x00x00x00x00x00x00&quantity=1000000
  cast tag:     ca74f7c43aaed622f1fd96c88312434d
  the server accepts it (the attacker's guess of the key size: 16)

HMAC-SHA-256: the server signed "person=alice&quantity=100" and revealed the tag
  3420ed03142286a6ee0b807e904003be7ba31f9d73125450e0eae9492b156f1d
  the identical assault, guessing each secret size from 1 to 64: rejected each time

The cast message incorporates the signed textual content, then the 0x80 marker, zeros and the 64-bit size (x01( is 296 bits, the size of the key plus the message), then the attacker’s suffix. The assault works towards MD5 as properly, with the size written within the different byte order.

HMAC defeats it as a result of it hashes twice with keys derived from the key, so the revealed tag is the output of an outer hash, not a state the attacker can proceed from. The final block of output reveals the identical assault failing towards HMAC-SHA-256 for each secret size from 1 to 64. RFC 2104 defines HMAC, and RFC 4231 provides the check vectors the repository checks.

SHA-512/256 and SHA-3 are usually not susceptible to size extension, however that’s no purpose to hand-build a MAC from them. Use HMAC, and evaluate tags in fixed time: CRYPTO_memcmp in OpenSSL, MessageDigest.isEqual in Java, hmac.compare_digest in Python and CryptographicOperations.FixedTimeEquals in C#.

Collisions: The place MD5 and SHA-1 Really Break

A collision is 2 completely different inputs with the identical digest. Size extension abuses the design; collisions assault the hash itself, and they’re what “damaged” means for MD5 and SHA-1.

collisions.c hashes a widely known pair of 128-byte inputs from the 2004 assault by Xiaoyun Wang and colleagues, and a pair of 640-byte SHA-1 collision information from the “SHA-1 is a Shambles” analysis (Leurent and Peyrin, USENIX Safety 2020):

MD5 collision: two 128-byte inputs that differ in 6 bytes
  differing offsets: 19 45 59 83 109 123
  MD5      79054025255fb1a26e4bc422aef54eb4  (enter 1)
  MD5      79054025255fb1a26e4bc422aef54eb4  (enter 2)
  SHA-256  8d12236e5c4ed9f4e790db4d868fd5c399df267e18ff65c1107c328228cffc98  (enter 1)
  SHA-256  b9fef2a8fc93b05e7701e97196fda6c4fbeea25ff8e64fdfee7015eca8fa617d  (enter 2)

SHA-1 collision: two 640-byte inputs that differ in 331 bytes
  SHA-1    8ac60ba76f1999a1ab70223f225aefdc78d4ddc0  (enter 1)
  SHA-1    8ac60ba76f1999a1ab70223f225aefdc78d4ddc0  (enter 2)
  SHA-256  3ead211681cec93d265c8ac123dd062e105408cebf82fa6e2b126f4f40bcb88c  (enter 1)
  SHA-256  208feafe1c6a95c73f662514ac48761f25e1f3b74922521a98d9ce287f4a2197  (enter 2)

Every pair shares one digest (MD5 for the primary, SHA-1 for the second) and has two completely different SHA-256 digests. SHA-256 has no recognized collision, and nothing on this output modifications that. The MD5 inputs differ in six bytes, at offsets 19, 45, 59, 83, 109 and 123.

The SHA-1 story has three dates. Google and CWI Amsterdam revealed the primary collision in February 2017, the “SHAttered” assault. The 2020 chosen-prefix assault put the price of a collision with attacker-chosen prefixes at roughly US$45,000. NIST has set 31 December 2030 because the date to cease utilizing SHA-1 in its remaining protocols. For MD5, RFC 6151 updates RFC 1321 with revised safety issues after the collision assaults.

What does that imply to your code?

  • Don’t use MD5 or SHA-1 for signatures, certificates, or to resolve whether or not two information are the identical when somebody hostile could have equipped one in all them.
  • It’s possible you’ll nonetheless use them to detect unintentional corruption, or to match an present format that requires them, so long as no attacker advantages from forging a match.

Don’t Hand-Roll This: Use OpenSSL or Your Platform’s Library

The implementations on this article are for studying and for testing. For actual knowledge, name a maintained library. In C++ that often means OpenSSL’s EVP interface, wrapped in RAII so the context is all the time freed. This perform is the core of openssl_digest.cpp:

// Streaming digest by way of EVP. Throws if OpenSSL studies an error.
std::string openssl_hex(const EVP_MD* kind, std::string_view knowledge)
{
    Ctx ctx(EVP_MD_CTX_new());
    if (!ctx || EVP_DigestInit_ex(ctx.get(), kind, nullptr) != 1 ||
        EVP_DigestUpdate(ctx.get(), knowledge.knowledge(), knowledge.dimension()) != 1)
        throw std::runtime_error("EVP digest failed");
    std::array<unsigned char, EVP_MAX_MD_SIZE> out{};
    unsigned int n = 0;
    if (EVP_DigestFinal_ex(ctx.get(), out.knowledge(), &n) != 1)
        throw std::runtime_error("EVP digest failed");
    static constexpr char hex[] = "0123456789abcdef";
    std::string s;
    for (unsigned int i = 0; i < n; ++i) {
        s += hex[out[i] >> 4];
        s += hex[out[i] & 15];
    }
    return s;
}

This system hashes abc and an HMAC check case by way of OpenSSL and thru the C++ code from this text, then compares them:

MD5          900150983cd24fb0d6963f7d28e17f72  matches digest.hpp
SHA-1        a9993e364706816aba3e25717850c26c9cd0d89d  matches digest.hpp
SHA-256      ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad  matches digest.hpp
HMAC-SHA-256 5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843  matches digest.hpp

The libraries are sooner too, and the hole is giant.

How This Was Measured

Setting Worth
Date 21 November 2026
{Hardware} 2-vCPU Intel Xeon @ 2.10 GHz cloud container (SHA and AVX2 instruction flags current)
OS and toolchain Ubuntu 24.04, GCC 13.3, CMake Launch construct, OpenSSL 3.0.13
Workload 64 MiB hashed by way of 64 KiB replace calls, one thread
Statistic Median of 5 runs, reported as MB/s of enter
Included within the timing Context initialisation, all updates, finalisation
Checked first Either side produce the identical digest of the identical buffer

Not captured: percentiles, CPU pinning and frequency-scaling management. It is a shared cloud container, so absolutely the numbers will differ in your machine; deal with the ratios because the discovering. The run was repeated thrice and the ranges beneath cowl all three.

Hash This text’s C code OpenSSL 3.0.13 OpenSSL sooner by
MD5 396–422 MB/s 627–638 MB/s 1.5–1.6×
SHA-1 169–185 MB/s 1,303–1,684 MB/s 7.7–9.6×
SHA-256 195–273 MB/s 1,412–1,519 MB/s 5.6–7.2×

MD5 reveals the smallest hole and SHA-1 and SHA-256 present the most important. The CPU studies the SHA instruction-set extension, which is the probably purpose, however this benchmark didn’t affirm that OpenSSL used it, so deal with that as a speculation. Regardless of the trigger, an easy C implementation leaves an element of 5 to 10 on the desk for the hashes that matter.

Hashing can also be the incorrect software for passwords. A quick hash is strictly what an attacker needs. OWASP’s Password Storage Cheat Sheet recommends Argon2id, with a minimal of 19 MiB of reminiscence, two iterations and one diploma of parallelism. Block ciphers such because the Serpent cipher clear up a unique drawback once more, and the identical rule applies: be taught from the implementation, ship the library.

Which Hash Ought to You Use?

Want Use Why
Fingerprint a file or detect tampering SHA-256 No recognized collisions; accessible all over the place
Authenticate a message with a shared secret HMAC-SHA-256 Resistant to size extension; evaluate in fixed time
Retailer a password Argon2id (scrypt or bcrypt whether it is unavailable) Intentionally gradual and memory-hard
Match a legacy format that requires MD5 or SHA-1 The legacy hash, remoted and documented Interoperability, not safety
Catch unintentional corruption, no attacker concerned CRC32, MD5 or SHA-256 Any works; choose the one your instruments already use

Key Takeaways

  • MD5, SHA-1 and SHA-256 share one design. Pad to 64-byte blocks, compress every block, output the ultimate state. One shared padding perform serves all three.
  • MD5 and SHA-1 have sensible collisions, proven right here with actual inputs. SHA-256 has none recognized.
  • SHA-256(secret + message) is just not a MAC. Size extension forges a legitimate tag with out the key; HMAC-SHA-256 stops it.
  • Printed vectors catch almost every part. Sixteen RFC and FIPS solutions caught 21 of 27 deliberate bugs; HMAC and resume exams caught the remainder.
  • Three quiet bugs produce incorrect digests: a signed char, strlen() on binary knowledge and a trailing newline.
  • Use a library for actual work, which was 1.5 to 9.6 occasions sooner than the plain C code right here. Use Argon2id for passwords.

Continuously Requested Questions

Conclusion

Hash capabilities appear like magic till you write one. The padding rule, the block loop and the ultimate state are about fifty traces of shared code, and as soon as you possibly can see the state you possibly can see why size extension works and why HMAC wraps the hash twice. The collision pair makes the identical level from the opposite facet: “damaged” means somebody constructed two inputs, and you’ll print them.

The following step is to run the code. Clone the repository to your language, change one fixed in digest.c and watch the exams fail, then discover the opposite laptop science and algorithm articles on MYCPLUS. Revisit this web page when NIST’s SHA-1 deadline approaches in 2030 or if a brand new assault on SHA-256 is revealed.

Supply Code and Checks

C examples
C++ examples
Java examples
Python examples
C# examples

All packages on this web page are within the cryptography/hash-functions listing of the MYCPLUS instance repositories: C, C++, Java, Python and C#.

Construct and check:

cd cryptography/hash-functions
sh run_all.sh

On Home windows, run .run_all.ps1 from PowerShell in the identical listing.

What the construct checks. Each repository compiles with warnings as errors and runs its vector exams. The demos, the collision demo, the length-extension assault and the three pitfalls should print precisely the information in exams/anticipated/. The C and C++ repositories additionally run the mutation script, and the C repository compiles below MSVC /W4 /WX and compares 20,000 digests and MACs with OpenSSL on Linux. CI doesn’t run the benchmark, and it doesn’t check for timing facet channels.

What I Verified

Verify Ubuntu 24.04 Home windows 11
C: construct and exams, C11 and C17 GCC 13.3 and Clang 18, zero warnings, all vectors move MSVC /W4 /WX, C11 and C17, all exams move
C: AddressSanitizer and UndefinedBehaviorSanitizer Clear Not relevant
C: Valgrind (vector exams and length-extension demo) Clear Not relevant
C: mutation exams 27 of 27 caught Not relevant
C: comparability with OpenSSL (20,000 digests and MACs) All match Not relevant
C++: construct and exams, C++17 and C++20 g++ 13.3 and Clang 18, all vectors move, 19 of 19 mutants caught MSVC, C++17 and C++20, all exams move
Java: construct and exams OpenJDK 21 with --release 17, -Xlint:all -Werror, all move Compiled with --release 17, all vectors move
Python: exams, ruff, mypy Python 3.13, all move All exams move
C#: construct and exams .NET 8 SDK on a net8.0 copy of the challenge, all move All exams move
Demo, collision, length-extension and pitfall output Matches exams/anticipated/ Matches exams/anticipated/ within the C and C++ check runs
Benchmark Run on the cloud container described above Not run

Not coated: 32-bit builds, big-endian machines, macOS, Java 17 and 25, .NET 10 (the C# challenge targets it, however the native run used the .NET 8 SDK), the GitHub Actions workflows themselves and side-channel timing. The SHA-3 and SHA-512/256 statements come from revealed references and weren’t run right here.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments