Skip to content

Hash Generator — PHP source

Compute SHA-1, SHA-256, SHA-384 and SHA-512 hashes of any text at once, using the browser's native SubtleCrypto. Copy each digest. 100% client-side.

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

<?php
/**
 * hash — SHA-1 / SHA-256 / SHA-384 / SHA-512 digest of a UTF-8 string.
 *
 * Language: PHP
 * CosmoDev polyglot showcase port of the `hash` tool.
 * Ported from src/tools/HashGenerator.tsx — display source, part of CosmoDev's
 * polyglot tool pages.
 *
 * Unkeyed SHA digests via PHP's built-in hash(), which is backed by the
 * system's vetted hashing library (OpenSSL or the bundled hash extension)
 * and returns lowercase hex by default — matching the TypeScript reference's
 * output format exactly. No extensions or Composer packages are required.
 *
 * Behavior mirrors the TypeScript reference: UTF-8 input, lowercase hex
 * output, and rejection of unknown algorithm names. PHP strings are byte
 * strings, so a UTF-8 $text is forwarded verbatim to hash() — no explicit
 * encoding step is needed.
 */

declare(strict_types=1);

/*
 * Canonical algorithm names. The spellings match the TypeScript union so the
 * same string works across every port.
 */
const SHA1   = 'SHA-1';
const SHA256 = 'SHA-256';
const SHA384 = 'SHA-384';
const SHA512 = 'SHA-512';

/**
 * Map a canonical algorithm name to the lowercase form PHP's hash() expects
 * ('sha256', etc.). Returns null for unknown names so the caller can raise a
 * clear error rather than relying on hash()'s warning + false return.
 */
function hashAlgoFor(string $algorithm): ?string
{
    switch ($algorithm) {
        case SHA1:
            return 'sha1';
        case SHA256:
            return 'sha256';
        case SHA384:
            return 'sha384';
        case SHA512:
            return 'sha512';
        default:
            return null;
    }
}

/**
 * Compute the digest of $text under the named algorithm and return it as
 * lowercase hex.
 *
 * @param string $text      The payload to hash.
 * @param string $algorithm One of SHA1/SHA256/SHA384/SHA512 (default SHA-256).
 * @return string           Lowercase hex digest.
 * @throws InvalidArgumentException On an unknown algorithm.
 */
function digest(string $text, string $algorithm = SHA256): string
{
    if ($algorithm === '') {
        $algorithm = SHA256;
    }

    $phpAlgo = hashAlgoFor($algorithm);
    if ($phpAlgo === null) {
        throw new InvalidArgumentException("Unsupported hash algorithm: {$algorithm}");
    }

    // raw_output = false (the default) → lowercase hex string.
    return hash($phpAlgo, $text, false);
}

/**
 * Compute every supported digest of $text, returning an associative array
 * keyed by the canonical algorithm name — the PHP equivalent of the Record
 * the React island renders.
 *
 * @param string $text The payload to hash.
 * @return array<string,string> Map of algorithm name -> hex digest.
 */
function hash_all(string $text): array
{
    return [
        SHA1   => digest($text, SHA1),
        SHA256 => digest($text, SHA256),
        SHA384 => digest($text, SHA384),
        SHA512 => digest($text, SHA512),
    ];
}

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 →