MD5 & SHA-256 哈希生成器 — 在线生成与验证文件哈希
即时为任意文本或文件生成 MD5、SHA-1、SHA-256、SHA-384 和 SHA-512 哈希值。创建 HMAC 签名,并验证文件的完整性校验和。
Hash Generator
Generate MD5, SHA-1, SHA-256, SHA-384, and SHA-512 hashes for text or files. Supports HMAC authentication codes. All processing happens locally in your browser.
Frequently Asked Questions About Hash Generation
什么是哈希函数?
哈希函数是一种数学算法,接受任意大小的输入并产生称为哈希值、摘要或校验和的固定大小输出。相同的输入始终产生相同的哈希值,即使输入的微小变化也会产生完全不同的输出。哈希函数是单向的——您无法通过哈希值还原原始数据。
MD5和SHA-256有什么区别?
MD5产生128位(32个字符十六进制)哈希值,速度明显更快,但它在密码学上已被攻破——研究人员自2004年起已证明实际碰撞攻击。SHA-256产生256位(64个字符十六进制)哈希值,是由NSA设计的SHA-2家族的一部分。SHA-256没有已知的实际攻击,是包括TLS证书和区块链在内的安全应用的推荐标准。
MD5还安全吗?
不。MD5在密码学上已被攻破,不应用于数字签名、证书验证或密码哈希等安全目的。在现代硬件上,碰撞攻击可在数秒内完成。但MD5对于非安全用途仍然可以接受,例如基本文件校验和以及不关心恶意篡改的数据去重场景。
如何用哈希值验证文件完整性?
下载文件和来源发布的哈希值(通常是SHA-256校验和)。使用本工具的文件上传选项卡生成您下载文件的哈希值。将生成的哈希值与发布的哈希值进行比对——如果完全匹配,则文件是完整的,在传输过程中没有损坏或被篡改。
什么是HMAC,它与普通哈希有什么不同?
HMAC(基于哈希的消息认证码)将哈希函数与密钥结合,生成认证码。与仅验证数据完整性的普通哈希不同,HMAC还可以证明真实性——只有拥有密钥的人才能生成正确的HMAC。HMAC用于API认证(AWS、Stripe)、JWT令牌(HS256=HMAC-SHA256)、TLS握手和Webhook验证。
什么是彩虹表?
彩虹表是预先计算好的查找表,将哈希值映射回原始输入。攻击者使用它们通过在表中查找哈希值来破解密码哈希,而不必暴力尝试每种可能性。彩虹表对未加盐的哈希有效,这就是为什么现代密码存储始终使用加盐(在哈希之前添加随机数据)来抵御彩虹表攻击。
哈希中的盐是什么?
盐是在哈希之前添加到输入中的随机值。每个密码都有唯一的盐,因此即使两个用户有相同的密码,它们存储的哈希值也会不同。加盐防止彩虹表攻击,使预计算不可行。盐与哈希值一起存储——它不需要保密,只需每个密码唯一即可。
为什么要用bcrypt或Argon2而不是SHA-256来存储密码?
SHA-256被设计为快速的,这对密码哈希来说是个缺点——攻击者每秒可以尝试数十亿次猜测。bcrypt、scrypt和Argon2是专门用于密码哈希的函数,故意设计为缓慢且内存密集型。它们包含可配置的工作因子,可以随着硬件改进而增加,使暴力攻击不切实际。
两个不同的输入会产生相同的哈希值吗?
理论上是的——这称为碰撞。由于哈希函数将无限可能的输入映射到有限的输出空间,碰撞必然存在。但对于像SHA-256这样的安全哈希函数,找到碰撞在计算上是不可行的(用当前技术需要数十亿年)。对于像MD5这样已被攻破的算法,碰撞可以在数秒内找到。
使用此工具时,我的数据会离开浏览器吗?
不会。所有哈希计算完全在您的浏览器中使用Web Crypto API和JavaScript进行。您的文本和文件永远不会上传到任何服务器。您可以在断网状态下使用该工具来验证这一点——它的工作效果完全相同。
SHA-512用于什么?
SHA-512产生512位(128个字符十六进制)哈希值,是SHA-2家族的一部分。它用于高安全性应用、数字签名、证书颁发机构,以及作为TLS 1.3等协议的构建块。SHA-512在64位处理器上实际上比SHA-256更快,因为其算法自然地在64位字上运行。它也是SHA-512/256的基础,用于某些加密货币实现中。
如何在电脑上生成文件的MD5或SHA-256哈希值?
在Windows上,打开PowerShell并运行:Get-FileHash -Algorithm SHA256 file.txt(将SHA256替换为MD5)。在macOS/Linux上,使用终端:sha256sum file.txt(Linux)或shasum -a 256 file.txt(macOS)。对于MD5:md5sum file.txt(Linux)或md5 file.txt(macOS)。或者,使用本页面的文件上传选项卡——它完全在您的浏览器中处理文件。
如何在Python或JavaScript中生成哈希值?
在Python中:import hashlib; hashlib.sha256(b"hello").hexdigest()返回SHA-256哈希值。对于MD5:hashlib.md5(b"hello").hexdigest()。在JavaScript(Node.js)中:crypto.createHash("sha256").update("hello").digest("hex")。在浏览器JavaScript中:await crypto.subtle.digest("SHA-256", new TextEncoder().encode("hello")),然后将ArrayBuffer转换为十六进制。
How to Generate a Hash Online
A cryptographic hash function converts any input — a password, a file, a message — into a fixed-length string of characters called a hash (also known as a digest or checksum). This tool computes MD5, SHA-1, SHA-256, SHA-384, and SHA-512 hashes simultaneously, entirely in your browser using the Web Crypto API. No data is ever sent to a server.
- Choose the Text Input tab to hash a string, or File Upload to hash a file, or HMAC to generate an authentication code.
- Type or paste your text, or drag and drop a file onto the upload zone.
- All five hash algorithms are computed in real time and displayed below the input.
- Use the output format toggle to switch between lowercase hex, uppercase hex, or Base64 encoding.
- Click the copy button to copy any hash. Use the Compare section to verify a hash matches.
MD5 vs SHA-256: Which Hash Algorithm Should You Use?
MD5 and SHA-256 are the two most commonly searched hash algorithms, but they serve very different purposes today. MD5 was designed by Ronald Rivest in 1991 and produces a 128-bit hash. MD5 is cryptographically broken— researchers demonstrated practical collision attacks in 2004, and by 2008 MD5 was used to forge a rogue SSL certificate. It should not be used for security purposes.
SHA-256, part of the SHA-2 family designed by the NSA, produces a 256-bit hash with no known practical attacks. It is the recommended standard for TLS certificates, blockchain (Bitcoin uses double-SHA-256), digital signatures, and any security-sensitive application. SHA-256 is slower than MD5 but dramatically more secure.
Rule of thumb: Use SHA-256 (or SHA-512) for anything security-related. MD5 is only acceptable for non-security checksums like data deduplication or legacy system compatibility where tampering is not a concern.
Hash Algorithm Comparison Table
The following table compares the five hash algorithms supported by this tool:
| Algorithm | Output Size | Security Status | Speed | Primary Use Case |
|---|---|---|---|---|
| MD5 | 128 bits (32 hex) | Broken | Very fast | Legacy checksums only |
| SHA-1 | 160 bits (40 hex) | Deprecated | Fast | Legacy systems, Git (transitioning) |
| SHA-256 | 256 bits (64 hex) | Secure | Moderate | TLS certificates, blockchain, general use |
| SHA-384 | 384 bits (96 hex) | Secure | Moderate | Government and high-security applications |
| SHA-512 | 512 bits (128 hex) | Secure | Fast on 64-bit | Maximum security, large data integrity |
How to Verify File Integrity with a Hash
Software publishers often provide a SHA-256 checksum alongside their downloads so you can verify the file was not corrupted or tampered with during transfer. Here's how to use this tool for verification:
- Download the file and note the published hash (usually SHA-256) from the official source.
- Switch to the File Upload tab and drag your downloaded file onto the upload zone.
- Copy the generated SHA-256 hash from the results.
- Paste the published hash into the Compare Hash section — if it shows "Match," your file is authentic.
You can also verify file hashes from the command line: certutil -hashfile file.zip SHA256 on Windows, sha256sum file.zip on Linux, or shasum -a 256 file.zip on macOS.
What Is HMAC and How Does It Work?
HMAC (Hash-based Message Authentication Code) combines a cryptographic hash function with a secret key to produce an authentication code. Unlike a plain hash, HMAC proves both the integrity andthe authenticity of a message — only someone with the secret key can generate the correct HMAC.
HMAC is defined in RFC 2104 as: HMAC(K, m) = H((K ⊕ opad) || H((K ⊕ ipad) || m)), where H is the hash function, K is the key padded to the block size, and ipad/opad are fixed padding constants (0x36 and 0x5c).
HMAC is used in TLS/SSL handshakes, API authentication (AWS Signature V4, Stripe webhooks), JWT token signing (HS256 = HMAC-SHA256), OAuth 1.0 request signing, and IPsec VPN authentication. Use the HMAC tab in this tool to generate HMAC values with any of the five supported algorithms.
Hash Generation in Programming Languages
Here are quick reference examples for generating SHA-256 hashes in popular programming languages:
| Language | SHA-256 Code Example |
|---|---|
| Python | hashlib.sha256(b"hello").hexdigest() |
| JavaScript | crypto.subtle.digest("SHA-256", data) |
| PHP | hash("sha256", "hello") |
| Java | MessageDigest.getInstance("SHA-256").digest(bytes) |
| C# | SHA256.HashData(Encoding.UTF8.GetBytes("hello")) |
| Go | sha256.Sum256([]byte("hello")) |
| Bash | echo -n "hello" | sha256sum |
| PowerShell | Get-FileHash -Algorithm SHA256 file.txt |
For MD5, replace the algorithm name accordingly: hashlib.md5() in Python, hash("md5", ...) in PHP, MD5.HashData() in C#. Note that most languages provide built-in hash functions through their standard crypto libraries.
Common Hash Use Cases
- File Integrity Verification — Compare a file's hash against the publisher's checksum to detect corruption or tampering
- Password Storage — Modern systems store password hashes (with salt) using specialized algorithms like bcrypt or Argon2, never plaintext
- Digital Signatures — A document is hashed, then the hash is signed with a private key for authenticity verification
- Blockchain & Cryptocurrency — Bitcoin uses double-SHA-256; Ethereum uses Keccak-256. Each block links to the previous via its hash
- Data Deduplication — Hash values quickly identify duplicate files without byte-by-byte comparison
- Content Addressing — Git uses SHA-1 (migrating to SHA-256), IPFS and Docker use SHA-256 as content identifiers
- API Authentication — HMAC-SHA256 authenticates API requests in AWS, Stripe, and many webhook systems
- SRI (Subresource Integrity) — Browsers verify CDN-hosted scripts with Base64-encoded SHA-384 hashes in the
integrityattribute
Learn More About Hashing
- How Cryptographic Hash Functions Work — MD5 vs SHA-256 vs SHA-512 Explained — Deep dive into hash function internals, collision attacks, password hashing with bcrypt/Argon2, HMAC, and real-world applications in blockchain and TLS
Related Tools
- Base64 Encoder & Decoder — Encode and decode data in Base64 format, commonly used alongside hashing for SRI and JWT
- Hex to Text Converter — Convert hexadecimal hash output to readable text or raw bytes
- Hex to Binary Converter — Convert hash hex strings to binary representation
- URL Encoder & Decoder — Percent-encode special characters in URLs, often needed when passing hash values as query parameters
- Binary Translator — Convert between binary and text using ASCII/UTF-8 encoding
- Caesar Cipher — Explore classical cryptography foundations