SHA-256哈希生成器
从任何文本生成SHA-256哈希
输入文本
SHA-256哈希
SHA-256哈希将显示在这里…
什么是SHA-256哈希?
SHA-256(安全哈希算法256位)是SHA-2家族中的一种密码学哈希函数,由NIST于2001年随FIPS 180-2发布(后更新为FIPS 180-4)。无论输入是单个字符、数GB的文件,还是空字符串,SHA-256均生成固定的256位(32字节)摘要,通常以64个十六进制字符表示。SHA-256是目前生产系统中部署最广泛的哈希函数,支撑着TLS证书链、比特币工作量证明、子资源完整性(SRI)以及代码签名工作流。
SHA-256是单向函数:从输入计算哈希值很快(在现代硬件上每秒可处理数百MB),但逆向操作——找到能生成特定哈希的输入——在计算上不可行。这一特性称为原像抵抗,使SHA-256适用于密码哈希(结合盐值和密钥拉伸)、数字签名以及数据完整性验证。与MD5和SHA-1不同,迄今为止尚未发现针对完整SHA-256的碰撞或原像攻击。
SHA-2家族包含六个变体:SHA-224、SHA-256、SHA-384、SHA-512、SHA-512/224和SHA-512/256。SHA-256以32位字为单位、每块执行64轮运算,针对32位处理器优化。SHA-512使用64位字和80轮运算,在64位平台上速度可能更快。对于大多数256位摘要已满足需求的应用场景,SHA-256仍是NIST、IETF及CA/Browser Forum的默认推荐算法。
为什么使用在线SHA-256生成器?
生成SHA-256哈希通常需要终端命令或几行代码。这个基于浏览器的工具让你无需安装任何东西、切换上下文或编写脚本,即可直接计算SHA-256摘要。
SHA-256使用场景
SHA-2家族变体对比
SHA-256属于FIPS 180-4定义的SHA-2家族。每个变体在摘要长度、性能特征和安全余量之间各有取舍。下表对你可能遇到的所有SHA-2变体进行比较。
| 变体 | 摘要长度 | 十六进制长度 | 字节大小 | 适用场景 |
|---|---|---|---|---|
| SHA-256 | 256 bits | 64 hex chars | 32 bytes | TLS, blockchain, code signing, JWTs, SRI |
| SHA-224 | 224 bits | 56 hex chars | 28 bytes | Truncated SHA-256 — rare, specific compliance |
| SHA-384 | 384 bits | 96 hex chars | 48 bytes | Government / CNSS, higher collision margin |
| SHA-512 | 512 bits | 128 hex chars | 64 bytes | Digital signatures, HMAC with large keys |
| SHA-512/256 | 256 bits | 64 hex chars | 32 bytes | SHA-512 speed on 64-bit CPUs, 256-bit output |
SHA-256 与 SHA-1、MD5、SHA-3 对比
选择合适的哈希算法取决于你的安全需求和兼容性约束。SHA-256处于实用的最佳平衡点:安全、得到广泛支持(包括Web Crypto API),并且对大多数工作负载来说足够快。下表涵盖了选择哈希函数时最重要的几项属性。
| 属性 | SHA-256 | SHA-1 | MD5 | SHA-3-256 |
|---|---|---|---|---|
| Digest size | 256 bits (64 hex) | 160 bits (40 hex) | 128 bits (32 hex) | 256 bits (64 hex) |
| Security status | Secure | Broken (2017) | Broken (2004) | Secure |
| Collision resistance | 2^128 operations | Practical attack | Practical attack | 2^128 operations |
| Block size | 512 bits | 512 bits | 512 bits | 1600 bits (sponge) |
| Rounds | 64 | 80 | 64 | 24 |
| Standard | FIPS 180-4 | FIPS 180-4 | RFC 1321 | FIPS 202 |
| Web Crypto API | Yes | Yes | No | No |
| Primary use today | TLS, blockchain, SRI | Legacy git only | Non-security checksums | Backup standard |
SHA-256的内部工作原理
SHA-256通过Merkle–Damgård构造以512位(64字节)为块处理输入。算法初始化八个32位状态字(H0–H7),这些值来源于前八个质数的平方根的小数部分。每个块经过64轮混合运算,使用位运算(AND、XOR、NOT、右旋、右移)以及64个来源于前64个质数立方根的轮常数。
SHA-256: b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
(256 bits = 32 bytes = 64 hex characters)
| 步骤 | 描述 |
|---|---|
| Padding | Append a 1-bit, then zeros until the message length is 448 mod 512. Append the original message length as a 64-bit big-endian integer. |
| Block splitting | Divide the padded message into 512-bit (64-byte) blocks. |
| Message schedule | Expand each 16-word (32-bit) block into 64 words using sigma functions with right-rotate and right-shift operations. |
| Compression | Process 64 rounds per block using Ch, Maj, and two Sigma functions with 64 round constants derived from cube roots of the first 64 primes. |
| Output | Concatenate the eight 32-bit state words (H0-H7) into a 256-bit (32-byte) digest, rendered as 64 hexadecimal characters. |
雪崩效应确保输入中翻转单个比特会使约50%的输出比特发生变化。这一特性加上2^128的碰撞抵抗能力,是SHA-256在2026年仍作为安全敏感应用基线推荐算法的原因。
SHA-256代码示例
SHA-256在每种主流语言和运行时中均原生支持。Web Crypto API在浏览器中无需任何库即可提供该功能。以下示例展示了包括Unicode输入处理和文件哈希在内的实际使用模式。
// Works in all modern browsers and Node.js 18+
async function sha256(text) {
const data = new TextEncoder().encode(text)
const hashBuffer = await crypto.subtle.digest('SHA-256', data)
const hashArray = Array.from(new Uint8Array(hashBuffer))
return hashArray.map(b => b.toString(16).padStart(2, '0')).join('')
}
await sha256('hello world')
// → "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"
// Node.js (built-in crypto module)
const crypto = require('crypto')
crypto.createHash('sha256').update('hello world').digest('hex')
// → "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"import hashlib
# Basic SHA-256 hash
result = hashlib.sha256(b'hello world').hexdigest()
print(result) # → "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"
# Hash a string (encode to bytes first)
text = 'café ☕'
hashlib.sha256(text.encode('utf-8')).hexdigest()
# → "3eb53e00aa1bb4b1e8aab1ab38e56e6b8fb0b20e1cf7e1d19f36e4fad2537445"
# Hash a file in chunks (memory-efficient)
with open('release.tar.gz', 'rb') as f:
sha = hashlib.sha256()
for chunk in iter(lambda: f.read(8192), b''):
sha.update(chunk)
print(sha.hexdigest())package main
import (
"crypto/sha256"
"fmt"
)
func main() {
data := []byte("hello world")
hash := sha256.Sum256(data)
fmt.Printf("%x\n", hash)
// → b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
}# Using sha256sum (Linux) or shasum (macOS) echo -n "hello world" | sha256sum # → b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9 - # macOS echo -n "hello world" | shasum -a 256 # → b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9 - # Verify a file checksum echo "b94d27b... myfile.bin" | sha256sum -c # → myfile.bin: OK # Using openssl (cross-platform) echo -n "hello world" | openssl dgst -sha256 # → SHA2-256(stdin)= b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9