DevTools.at

MD5 vs SHA: Which to Use?

Choosing between MD5 and SHA algorithms is crucial for application security. While both are hash functions, they have very different security properties and appropriate use cases. This guide compares the algorithms and provides clear recommendations for modern development.

MD5 (128-bit)

MD5 was designed in 1991 by Ronald Rivest as an improvement over his earlier MD4 algorithm. It produces a 128-bit (16-byte) hash, typically represented as 32 hexadecimal characters. For over a decade, MD5 was the standard hash algorithm for checksums, password storage, and data integrity verification. However, MD5's security has been progressively destroyed over the years. Theoretical weaknesses were published in 1996. In 2004, researchers demonstrated practical collision attacks. By 2008, a team used MD5 weaknesses to create a rogue CA certificate. Today, MD5 collisions can be generated in seconds on consumer hardware. The implications are severe for security applications. If attackers can create collisions, they can create two files with the same MD5 hash—one malicious, one innocent. For password hashing, rainbow tables for MD5 are comprehensive and freely available, making unsalted MD5 password hashes trivially crackable. Despite its security weaknesses, MD5 remains useful for non-security purposes. It's fast and produces reasonably short hashes. Use cases where MD5 is still acceptable include: generating cache keys where security isn't a concern, quick file change detection (not protection against tampering), non-cryptographic deduplication, legacy system compatibility where you can't change the algorithm. Key point: MD5 is not broken for collision resistance in practice for random inputs—you can still use it to verify if two files are identical (if an attacker isn't trying to make them different with the same hash). It's broken in the sense that an attacker CAN create two different files with the same hash if they control the content. For any security-sensitive application, do not use MD5. The algorithm is fundamentally compromised for cryptographic purposes.

SHA-1 (160-bit)

SHA-1 (Secure Hash Algorithm 1) was designed by the NSA and published in 1995. It produces a 160-bit (20-byte) hash, represented as 40 hexadecimal characters. SHA-1 was the dominant hash algorithm for security applications throughout the 2000s, used in SSL certificates, Git, and countless other systems. Theoretical attacks against SHA-1 were published in 2005, suggesting the algorithm wasn't as secure as previously believed. However, practical attacks remained infeasible for years. This changed dramatically in 2017 when Google and CWI Amsterdam announced "SHAttered"—the first practical SHA-1 collision. They created two different PDF files with identical SHA-1 hashes. The SHAttered attack required approximately 6,500 CPU-years of computation—significant but achievable by well-resourced attackers. As of 2020, researchers have demonstrated even more efficient attacks. Creating SHA-1 collisions is now feasible for academic research groups and certainly for nation-states. The security community has responded decisively. Major browsers deprecated SHA-1 certificates starting in 2016, and most now reject them entirely. Certificate authorities can't issue SHA-1 signed certificates. Security standards like NIST have moved SHA-1 to "deprecated" or "legacy only" status. Git's use of SHA-1 is a notable concern. While Git is transitioning to SHA-256, many repositories still use SHA-1. The practical risk is somewhat mitigated because exploiting SHA-1 collisions in Git requires specific attack scenarios, but the theoretical risk exists. SHA-1 should not be used for new security applications. It may be acceptable for legacy system compatibility or non-security applications where collision resistance isn't required. For any new project, use SHA-256 or stronger.

SHA-256 (256-bit)

SHA-256 is part of the SHA-2 family, designed by the NSA and published in 2001. It produces a 256-bit (32-byte) hash, represented as 64 hexadecimal characters. SHA-256 has become the de facto standard for cryptographic hashing, used in TLS/SSL, code signing, Bitcoin, and most modern security protocols. No practical attacks have been demonstrated against SHA-256. While some theoretical analyses have been published, they fall far short of compromising the algorithm's security. The 256-bit output provides a massive security margin—finding a collision through brute force would require approximately 2^128 operations, which is computationally infeasible with any foreseeable technology. SHA-256 is the recommended choice for most applications. It offers an excellent balance of security, performance, and widespread support. Every modern programming language has SHA-256 implementations. Hardware acceleration is available on many CPUs (Intel SHA extensions, ARM Cryptography Extensions), making SHA-256 very fast on modern systems. Common uses for SHA-256 include: TLS/HTTPS certificates for securing web traffic, digital signatures on code, documents, and email, file integrity verification for downloads and updates, merkle trees in blockchain and distributed systems, general-purpose cryptographic hashing when you need strong security. For password hashing, remember that SHA-256 alone is insufficient. While SHA-256 is secure, it's too fast for password hashing—attackers can try billions of guesses per second. Use bcrypt, scrypt, or Argon2, which are specifically designed to be slow and memory-intensive. If you're unsure which hash algorithm to use, SHA-256 is the safe default choice. It's secure, fast, well-supported, and widely audited.

SHA-512 (512-bit)

SHA-512 is the largest member of the SHA-2 family, producing a 512-bit (64-byte) hash represented as 128 hexadecimal characters. While double the output size of SHA-256, SHA-512 isn't simply "more secure" in a straightforward way—the relationship between output size and security is more nuanced. SHA-512's primary advantage is an even larger security margin. While SHA-256's collision resistance (2^128) is already far beyond practical attack, SHA-512 provides 2^256 collision resistance. This matters primarily for very long-term security or contexts where you want absolute maximum protection. Interestingly, SHA-512 can be faster than SHA-256 on 64-bit systems. SHA-512 uses 64-bit operations internally, which modern 64-bit CPUs handle efficiently. SHA-256 uses 32-bit operations that don't fully utilize 64-bit processors. In benchmarks on 64-bit systems, SHA-512 often matches or exceeds SHA-256 performance. SHA-512 sees use in: high-security applications requiring maximum margins, systems designed to remain secure for decades, cryptographic protocols that need longer hash outputs, some password hashing schemes (via SHA-512crypt). SHA-512/256 is a truncated version of SHA-512 that outputs 256 bits. It has the speed advantages of SHA-512 on 64-bit systems with the output size of SHA-256. Some modern systems prefer SHA-512/256 for this performance advantage. Choose SHA-512 when: you need the absolute maximum security margin, you're on 64-bit systems and want optimal performance, your security requirements mandate the strongest available algorithm, you're building systems intended to remain secure for 20+ years. For most applications, SHA-256 and SHA-512 are interchangeable from a security perspective. The choice often comes down to output size requirements or performance characteristics on your target platform.

Try Hash Generator

Put this knowledge into practice

Use Hash Generator

Related Articles