md5
md5(string $string, bool $binary = false): string
Computes the MD5 digest of a string as 32 lowercase hex characters (cryptographically broken; checksum uses only).
C C23
#include <stdio.h>
#include <string.h>
#include "md5.h" // vendored implementation (Brad Conte crypto-algorithms, public domain)
int main(void) {
const char *s = "hello php";
MD5_CTX ctx;
BYTE digest[MD5_BLOCK_SIZE]; // MD5 outputs a 16-byte digest
md5_init(&ctx);
md5_update(&ctx, (const BYTE *)s, strlen(s));
md5_final(&ctx, digest);
for (int i = 0; i < MD5_BLOCK_SIZE; i++) {
printf("%02x", digest[i]); // two lowercase hex digits per byte
}
printf("\n");
return 0;
}
a689d80f8d9e21360017b2ce3b356fba
C++ C++26
#include <cstdint>
#include <cstdio>
#include <cstring>
// Hand-written minimal MD5: the standard library has no hash API
static const uint32_t K[64] = {
0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391,
};
static const int S[64] = {
7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21,
};
static uint32_t rotl(uint32_t x, int c) { return (x << c) | (x >> (32 - c)); }
// Compress one 64-byte block into the state
static void md5_block(uint32_t st[4], const uint8_t p[64]) {
uint32_t m[16];
memcpy(m, p, 64); // little-endian word load
uint32_t a = st[0], b = st[1], c = st[2], d = st[3];
for (int i = 0; i < 64; i++) {
uint32_t f;
int g;
if (i < 16) { f = (b & c) | (~b & d); g = i; }
else if (i < 32) { f = (d & b) | (~d & c); g = (5 * i + 1) % 16; }
else if (i < 48) { f = b ^ c ^ d; g = (3 * i + 5) % 16; }
else { f = c ^ (b | ~d); g = (7 * i) % 16; }
uint32_t tmp = d;
d = c;
c = b;
b = b + rotl(a + f + K[i] + m[g], S[i]);
a = tmp;
}
st[0] += a; st[1] += b; st[2] += c; st[3] += d;
}
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;
memcpy(buf + total - 8, &bits, 8); // little-endian bit length
uint32_t st[4] = {0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476};
for (size_t off = 0; off < total; off += 64) md5_block(st, buf + off);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
printf("%02x", (st[i] >> (8 * j)) & 0xff); // little-endian hex output
}
}
printf("\n");
return 0;
}
a689d80f8d9e21360017b2ce3b356fba
C# v11.0.100
using System.Security.Cryptography;
using System.Text;
byte[] digest = MD5.HashData(Encoding.UTF8.GetBytes("hello php")); // static API since .NET 5
Console.WriteLine(Convert.ToHexString(digest).ToLowerInvariant()); // ToHexString gives uppercase: lower it
a689d80f8d9e21360017b2ce3b356fba
Go v1.27.4
package main
import (
"crypto/md5"
"fmt"
)
func main() {
sum := md5.Sum([]byte("hello php")) // returns a [16]byte array, not a slice
fmt.Printf("%x\n", sum) // %x hex-encodes byte sequences
}
a689d80f8d9e21360017b2ce3b356fba
Java 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("MD5"); // JCA generic factory, checked exception
StringBuilder hex = new StringBuilder();
for (byte b : md.digest("hello php".getBytes(StandardCharsets.UTF_8))) {
hex.append(String.format("%02x", b)); // two lowercase hex digits per byte
}
System.out.println(hex);
}
}
a689d80f8d9e21360017b2ce3b356fba
JavaScript v26.13.1
const { createHash } = require("node:crypto");
const hex = createHash("md5").update("hello php").digest("hex"); // Node built-in crypto module
console.log(hex);
a689d80f8d9e21360017b2ce3b356fba
PHP v8.6.0
echo md5("hello php"); // lowercase 32-hex string; pass true as 2nd arg for raw 16 bytes
a689d80f8d9e21360017b2ce3b356fba
Python v3.15.2
import hashlib
print(hashlib.md5(b"hello php").hexdigest()) # takes bytes, returns lowercase hex
a689d80f8d9e21360017b2ce3b356fba
Rust v1.99.1
use md5::{Digest, Md5}; // md-5 crate (RustCrypto); import root is md5
fn main() {
let mut hasher = Md5::new();
hasher.update(b"hello php");
let digest = hasher.finalize(); // 16-byte generic array
let hex: String = digest.iter().map(|b| format!("{:02x}", b)).collect();
println!("{}", hex);
}
a689d80f8d9e21360017b2ce3b356fba
TypeScript v26.13.1
import { createHash } from "node:crypto";
const hex: string = createHash("md5").update("hello php").digest("hex");
console.log(hex);
a689d80f8d9e21360017b2ce3b356fba
Language differences
C: no hash API in the standard library — uses the public-domain implementation vendored into this repo (Brad Conte crypto-algorithms; the validator links md5.c automatically when it sees #include "md5.h").
C++: no hash API in the standard library — this entry hand-writes the core algorithm to show its internals.
C#: MD5.HashData, a standard-library one-liner; Convert.ToHexString is uppercase and needs lowering.
Go: crypto/md5, a standard-library one-liner; md5.Sum returns a [16]byte array (streaming via md5.New()).
Java: MessageDigest, a standard-library one-liner; the checked exception must be handled and hex assembled manually.
JavaScript: Node crypto.createHash("md5"), a standard-library one-liner.
PHP: MD5 outputs a 128-bit digest (16 bytes -> 32 hex chars), and all ten language examples print the exact same value (hashing is deterministic) — md5() is a standard-library one-liner; security warning: MD5 is cryptographically broken (practical collisions since 2004; the 2012 Flame malware forged an MD5 signature), and must never be used for password storage, digital signatures, or any adversarial purpose; it is only acceptable for non-adversarial uses such as cache keys, deduplication, and checksums of files whose integrity nobody is actively attacking; for passwords see the password_hash entry (bcrypt/argon2).
Python: hashlib.md5, a standard-library one-liner; API detail: takes bytes and returns str.
Rust: uses the ecosystem-standard md-5 crate (the validator switches to cargo when it sees md5::).