||This article may be too technical for most readers to understand. (July 2012)|
|Designers||National Security Agency|
|Series||(SHA-0), SHA-1, SHA-2, SHA-3|
|Certification||FIPS PUB 180-2|
|Digest sizes||224/256 bits or 384/512 bits|
|Rounds||64 or 80|
|Best public cryptanalysis|
SHA-2 is a set of cryptographic hash functions (SHA-224, SHA-256, SHA-384, SHA-512) designed by the U.S. National Security Agency (NSA) and published in 2001 by the NIST as a U.S. Federal Information Processing Standard. A hash function is an algorithm that transforms (hashes) an arbitrary set of data elements, such as a text file, into a single fixed length value (the hash). The computed hash value may then be used to verify the integrity of copies of the original data without providing any means to derive said original data. This irreversibility means that a hash value may be freely distributed or stored, as it is used for comparative purposes only. SHA stands for Secure Hash Algorithm. SHA-2 includes a significant number of changes from its predecessor, SHA-1. SHA-2 consists of a set of four hash functions with digests that are 224, 256, 384 or 512 bits.
The security provided by a hashing algorithm is entirely dependent upon its ability to produce a unique value for any specific set of data. When a hash function produces the same hash value for two different sets of data then a collision is said to occur. Collision raises the possibility that an attacker may be able to computationally craft sets of data which provide access to information secured by the hashed values of pass codes or to alter computer data files in a fashion that would not change the resulting hash value and would thereby escape detection. A strong hash function is one that is resistant to such computational attacks. A weak hash function is one where a computational approach to producing collisions is believed to be possible. A broken hash function is one where a computational method for producing collisions is known to exist.
In 2005, security flaws were identified in SHA-1, namely that a mathematical weakness might exist, indicating that a stronger hash function would be desirable. Although SHA-2 bears some similarity to the SHA-1 algorithm, these attacks have not been successfully extended to SHA-2.
Hash function 
NIST published four additional hash functions in the SHA family, named after their digest lengths (in bits): SHA-224, SHA-256, SHA-384, and SHA-512. The algorithms are collectively known as SHA-2.
The algorithms were first published in 2001 in the draft FIPS PUB 180-2, at which time review and comments were accepted. FIPS PUB 180-2, which also includes SHA-1, was released as an official standard in 2002. In February 2004, a change notice was published for FIPS PUB 180-2, specifying an additional variant, SHA-224, defined to match the key length of two-key Triple DES. These variants are patented in US 6829355 . The United States has released the patent under a royalty-free license.
SHA-256 and SHA-512 are novel hash functions computed with 32- and 64-bit words, respectively. They use different shift amounts and additive constants, but their structures are otherwise virtually identical, differing only in the number of rounds. SHA-224 and SHA-384 are simply truncated versions of the first two, computed with different initial values. In 2012, an update of the standard added the hash functions SHA-512/t that use a construction similar to SHA-384.
Comparison of SHA functions 
In the table below, internal state means the "internal hash sum" after each compression of a data block.
|Output size (bits)||Internal state size (bits)||Block size (bits)||Max message size (bits)||Word size (bits)||Rounds||Operations||Collisions found||Example Performance (MiB/s)|
|MD5 (as reference)||128||128||512||264 − 1||32||64||+,and,or,xor,rot||Yes||255|
|SHA-0||160||160||512||264 − 1||32||80||+,and,or,xor,rot||Yes||-|
|SHA-1||160||160||512||264 − 1||32||80||+,and,or,xor,rot||Theoretical attack (260)||153|
|256||512||264 − 1||32||64||+,and,or,xor,shr,rot||Yes (228.5:24)||111|
|512||1024||2128 − 1||64||80||+,and,or,xor,shr,rot||Yes (232.5:24)||99|
(5×5 array of 64-bit words)
The performance numbers above were for a single-threaded implementation on an Intel Core 2 1.83 GHz processor under Windows Vista in 32-bit mode, and serve only as a rough point for general comparison. On 64-bit machines, SHA-512 is significantly faster than SHA-256.
The cryptocurrency Bitcoin relies heavily on a double round of SHA-256. SHA-256 is used to authenticate Debian Linux software packages and in the DKIM message signing standard; SHA-512 is part of a system to authenticate archival video from the International Criminal Tribunal of the Rwandan genocide. SHA-256 and SHA-512 are proposed for use in DNSSEC. Unix and Linux vendors are moving to using 256- and 512-bit SHA-2 for secure password hashing.
SHA-1 and SHA-2 are the secure hash algorithms required by law for use in certain U.S. Government applications, including use within other cryptographic algorithms and protocols, for the protection of sensitive unclassified information. FIPS PUB 180-1 also encouraged adoption and use of SHA-1 by private and commercial organizations. SHA-1 is being retired for most government uses; the U.S. National Institute of Standards and Technology says, "Federal agencies should stop using SHA-1 for...applications that require collision resistance as soon as practical, and must use the SHA-2 family of hash functions for these applications after 2010" (emphasis in original). NIST's directive that U.S. government agencies must stop uses of SHA-1 after 2010 and the completion of SHA-3 may accelerate migration away from SHA-1.
The SHA-2 functions are not as widely used as SHA-1, despite their better security. Reasons might include lack of support for SHA-2 on systems running Windows XP SP2 or older, or a lack of perceived urgency since SHA-1 collisions have not yet been found.
Cryptanalysis and validation 
For a hash function for which L is the number of bits in the message digest, finding a message that corresponds to a given message digest can always be done using a brute force search in 2L evaluations. This is called a preimage attack and may or may not be practical depending on L and the particular computing environment. The second criterion, finding two different messages that produce the same message digest, known as a collision, requires on average only 2L/2 evaluations using a birthday attack.
In terms of practical security, a major concern about these[which?] new attacks is that they might pave the way to more efficient ones. Whether this is the case has yet to be seen, but a migration to stronger hashes is believed to be prudent. Some of the applications that use cryptographic hashes, such as password storage, are only minimally affected by a collision attack. Constructing a password that works for a given account requires a preimage attack, as well as access to the hash of the original password (typically in the shadow file) which may or may not be trivial. Reversing password encryption (e.g., to obtain a password to try against a user's account elsewhere) is not made possible by the attacks. (However, even a secure password hash cannot prevent brute-force attacks on weak passwords.)
In the case of document signing, an attacker could not simply fake a signature from an existing document—the attacker would have to produce a pair of documents, one innocuous and one damaging, and get the private key holder to sign the innocuous document. There are practical circumstances in which this is possible; until the end of 2008, it was possible to create forged SSL certificates using an MD5 collision.
There are two meet-in-the-middle preimage attacks against SHA-2 with a reduced number of rounds. The first one attacks 41-round SHA-256 out of 64 rounds with time complexity of 2253.5 and space complexity of 216, and 46-round SHA-512 out of 80 rounds with time 2511.5 and space 23. The second one attacks 42-round SHA-256 with time complexity of 2251.7 and space complexity of 212, and 42-round SHA-512 with time 2502 and space 222.
Official validation 
Implementations of all FIPS-approved security functions can be officially validated through the CMVP program, jointly run by the National Institute of Standards and Technology (NIST) and the Communications Security Establishment (CSE). For informal verification, a package to generate a high number of test vectors is made available for download on the NIST site; the resulting verification however does not replace in any way the formal CMVP validation, which is required by law for certain applications.
Examples of SHA-2 variants 
Hash values of empty string.
SHA224("") 0x d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f SHA256("") 0x e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 SHA384("") 0x 38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b SHA512("") 0x cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e
Even a small change in the message will (with overwhelming probability) result in a mostly different hash, due to the avalanche effect. For example, adding a period to the end of the sentence:
SHA224("The quick brown fox jumps over the lazy dog") 0x 730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525 SHA224("The quick brown fox jumps over the lazy dog.") 0x 619cba8e8e05826e9b8c519c0a5c68f4fb653e8a3d8aa04bb2c8cd4c SHA256("The quick brown fox jumps over the lazy dog") 0x d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592 SHA256("The quick brown fox jumps over the lazy dog.") 0x ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c SHA384("The quick brown fox jumps over the lazy dog") 0x ca737f1014a48f4c0b6dd43cb177b0afd9e5169367544c494011e3317dbf9a509cb1e5dc1e85a941bbee3d7f2afbc9b1 SHA384("The quick brown fox jumps over the lazy dog.") 0x ed892481d8272ca6df370bf706e4d7bc1b5739fa2177aae6c50e946678718fc67a7af2819a021c2fc34e91bdb63409d7 SHA512("The quick brown fox jumps over the lazy dog") 0x 07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6 SHA512("The quick brown fox jumps over the lazy dog.") 0x 91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bbc6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed
Pseudocode for the SHA-256 algorithm follows. Note the great increase in mixing between bits of the
w[16..63] words compared to SHA-1.
Note 1: All variables are unsigned 32 bits and wrap modulo 232 when calculating Note 2: All constants in this pseudo code are in big endian Initialize variables (first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19): h[0..7] := 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 Initialize table of round constants (first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311): k[0..63] := 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 Pre-processing: append the bit '1' to the message append k bits '0', where k is the minimum number >= 0 such that the resulting message length (in bits) is 448 (modulo 512). append length of message (before pre-processing), in bits, as 64-bit big-endian integer Process the message in successive 512-bit chunks: break message into 512-bit chunks for each chunk break chunk into sixteen 32-bit big-endian words w[0..15] Extend the sixteen 32-bit words into sixty-four 32-bit words: for i from 16 to 63 s0 := (w[i-15] rightrotate 7) xor (w[i-15] rightrotate 18) xor (w[i-15] rightshift 3) s1 := (w[i-2] rightrotate 17) xor (w[i-2] rightrotate 19) xor (w[i-2] rightshift 10) w[i] := w[i-16] + s0 + w[i-7] + s1 Initialize hash value for this chunk: a := h0 b := h1 c := h2 d := h3 e := h4 f := h5 g := h6 h := h7 Main loop: for i from 0 to 63 S1 := (e rightrotate 6) xor (e rightrotate 11) xor (e rightrotate 25) ch := (e and f) xor ((not e) and g) temp := h + S1 + ch + k[i] + w[i] d := d + temp; S0 := (a rightrotate 2) xor (a rightrotate 13) xor (a rightrotate 22) maj := (a and (b xor c)) xor (b and c) temp := temp + S0 + maj h := g g := f f := e e := d d := c c := b b := a a := temp Add this chunk's hash to result so far: h0 := h0 + a h1 := h1 + b h2 := h2 + c h3 := h3 + d h4 := h4 + e h5 := h5 + f h6 := h6 + g h7 := h7 + h Produce the final hash value (big-endian): digest := hash := h0 append h1 append h2 append h3 append h4 append h5 append h6 append h7
The computation of the
maj values can be optimized the same way as described for SHA-1.
SHA-224 is identical to SHA-256, except that:
- the initial variable values
h7are different, and
- the output is constructed by omitting
Here the initial values for the variables (in big endian): (The second 32 bits of the fractional parts of the square roots of the 9th through 16th primes 23..53) h[0..7] := 0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4
SHA-512 is identical in structure, but:
- the message is broken into 1024-bit chunks,
- all numbers are 64 bits long,
- there are 80 rounds instead of 64,
- the appended length of the message (before pre-processing), in bits, is a 128-bit big-endian integer
- the initial values and additive constants are extended to 64 bits, and
- the shift and rotate amounts used are different.
SHA-384 is identical to SHA-512, except that:
- the initial values
h7are different (taken from the 9th through 16th primes), and
- the output is constructed by omitting
SHA-512/t is identical to SHA-512 except that:
- the initial values
h7are given by the SHA-512/t IV generation function,
- the output is constructed by truncating the concatenation of
h7at t bits,
- t equal to 384 is not allowed, instead SHA-384 should be used as specified, and
- t values 224 and 256 are especially mentioned as approved.
Implementations in common languages 
Other implementations 
- Bouncy Castle
- The Bouncy Castle Library is a free Java and C# class library that contains implementations of the SHA-1, SHA-224, SHA-256, SHA-384 and SHA-512 algorithms as well as other algorithms like Whirlpool, Tiger, RIPEMD, GOST-3411, MD2, MD4 and MD5.
- an open source cross-platform software security toolkit library.
- An open source, permissively licensed, C++ class library of cryptographic schemes, including implementations of the SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512 algorithms.
- A formally verified implementation of several widely used symmetric cryptographic algorithms using the SPARK programming language and toolset, complete library proofs of the absence of run-time errors like type range violations, division by zero and numerical overflows are available.
- A portable ISO C cryptographic toolkit, Public Domain.
- A general purpose cryptographic library based on the code from GNU Privacy Guard.
- A set of programs to compute MD5, SHA-1, SHA-256, Tiger, or Whirlpool cryptographic message digests on an arbitrary number of files. It is used in computer security, system administration and computer forensics communities for purposes of running large numbers of files through any of several different cryptographic digests. It is similar to sha1sum from GNU Core Utilities and md5sum.
- The widely used OpenSSL
cryptolibrary includes free, open-source – implementations of SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512.
- The small PolarSSL
SSLlibrary includes open-source – implementations of SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512.
- A free, open-source library which implements many hash functions, including SHA-1 and SHA-2, both in portable (but optimized) C and in Java.
- A free, open-source collection of hardware implementations of hash functions (including SHA-1 and SHA-2) in portable (but optimized) VHDL.
See also 
- Comparison of cryptographic hash functions
- Digital timestamping
- Hash based Message Authentication Code (HMAC) or Keyed Hash
- Hash collision
- International Association for Cryptologic Research (IACR)
- Secure Hash Standard
- sha1sum (sha224sum, sha256sum, sha384sum and sha512sum) commands
- Yu Sasaki, Lei Wang, and Kazumaro Aoki (2008-11-25). Preimage Attacks on 41-Step SHA-256 and 46-Step SHA-512.
- "Schneier on Security: Cryptanalysis of SHA-1". Schneier.com. Retrieved 2011-11-08.
- NIST Selects Winner of Secure Hash Algorithm (SHA-3) Competition
- Licensing Declaration for US patent 6829355.. Retrieved 2008-02-17.
- Mario Lamberger and Florian Mendel, Higher-Order Differential Attack on Reduced SHA-256, accessed August 24, 2012.
- Yu Sasaki, Lei Wang, and Kazumaro Aoki, Preimage Attacks on 41-Step SHA-256 and 46-Step SHA-512, accessed January 3, 2010.
- "Crypto++ 5.6.0 Benchmarks". Retrieved 2011-02-27.
- "Cryptanalysis of MD5 & SHA-1" (PDF). Retrieved 2013-04-25.
- "Debian codebase in Google Code". Google. Retrieved 2011-11-08.[dead link]
- John Markoff, A Tool to Verify Digital Records, Even as Technology Shifts, New York Times, January 26, 2009
- RFC 5702,RFC-Editor.org
- Ulrich Drepper, Unix crypt with SHA-256/512
- National Institute on Standards and Technology Computer Security Resource Center, NIST's Policy on Hash Functions, accessed March 29, 2009.
- "Secure Hashing - NIST Computer Security Division - Computer Security Resource Center". NIST. Retrieved 2010-11-25.
- Microsoft Corporation,Overview of Windows XP Service Pack 3
- Alexander Sotirov, Marc Stevens, Jacob Appelbaum, Arjen Lenstra, David Molnar, Dag Arne Osvik, Benne de Weger, MD5 considered harmful today: Creating a rogue CA certificate, accessed March 29, 2009.
- Jian Guo, Krystian Matusiewicz (2008-11-25). Preimages for Step-Reduced SHA-2.
- "The Go Programming Language". Retrieved 2013-02-04.
- "Hashing Java". OWASP. Retrieved 2011-11-08.
- "14.1. hashlib — Secure hashes and message digests — Python v2.7.2 documentation". Docs.python.org. Retrieved 2011-11-08.
- "hash - Manual". PHP. 2011-11-04. Retrieved 2011-11-08.
- "Digest::SHA". search.cpan.org. Retrieved 2011-11-08.
- Henri Gilbert, Helena Handschuh: Security Analysis of SHA-256 and Sisters. Selected Areas in Cryptography 2003: pp175–193
- "Proposed Revision of Federal Information Processing Standard (FIPS) 180, Secure Hash Standard". Federal Register 59 (131): 35317–35318. 1994-07-11. Retrieved 2007-04-26.
- Descriptions of SHA-256, SHA-384, and SHA-512 from NIST
- Specifications for a Secure Hash Standard (SHS) – Draft for proposed SHS (SHA-0)
- Secure Hash Standard (SHS) – Proposed SHS (SHA-0)
- CSRC Cryptographic Toolkit – Official NIST site for the Secure Hash Standard
- Test vectors for SHA-256/384/512 from the NESSIE project
- Test vectors for SHA-1, SHA-2 from NIST site
- NIST Cryptographic Hash Project SHA-3 competition
- RFC 3874: A 224-bit One-way Hash Function: SHA-224.
- RFC 6234: US Secure Hash Algorithms SHA and SHA-based HMAC and HKDF. Contains sample C implementation.