Skip to content

Checksum Verifier — PHP source

Drag and drop a file to compute its MD5, SHA-1, SHA-256, and SHA-512 checksums. Paste an expected hash to verify integrity - detect tampered or corrupted downloads instantly. Runs entirely in your browser.

This is the PHP implementation — the same logic the interactive tool runs, in a shareable, citable form.

<?php
/**
 * checksum-verifier — MD5 / SHA-1 / SHA-256 / SHA-512 checksums + verification.
 *
 * Language: PHP 7.4+ (standard library only).
 * CosmoDev polyglot showcase port of the `checksum-verifier` tool, ported from
 * src/lib/checksum-verifier.ts (the canonical TypeScript implementation).
 *
 * The TS reference hand-rolls MD5 (RFC 1321) because the Web Crypto API does
 * not ship it; PHP's standard library has it (md5(), hash_init()), so this
 * port uses the platform digests for all four algorithms and ports the
 * verification helpers as-is. MD5 is only for integrity checks - not security.
 *
 * display source — part of CosmoDev's polyglot tool pages.
 */

declare(strict_types=1);

/** Detect the algorithm from the hex length: 32=MD5, 40=SHA-1, 64=SHA-256, 128=SHA-512. */
const LENGTH_TO_ALGO = [32 => 'md5', 40 => 'sha1', 64 => 'sha256', 128 => 'sha512'];

/**
 * Create an incremental MD5 hasher (RFC 1321). Feed chunks to
 * hash_update() — no length limit — then finish with hash_final()
 * (pad, append the 64-bit length, lowercase hex via hash_final(..., true)).
 */
function create_md5(): HashContext
{
    return hash_init('md5');
}

/** One-shot MD5 of a binary string, as lowercase hex (integrity checks only). */
function cv_md5(string $data): string
{
    return md5($data);
}

/**
 * Digest a buffer with SHA-1 / SHA-256 / SHA-512 and return lowercase hex.
 *
 * @throws InvalidArgumentException for an unsupported algorithm name.
 */
function sha_hex(string $algorithm, string $data): string
{
    $algo = strtolower(str_replace('-', '', $algorithm));
    if (!in_array($algo, ['sha1', 'sha256', 'sha512'], true)) {
        throw new InvalidArgumentException("Unsupported algorithm: {$algorithm}");
    }
    return hash($algo, $data);
}

/**
 * Compute all four checksums of a binary string, as lowercase hex:
 * ['md5' => ..., 'sha1' => ..., 'sha256' => ..., 'sha512' => ...].
 */
function compute_checksums(string $data): array
{
    return [
        'md5' => cv_md5($data),
        'sha1' => sha_hex('SHA-1', $data),
        'sha256' => sha_hex('SHA-256', $data),
        'sha512' => sha_hex('SHA-512', $data),
    ];
}

/** Normalize a pasted hash: drop whitespace and `:` grouping, lowercase. */
function normalize_hash(string $hash): string
{
    return strtolower(preg_replace('/[\s:]/', '', trim($hash)));
}

/**
 * Detect the algorithm from the hex length; null when the string is not a
 * recognizable hex hash of a supported length.
 */
function detect_hash_algorithm(string $hash): ?string
{
    $h = normalize_hash($hash);
    if (preg_match('/^[0-9a-f]+$/', $h) !== 1) {
        return null;
    }
    return LENGTH_TO_ALGO[strlen($h)] ?? null;
}

/**
 * Compare an expected hash against a computed result. Returns null when the
 * expected string is not a recognizable hex hash of a supported length, else
 * ['algorithm' => ..., 'match' => bool].
 */
function verify_checksum(string $expected, array $result): ?array
{
    $algorithm = detect_hash_algorithm($expected);
    if ($algorithm === null) {
        return null;
    }
    return ['algorithm' => $algorithm, 'match' => normalize_hash($expected) === $result[$algorithm]];
}

Also available in 13 other languages

Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →