hash
hash(string $algo, string $data, bool $binary = false, array $options = []): string
Generic hashing entry: computes a digest by algorithm name (e.g. sha256) and returns lowercase hex.
C C23
#include <stdio.h>
#include <string.h>
#include "sha256.h" // vendored implementation (Brad Conte crypto-algorithms, public domain)
int main(void) {
SHA256_CTX ctx;
BYTE digest[SHA256_BLOCK_SIZE]; // 32 bytes
sha256_init(&ctx);
sha256_update(&ctx, (const BYTE *)"hello php", 9);
sha256_final(&ctx, digest);
for (int i = 0; i < SHA256_BLOCK_SIZE; i++) {
printf("%02x", digest[i]);
}
printf("\n");
return 0;
}
b8f4a8fb1ece1774a9b9882ac17127d7c68ecec82fe0a7c036dd6e03b638b888
C++ C++26
#include <cstdio>
#include <cstring>
#include "sha256.h" // the vendored C implementation compiles fine as C++
int main() {
SHA256_CTX ctx;
BYTE digest[SHA256_BLOCK_SIZE]; // 32 bytes
sha256_init(&ctx);
sha256_update(&ctx, (const BYTE *)"hello php", 9);
sha256_final(&ctx, digest);
for (int i = 0; i < SHA256_BLOCK_SIZE; i++) {
printf("%02x", digest[i]);
}
printf("\n");
return 0;
}
b8f4a8fb1ece1774a9b9882ac17127d7c68ecec82fe0a7c036dd6e03b638b888
C# v11.0.100
using System.Security.Cryptography;
using System.Text;
byte[] digest = SHA256.HashData(Encoding.UTF8.GetBytes("hello php")); // static API since .NET 5
Console.WriteLine(Convert.ToHexString(digest).ToLowerInvariant());
b8f4a8fb1ece1774a9b9882ac17127d7c68ecec82fe0a7c036dd6e03b638b888
Go v1.27.4
package main
import (
"crypto/sha256"
"fmt"
)
func main() {
sum := sha256.Sum256([]byte("hello php")) // dedicated package; crypto.SHA256.New() is the generic form
fmt.Printf("%x\n", sum)
}
b8f4a8fb1ece1774a9b9882ac17127d7c68ecec82fe0a7c036dd6e03b638b888
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("SHA-256"); // generic factory by algorithm name
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);
}
}
b8f4a8fb1ece1774a9b9882ac17127d7c68ecec82fe0a7c036dd6e03b638b888
JavaScript v26.13.1
const { createHash } = require("node:crypto");
// same generic entry as PHP hash(): pick the algorithm by name
const hex = createHash("sha256").update("hello php").digest("hex");
console.log(hex);
b8f4a8fb1ece1774a9b9882ac17127d7c68ecec82fe0a7c036dd6e03b638b888
PHP v8.6.0
echo hash("sha256", "hello php"); // generic entry: md5('x') == hash('md5', 'x')
b8f4a8fb1ece1774a9b9882ac17127d7c68ecec82fe0a7c036dd6e03b638b888
Python v3.15.2
import hashlib
print(hashlib.new("sha256", b"hello php").hexdigest()) # generic factory, mirrors hash($algo, ...)
b8f4a8fb1ece1774a9b9882ac17127d7c68ecec82fe0a7c036dd6e03b638b888
Rust v1.99.1
use sha2::{Digest, Sha256}; // sha2 crate (RustCrypto)
fn main() {
let mut hasher = Sha256::new();
hasher.update(b"hello php");
let digest = hasher.finalize(); // 32 bytes
let hex: String = digest.iter().map(|b| format!("{:02x}", b)).collect();
println!("{}", hex);
}
b8f4a8fb1ece1774a9b9882ac17127d7c68ecec82fe0a7c036dd6e03b638b888
TypeScript v26.13.1
import { createHash } from "node:crypto";
const hex: string = createHash("sha256").update("hello php").digest("hex");
console.log(hex);
b8f4a8fb1ece1774a9b9882ac17127d7c68ecec82fe0a7c036dd6e03b638b888
Language differences
C: no hash API in the standard library — reuses the public-domain implementation vendored into this repo (the validator appends sha256.c when it sees #include "sha256.h").
C++: no hash API in the standard library — reuses the vendored implementation (the C source is compiled with -x c++ on the C++ side).
C#: the exception — one class per algorithm (SHA256.HashData); no need for runtime string dispatch, and more compile-time safety.
Go: the exception — one package per algorithm (crypto/sha256; the generic form is crypto.SHA256.New()); no need for runtime string dispatch, and more compile-time safety.
Java: MessageDigest.getInstance("SHA-256") follows PHP's idea (name dispatch).
JavaScript: Node's createHash("sha256") follows PHP's idea (name dispatch); in browsers use crypto.subtle.digest("SHA-256", ...) (async, SHA-family only) — Node's synchronous API is shown in the example.
PHP: hash($algo, $data) is a name-dispatched generic entry — md5($s) is shorthand for hash('md5', $s), and hash('sha256', ...) / hash('sha512', ...) work the same way; SHA-256 belongs to the SHA-2 family (FIPS 180-4) and is currently the default choice for data integrity, HMAC, and certificate signing, with no practical collision attacks known; it is still a fast hash, though, so it must never store passwords directly (GPUs try billions per second) — use password_hash (bcrypt/argon2) or PBKDF2 for that.
Python: hashlib.new("sha256", ...) follows PHP's idea (name dispatch).
Rust: uses the sha2 crate.