SHA-256

SHA-256 is a cryptographic hash function in the SHA-2 family. It produces a 256-bit digest (32 bytes), often displayed as 64 hexadecimal characters. SHA-256 takes no secret key and provides no decryption operation. The same input produces the same digest. A digest can help verify file integrity if the expected value comes from a trusted source.[1]
How SHA-256 works
SHA-256 accepts messages shorter than 264 bits. It pads the message, including an encoding of the original bit length, and processes it in 512-bit (64-byte) blocks. Each block supplies sixteen 32-bit words. The algorithm expands them into a message schedule and runs 64 rounds to update an internal state of eight 32-bit words. The final state is the digest.[1]
SHA-256 uses an iterated construction commonly described as Merkle–Damgård. The padding appends a 1 bit and enough zero bits to leave room for the 64-bit length field. The sixteen input words expand to a schedule of 64 words. Each round combines modular addition (modulo 232), bit rotations, shifts, and Boolean functions. The state produced by each block becomes the starting state for the next block.[1]
For example, NIST's test message abc consists of three ASCII bytes with no trailing newline. Its SHA-256 digest is ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad. A newline changes the input bytes, so a hash of a text file containing that line can differ.[2]
A collision occurs when two distinct messages produce the same digest. Collision resistance concerns the difficulty of finding such a pair. Preimage resistance concerns the difficulty of finding an input that matches a given digest. These properties do not prevent someone from hashing guesses for a predictable message. Password storage requires a password-hashing scheme with a salt and a cost factor.[1][3]
Second-preimage resistance addresses a third question: given a particular input, how hard is it to find a different input with the same digest? NIST rates SHA-256 at 128 bits of collision strength and 256 bits of preimage strength. The collision estimate is lower because a search can compare every generated digest with the other digests it has already seen. These are security-strength estimates, distinct from the 256-bit size of the digest.[4]
Use in Bitcoin
Bitcoin applies SHA-256 twice in several places, a construction called SHA-256d. In proof of work, miners hash the serialized 80-byte block header twice and seek a result at or below the target. The header contains the previous block's header hash and a Merkle root derived from transaction hashes. The transactions themselves are stored outside the header.[5]
Mining and chain history
The header's nBits field encodes the target. Miners vary the 32-bit nonce. They can also change the coinbase transaction to obtain a new Merkle root when they need more candidate headers. A smaller target makes a valid hash less likely on each attempt. On Bitcoin mainnet the protocol retargets every 2,016 blocks with a target interval of about ten minutes per block. Because each block points to its predecessor, changing a confirmed transaction would require redoing proof of work from the affected block and catching up with the chain's accumulated work. Nodes independently check the other consensus rules as well.[5][6]
Within the transaction Merkle tree, Bitcoin concatenates pairs of hashes as raw bytes and hashes them twice. A transaction's txid is the double SHA-256 of its serialization without witness data. Segregated Witness adds a wtxid computed with witness data and commits to a separate witness tree through the coinbase transaction.[5][7]
Addresses and signatures
A legacy P2PKH address uses the RIPEMD-160 hash of a SHA-256 hash of the public key. Base58Check adds a checksum taken from the first four bytes of a double SHA-256 digest. Other output types use hashes differently. For example, a version-0 P2WSH output commits to the single SHA-256 of a script, and the Schnorr signature scheme specified by BIP 340 uses SHA-256 with distinct tags to separate different hashing purposes.[8][7][9]
Namecoin provides a separate example: its merged-mining rules can recognize work from a SHA-256d parent chain such as Bitcoin.[10]
Standardization and certificates
NIST published SHA-256 in FIPS 180-2 in 2002. Its specification also appears in FIPS 180-4. SHA3-256 has the same digest length but belongs to the separate SHA-3 family standardized in FIPS 202.[11][12]
SHA-256 is also used with digital signature algorithms to sign TLS certificates. The CA/Browser Forum phased out SHA-1 signatures under its requirements for publicly trusted TLS certificates. The digest is an input to the signature process. A browser verifies the signature and the certificate chain as part of checking the server's identity.[13]
SHA stands for Secure Hash Algorithm. A U.S. patent on a related one-way hashing method, assigned to the United States as represented by the National Security Agency, was filed in 2001 and granted in December 2004. NIST's February 2004 change notice added SHA-224 to the standard. NIST selected Keccak in 2012 and standardized it in 2015 as SHA-3, an alternative with a different construction.[14][15][16][12]
The public web's move away from SHA-1 took place in stages. The CA/Browser Forum barred new SHA-1 subscriber and subordinate CA certificates starting January 1, 2016. Browser rejection of older certificates followed, including Mozilla's Firefox rollout in 2017. Those browser rules concerned publicly trusted TLS certificates, so the timeline should be read in that context.[13][17]
File integrity and authentication
An archive can record file digests in a manifest and recompute them later to detect changes. The BagIt packaging format specifies SHA-256 and SHA-512 support for this purpose. Bitcoin Core publishes its SHA-256 release checksums with a separate signature file. Checking the signatures against trusted public keys authenticates the list of expected checksums.[18][19]
HMAC-SHA-256 combines SHA-256 with a shared secret to authenticate a message. In TLS 1.3, a suite such as TLS_AES_128_GCM_SHA256 uses SHA-256 in key derivation and transcript hashing, while AES-GCM protects the records.[20][21]
References
- ↑ 1.0 1.1 1.2 1.3 NIST, FIPS 180-4, Secure Hash Standard, §§ 3, 5.1.1, 6.2.
- ↑ NIST SHA-256 example with intermediate values.
- ↑ NIST SP 800-63B-4, Authentication and Authenticator Management, § 3.1.1.2.
- ↑ NIST, Security Strengths of Approved Hash Functions.
- ↑ 5.0 5.1 5.2 Bitcoin Developer Reference, Block Headers and Merkle Trees.
- ↑ Bitcoin Developer Guide, Proof of Work.
- ↑ 7.0 7.1 BIP 141, Segregated Witness (Consensus layer).
- ↑ Bitcoin Developer Reference, Address Conversion.
- ↑ BIP 340, Schnorr Signatures for secp256k1.
- ↑ Namecoin documentation, merged mining.
- ↑ NIST, FIPS 180-2, Secure Hash Standard (August 2002).
- ↑ 12.0 12.1 NIST, FIPS 202, SHA-3 Standard.
- ↑ 13.0 13.1 CA/Browser Forum, TLS Baseline Requirements, § 7.1.3.2.1. RFC 5280, § 6.
- ↑ U.S. patent 6,829,355, Device for and method of one-way cryptographic hashing.
- ↑ NIST, draft FIPS 180-3 announcement and history of Change Notice 1.
- ↑ NIST, selection of Keccak for SHA-3 (2012).
- ↑ Mozilla, The end of SHA-1 on the Public Web (2017).
- ↑ RFC 8493, The BagIt File Packaging Format.
- ↑ Bitcoin Core, release verification instructions.
- ↑ RFC 4231, HMAC-SHA-224, HMAC-SHA-256, HMAC-SHA-384, and HMAC-SHA-512.
- ↑ RFC 8446, TLS 1.3, sections 4.4.1, 7.1 and Appendix B.4.