hash

hash(string $algo, string $data, bool $binary = false, array $options = []): string

通用哈希入口:按算法名(如 sha256)计算摘要,返回小写十六进制字符串。

C C23

C23
#include <stdio.h>
#include <string.h>
#include "sha256.h" // 随仓库入库的实现(Brad Conte crypto-algorithms,公有领域)

int main(void) {
    SHA256_CTX ctx;
    BYTE digest[SHA256_BLOCK_SIZE]; // 32 字节
    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

C++26
#include <cstdio>
#include <cstring>
#include "sha256.h" // vendor 的 C 实现可直接作为 C++ 编译

int main() {
    SHA256_CTX ctx;
    BYTE digest[SHA256_BLOCK_SIZE]; // 32 字节
    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

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

byte[] digest = SHA256.HashData(Encoding.UTF8.GetBytes("hello php")); // .NET 5 起的静态 API
Console.WriteLine(Convert.ToHexString(digest).ToLowerInvariant());
输出
b8f4a8fb1ece1774a9b9882ac17127d7c68ecec82fe0a7c036dd6e03b638b888

Go v1.27.4

v1.27.4
package main

import (
    "crypto/sha256"
    "fmt"
)

func main() {
    sum := sha256.Sum256([]byte("hello php")) // 专用包;crypto.SHA256.New() 是通用形式
    fmt.Printf("%x\n", sum)
}
输出
b8f4a8fb1ece1774a9b9882ac17127d7c68ecec82fe0a7c036dd6e03b638b888

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-256"); // 按算法名的通用工厂
        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

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

// 与 PHP hash() 同样的通用入口:按名字选算法
const hex = createHash("sha256").update("hello php").digest("hex");
console.log(hex);
输出
b8f4a8fb1ece1774a9b9882ac17127d7c68ecec82fe0a7c036dd6e03b638b888

PHP v8.6.0

v8.6.0
echo hash("sha256", "hello php"); // 通用入口:md5('x') 等价于 hash('md5', 'x')
输出
b8f4a8fb1ece1774a9b9882ac17127d7c68ecec82fe0a7c036dd6e03b638b888

Python v3.15.2

v3.15.2
import hashlib

print(hashlib.new("sha256", b"hello php").hexdigest())  # 通用工厂,对应 hash($algo, ...)
输出
b8f4a8fb1ece1774a9b9882ac17127d7c68ecec82fe0a7c036dd6e03b638b888

Rust v1.99.1

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 字节
    let hex: String = digest.iter().map(|b| format!("{:02x}", b)).collect();
    println!("{}", hex);
}
输出
b8f4a8fb1ece1774a9b9882ac17127d7c68ecec82fe0a7c036dd6e03b638b888

TypeScript v26.13.1

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

const hex: string = createHash("sha256").update("hello php").digest("hex");
console.log(hex);
输出
b8f4a8fb1ece1774a9b9882ac17127d7c68ecec82fe0a7c036dd6e03b638b888

语言差异说明

C:标准库无哈希 API,复用随仓库入库的公版实现(校验器检测 #include "sha256.h" 自动附加 sha256.c)。

C++:标准库无哈希 API,复用随仓库入库的公版实现(C++ 侧以 -x c++ 把 C 源当 C++ 编译)。

C#:例外——用每算法一个类(SHA256.HashData),无通用字符串分发的必要,编译期类型更安全。

Go:例外——用每算法一个包(crypto/sha256,通用形式是 crypto.SHA256.New()),无通用字符串分发的必要,编译期类型更安全。

Java:MessageDigest.getInstance("SHA-256") 与 PHP 同思路(按算法名分发)。

JavaScript:Node 的 createHash("sha256") 与 PHP 同思路(按算法名分发);浏览器端用 crypto.subtle.digest("SHA-256", ...)(异步、只支持 SHA 系),Node 同步 API 见示例。

PHP:hash($algo, $data) 是按算法名分发的通用入口:md5($s) 就是 hash('md5', $s) 的快捷方式,hash('sha256', ...)、hash('sha512', ...) 同理;SHA-256 属 SHA-2 家族(FIPS 180-4),目前是数据完整性校验、HMAC、证书签名的默认选择,尚无实际碰撞攻击;但它仍是快速哈希,不能直接存密码(GPU 每秒可算数十亿次),密码场景用 password_hash(bcrypt/argon2)或 PBKDF2(见该条目)。

Python:hashlib.new("sha256", ...) 与 PHP 同思路(按算法名分发)。

Rust:用 sha2 crate。