sha1

sha1(string $string, bool $binary = false): string

Computes the SHA-1 digest of a string as 40 lowercase hex characters (broken by practical collision attacks; not for security).

C C23

C23
#include <stdint.h>
#include <stdio.h>
#include <string.h>

// Hand-written minimal SHA-1: no hash API in the standard library
static uint32_t rotl(uint32_t x, int c) { return (x << c) | (x >> (32 - c)); }

static void sha1_block(uint32_t st[5], const uint8_t p[64]) {
    uint32_t w[80];
    for (int i = 0; i < 16; i++) {
        w[i] = (uint32_t)p[4 * i] << 24 | (uint32_t)p[4 * i + 1] << 16 | (uint32_t)p[4 * i + 2] << 8 | p[4 * i + 3];
    }
    for (int i = 16; i < 80; i++) {
        w[i] = rotl(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1); // message schedule
    }
    uint32_t a = st[0], b = st[1], c = st[2], d = st[3], e = st[4];
    for (int i = 0; i < 80; i++) {
        uint32_t f, k;
        if (i < 20)      { f = (b & c) | (~b & d);          k = 0x5a827999; }
        else if (i < 40) { f = b ^ c ^ d;                   k = 0x6ed9eba1; }
        else if (i < 60) { f = (b & c) | (b & d) | (c & d); k = 0x8f1bbcdc; }
        else             { f = b ^ c ^ d;                   k = 0xca62c1d6; }
        uint32_t tmp = rotl(a, 5) + f + e + k + w[i];
        e = d;
        d = c;
        c = rotl(b, 30);
        b = a;
        a = tmp;
    }
    st[0] += a; st[1] += b; st[2] += c; st[3] += d; st[4] += e;
}

int main(void) {
    const char *msg = "hello php";
    size_t len = strlen(msg);
    uint8_t buf[128] = {0};
    memcpy(buf, msg, len);
    buf[len] = 0x80; // padding: append 0x80 then zeros
    size_t total = (len + 9 <= 64) ? 64 : 128;
    uint64_t bits = (uint64_t)len * 8;
    for (int i = 0; i < 8; i++) buf[total - 1 - i] = (uint8_t)(bits >> (8 * i)); // big-endian bit length
    uint32_t st[5] = {0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0};
    for (size_t off = 0; off < total; off += 64) sha1_block(st, buf + off);
    for (int i = 0; i < 5; i++) {
        for (int j = 3; j >= 0; j--) {
            printf("%02x", (st[i] >> (8 * j)) & 0xff); // big-endian hex output
        }
    }
    printf("\n");
    return 0;
}
Output
ed1ba164e6db812e51d58fa3c04f00ea54617ce6

C++ C++26

C++26
#include <cstdint>
#include <cstdio>
#include <cstring>

// Hand-written minimal SHA-1: 80-step single loop, big-endian output
static uint32_t rotl(uint32_t x, int c) { return (x << c) | (x >> (32 - c)); }

static void sha1_block(uint32_t st[5], const uint8_t p[64]) {
    uint32_t w[80];
    for (int i = 0; i < 16; i++) {
        w[i] = (uint32_t)p[4 * i] << 24 | (uint32_t)p[4 * i + 1] << 16 | (uint32_t)p[4 * i + 2] << 8 | p[4 * i + 3];
    }
    for (int i = 16; i < 80; i++) {
        w[i] = rotl(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1); // message schedule
    }
    uint32_t a = st[0], b = st[1], c = st[2], d = st[3], e = st[4];
    for (int i = 0; i < 80; i++) {
        uint32_t f, k;
        if (i < 20)      { f = (b & c) | (~b & d);          k = 0x5a827999; }
        else if (i < 40) { f = b ^ c ^ d;                   k = 0x6ed9eba1; }
        else if (i < 60) { f = (b & c) | (b & d) | (c & d); k = 0x8f1bbcdc; }
        else             { f = b ^ c ^ d;                   k = 0xca62c1d6; }
        uint32_t tmp = rotl(a, 5) + f + e + k + w[i];
        e = d;
        d = c;
        c = rotl(b, 30);
        b = a;
        a = tmp;
    }
    st[0] += a; st[1] += b; st[2] += c; st[3] += d; st[4] += e;
}

