SHA-1哈希生成器
从任何文本生成SHA-1哈希
输入文本
SHA-1哈希
SHA-1哈希将显示在这里…
什么是SHA-1哈希?
SHA-1(安全哈希算法1)是一种密码哈希函数,无论输入大小如何,都能从任意输入生成固定160位(20字节)的摘要。SHA-1由NSA发布,1995年由NIST以FIPS PUB 180-1标准化,后在RFC 3174中记录,被设计为SHA-0和MD5更强的继承者。该算法将输入以512位为一块进行处理,经过80轮位运算,生成40个字符的十六进制指纹,在十余年间作为SSL/TLS、PGP、SSH和IPsec的核心被广泛使用。
与所有密码哈希函数一样,SHA-1是一种单向变换:给定输入,可以快速计算哈希值,但仅凭哈希值无法还原原始输入。输入中哪怕只改变一个比特,也会产生完全不同的160位摘要——这一特性称为雪崩效应。SHA-1将任意大小的输入空间映射到固定的160位输出,因此从数学上讲,碰撞(两个不同输入产生相同哈希)必然存在。哈希函数的安全性要求找到碰撞大约需要2^80次运算——即输出位长度的一半。
2017年,Google与CWI阿姆斯特丹发布了SHAttered攻击,通过生成两个摘要相同的不同PDF文件,首次演示了实际的SHA-1碰撞。该攻击约需2^63.1次SHA-1运算——远低于理论上的2^80界限。2020年,Gaetan Leurent和Thomas Peyrin进一步降低了攻击成本,选择前缀碰撞攻击仅需约2^63.4次运算。因此,所有主流浏览器、证书颁发机构和标准机构均已弃用SHA-1用于数字签名和TLS证书。然而,SHA-1在非安全用途中仍然活跃:Git对象ID(尽管Git正在迁移至SHA-256)、遗留HMAC构造,以及无需对抗性抗碰撞性的文件完整性校验。
为什么使用这个SHA-1生成器?
无需安装任何软件或编写代码,即可即时生成SHA-1哈希。粘贴文本,实时获取40个字符的十六进制摘要——适用于验证遗留校验和、调试Git内部机制或测试基于哈希的工作流。
SHA-1使用场景
SHA-1与其他哈希算法对比
SHA-1生成160位摘要——比MD5(128位)长,但远短于SHA-2系列算法。下表比较了各算法的摘要大小、标准和适用场景。
| 算法 | 摘要大小 | 十六进制长度 | 标准 | 最佳用途 |
|---|---|---|---|---|
| SHA-1 | 160 bits | 40 hex chars | 1995 / RFC 3174 | Deprecated — legacy git commits, old TLS |
| SHA-256 | 256 bits | 64 hex chars | 2001 / FIPS 180-4 | TLS certificates, blockchain, JWTs |
| SHA-384 | 384 bits | 96 hex chars | 2001 / FIPS 180-4 | Government systems, higher security margin |
| SHA-512 | 512 bits | 128 hex chars | 2001 / FIPS 180-4 | Digital signatures, HMAC with large keys |
| MD5 | 128 bits | 32 hex chars | 1992 / RFC 1321 | Checksums only — broken since 2004 |
| SHA-3 | 256 bits | 64 hex chars | 2015 / FIPS 202 | Post-quantum readiness, alternative to SHA-2 |
| BLAKE3 | 256 bits | 64 hex chars | 2020 | High-performance checksums, Merkle trees |
SHA-1的工作原理
SHA-1遵循Merkle-Damgård结构:对消息进行填充,分割成512位的块,每块经过80轮位运算处理,将输入与从该块派生的消息调度混合。五个32位状态字(H0至H4)保存运行中的哈希状态,这些状态字最终拼接生成160位摘要。
SHA-1: 2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
(160 bits = 20 bytes = 40 hex characters)
| 步骤 | 说明 |
|---|---|
| Padding | Append a 1-bit, then zeros, until message length is 448 mod 512. Append the original length as a 64-bit big-endian integer. |
| Block splitting | Divide the padded message into 512-bit (64-byte) blocks. |
| Expansion | Expand each 16-word block into 80 words using a left-rotate-by-1 XOR feedback schedule. |
| Compression | Process 80 rounds per block using four nonlinear functions (Ch, Parity, Maj, Parity) across rounds 0-19, 20-39, 40-59, and 60-79. |
| Output | Concatenate the five 32-bit state words (H0-H4) into a 160-bit (20-byte) digest, rendered as 40 hexadecimal characters. |
SHA-1与MD5的关键区别在于状态字数量(5个对4个)、每块的轮数(80轮对64轮),以及使用带左旋转反馈的消息调度。这些差异使SHA-1具有更大的输出(160位对128位),并最初提供了更高的安全裕度,但两种算法现在都被认为在抗碰撞性方面已被攻破。
代码示例
如何在常用语言和环境中生成SHA-1哈希。与MD5不同,SHA-1在浏览器Web Crypto API中可用,因此在浏览器和Node.js环境中均可无需外部库直接使用。
// SHA-1 is available in the Web Crypto API
async function sha1(text) {
const data = new TextEncoder().encode(text)
const hashBuffer = await crypto.subtle.digest('SHA-1', data)
const hashArray = Array.from(new Uint8Array(hashBuffer))
return hashArray.map(b => b.toString(16).padStart(2, '0')).join('')
}
await sha1('hello world')
// → "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed"
// Node.js (built-in crypto module)
const crypto = require('crypto')
crypto.createHash('sha1').update('hello world').digest('hex')
// → "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed"import hashlib
# Basic SHA-1 hash
result = hashlib.sha1(b'hello world').hexdigest()
print(result) # → "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed"
# Hash a string (encode to bytes first)
text = 'hello world'
hashlib.sha1(text.encode('utf-8')).hexdigest()
# → "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed"
# Hash a file in chunks
with open('file.bin', 'rb') as f:
sha1 = hashlib.sha1()
for chunk in iter(lambda: f.read(8192), b''):
sha1.update(chunk)
print(sha1.hexdigest())package main
import (
"crypto/sha1"
"fmt"
)
func main() {
data := []byte("hello world")
hash := sha1.Sum(data)
fmt.Printf("%x\n", hash)
// → 2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
}# Using sha1sum (Linux) or shasum (macOS) echo -n "hello world" | sha1sum # → 2aae6c35c94fcfb415dbe95f408b9ce91ee846ed - # macOS echo -n "hello world" | shasum -a 1 # → 2aae6c35c94fcfb415dbe95f408b9ce91ee846ed - # Hash a file sha1sum package.json # → a1b2c3d4e5f6... package.json # Using openssl (cross-platform) echo -n "hello world" | openssl sha1 # → SHA1(stdin)= 2aae6c35c94fcfb415dbe95f408b9ce91ee846ed