int main() {
    const char *msg = "hello php";
    size_t len = strlen(msg);
    uint8_t buf[128] = {0};
    memcpy(buf, msg, len);
    buf[len] = 0x80; // padding: append 0x80 then zeros
    size_t total = (len + 9 <= 64) ? 64 : 128;
    uint64_t bits = (uint64_t)len * 8;
    for (int i = 0; i < 8; i++) buf[total - 1 - i] = (uint8_t)(bits >> (8 * i)); // big-endian bit length
    uint32_t st[5] = {0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0};
    for (size_t off = 0; off < total; off += 64) sha1_block(st, buf + off);
    for (int i = 0; i < 5; i++) {
        for (int j = 3; j >= 0; j--) {
            printf("%02x", (st[i] >> (8 * j)) & 0xff);
        }
    }
    printf("\n");
    return 0;
}
Output
ed1ba164e6db812e51d58fa3c04f00ea54617ce6

C# v11.0.100

v11.0.100
using System.Security.Cryptography;
using System.Text;

byte[] digest = SHA1.HashData(Encoding.UTF8.GetBytes("hello php")); // static API since .NET 5
Console.WriteLine(Convert.ToHexString(digest).ToLowerInvariant());
Output
ed1ba164e6db812e51d58fa3c04f00ea54617ce6

Go v1.27.4

v1.27.4
package main

import (
    "crypto/sha1"
    "fmt"
)

func main() {
    sum := sha1.Sum([]byte("hello php")) // returns a [20]byte array
    fmt.Printf("%x\n", sum)
}
Output
ed1ba164e6db812e51d58fa3c04f00ea54617ce6

Java v27.0.1

v27.0.1
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;

public class Main {
    public static void main(String[] args) throws Exception {
        MessageDigest md = MessageDigest.getInstance("SHA-1"); // 40-hex = 20 bytes
        StringBuilder hex = new StringBuilder();
        for (byte b : md.digest("hello php".getBytes(StandardCharsets.UTF_8))) {
            hex.append(String.format("%02x", b));
        }
        System.out.println(hex);
    }
}
Output
ed1ba164e6db812e51d58fa3c04f00ea54617ce6

JavaScript v26.13.1

v26.13.1
const { createHash } = require("node:crypto");

console.log(createHash("sha1").update("hello php").digest("hex")); // browsers: md5/sha1 are NOT in Web Crypto
Output
ed1ba164e6db812e51d58fa3c04f00ea54617ce6

PHP v8.6.0

v8.6.0
echo sha1("hello php"); // lowercase 40-hex string; pass true as 2nd arg for raw 20 bytes
Output
ed1ba164e6db812e51d58fa3c04f00ea54617ce6

Python v3.15.2

v3.15.2
import hashlib

print(hashlib.sha1(b"hello php").hexdigest())
Output
ed1ba164e6db812e51d58fa3c04f00ea54617ce6

Rust v1.99.1

v1.99.1
use sha1::{Digest, Sha1}; // sha1 crate (RustCrypto)

fn main() {
    let mut hasher = Sha1::new();
    hasher.update(b"hello php");
    let digest = hasher.finalize(); // 20 bytes
    let hex: String = digest.iter().map(|b| format!("{:02x}", b)).collect();
    println!("{}", hex);
}
Output
ed1ba164e6db812e51d58fa3c04f00ea54617ce6

TypeScript v26.13.1

v26.13.1
import { createHash } from "node:crypto";

const hex: string = createHash("sha1").update("hello php").digest("hex");
console.log(hex);
Output
ed1ba164e6db812e51d58fa3c04f00ea54617ce6

Language differences

C: no hash API in the standard library — this entry hand-writes the core algorithm (SHA-1 is tidier than MD5: a single 80-step loop, big-endian; the vendored crypto library in this repo only covers md5 and sha256, so SHA-1 is hand-written to show its structure).

C++: same as C — hand-written core algorithm.

C#: standard-library SHA1.HashData.

Go: available directly in the standard library.

Java: standard-library MessageDigest.getInstance("SHA-1").

JavaScript: available directly in Node; browser note: Web Crypto's crypto.subtle.digest supports async "SHA-1" (md5 is absent there); on the Node side both are available synchronously.

PHP: SHA-1 outputs a 160-bit digest (20 bytes -> 40 hex chars), and all languages print the same value — available directly in the standard library; security warning: SHA-1 is broken by practical attacks — the 2017 SHAttering collision for PDFs cost roughly 6500 CPU-years, and Chrome and other browsers have since rejected SHA-1 certificates; signatures, certificates, and password storage are all off-limits; Git still uses SHA-1 for object IDs but is migrating to SHA-256 (Git's content framing mitigates the risk); legacy download checksums are tolerable, but new designs should pick SHA-256 directly (see the hash entry); for passwords see password_hash.

Python: available directly in the standard library.

Rust: uses the sha1 crate (RustCrypto).

TypeScript: same as JS — browsers via crypto.subtle, Node synchronously.