What Is SHA-256?
SHA-256 (Secure Hash Algorithm 256-bit) is a member of the SHA-2 family of cryptographic hash functions designed by the National Security Agency (NSA) and published by the National Institute of Standards and Technology (NIST). It takes an input of any length and produces a fixed-size 256-bit (32-byte) hash value, typically represented as a 64-character hexadecimal string.
SHA-256 was first published in 2001 as part of FIPS PUB 180-2 and has since become one of the most widely used hash functions in the world. It is the hash algorithm behind Bitcoin mining, TLS certificates, digital signatures, password storage, and countless other security applications. Its 256-bit output provides an astronomically large hash space of 2^256 possible values, making collisions practically impossible with current and foreseeable computing technology.
Like all cryptographic hash functions, SHA-256 exhibits three critical properties: preimage resistance (you cannot determine the input from the hash), second preimage resistance (you cannot find a different input that produces the same hash), and collision resistance (you cannot find any two inputs that produce the same hash). As of 2026, no practical attack has been found that compromises any of these properties for SHA-256.
How SHA-256 Works
The SHA-256 algorithm follows a structure similar to MD5 but with significantly larger internal state and more complex operations, making it far more secure.
Message Padding
The input message is padded with a 1 bit, followed by zeros, followed by a 64-bit representation of the message length. The padding ensures the total length is a multiple of 512 bits.
Block Processing
The padded message is split into 512-bit blocks. Each block is expanded into a 64-entry message schedule (W array) using a specific word expansion algorithm. The algorithm then processes each block through 64 rounds of compression, maintaining eight 32-bit working variables (a through h) that are initialized from eight 32-bit hash constants derived from the fractional parts of the square roots of the first eight prime numbers.
The Compression Function
Each of the 64 rounds applies a series of operations including the Ch (Choose) and Maj (Majority) functions, bitwise rotations, and additions modulo 2^32. The Ch function selects bits from two inputs based on a third: Ch(x, y, z) = (x AND y) XOR (NOT x AND z). The Maj function outputs the majority bit: Maj(x, y, z) = (x AND y) XOR (x AND z) XOR (y AND z). Additionally, SHA-256 uses six 64-element constant arrays derived from the fractional parts of cube roots of the first 64 prime numbers.
Final Hash Output
After processing all blocks, the eight working variables are concatenated to form the final 256-bit hash value. The result is deterministic: the same input always produces the same output, and changing even a single bit of the input changes approximately half of the output bits due to the avalanche effect.
Why SHA-256 Is the Industry Standard
- Proven security: After more than two decades of public scrutiny, no practical attack has been found against SHA-256. The best known theoretical attack reduces the preimage resistance from 2^256 to approximately 2^254.3, which is still completely infeasible.
- Widespread hardware support: Modern CPUs include dedicated SHA-256 extension instructions (Intel SHA Extensions, ARM Crypto Extensions) that dramatically accelerate SHA-256 computation.
- Regulatory compliance: SHA-256 is approved by NIST and meets the requirements of FIPS 180-4, making it suitable for government and regulated industry applications.
- Blockchain backbone: Bitcoin and many other cryptocurrencies use SHA-256 as their proof-of-work function, creating massive investment in SHA-256 ASIC hardware.
Practical Applications of SHA-256
Password Hashing and HMAC
While SHA-256 alone is too fast for direct password hashing, it is used as the underlying hash function in many password hashing schemes. HMAC-SHA-256 is commonly used for API key verification and message authentication.
# HMAC-SHA-256 for API authentication import hmac, hashlib key = b"secret-api-key" message = b"request-data" signature = hmac.new(key, message, hashlib.sha256).hexdigest()
Digital Certificates and TLS
Most SSL/TLS certificates issued today are signed using SHA-256. When you see a padlock icon in your browser, the certificate chain almost certainly uses SHA-256 signatures. The transition from SHA-1 to SHA-256 for certificates was largely completed by 2017.
File Integrity and Checksums
SHA-256 is the recommended hash function for verifying file integrity in security-sensitive contexts. Unlike MD5, which is vulnerable to collision attacks, SHA-256 provides confidence that the file has not been tampered with.
# Linux sha256sum important-file.tar.gz # macOS shasum -a 256 important-file.tar.gz # Windows PowerShell Get-FileHash -Algorithm SHA256 important-file.tar.gz
Blockchain and Cryptocurrency
Bitcoin mining is essentially a massive SHA-256 computation race. Miners repeatedly hash block headers with different nonces until they find a hash below the current difficulty target. This proof-of-work mechanism secures the Bitcoin network and has driven the development of specialized SHA-256 mining hardware (ASICs).
Code Signing and Software Distribution
Software vendors use SHA-256 hashes to sign their releases. When you download software, the provided SHA-256 checksum lets you verify that the file has not been modified in transit. Package managers like npm, pip, and apt use SHA-256 to verify package integrity.
How to Generate SHA-256 Hashes
Online SHA-256 Generator
Our free online SHA-256 generator lets you compute SHA-256 hashes of any text directly in your browser. No data is sent to any server, all computation happens client-side using the Web Crypto API.
Command Line
# Hash a string echo -n "hello" | sha256sum # Hash a file sha256sum myfile.txt # macOS alternative echo -n "hello" | shasum -a 256
Programming Languages
# Python
import hashlib
hash = hashlib.sha256(b"hello").hexdigest()
# 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
# JavaScript (browser)
async function sha256(message) {
const buffer = new TextEncoder().encode(message);
const hash = await crypto.subtle.digest("SHA-256", buffer);
return Array.from(new Uint8Array(hash)).map(b => b.toString(16).padStart(2, "0")).join("");
}
# Node.js
const crypto = require("crypto");
const hash = crypto.createHash("sha256").update("hello").digest("hex");
SHA-256 vs Other Hash Functions
SHA-256 vs MD5
SHA-256 produces a 256-bit hash versus MD5's 128-bit hash. SHA-256 is cryptographically secure while MD5 is broken. SHA-256 is slower but provides dramatically stronger security guarantees. Use SHA-256 for any security-sensitive application.
SHA-256 vs SHA-1
SHA-1 produces a 160-bit hash and was deprecated by NIST in 2011 after practical collision attacks were demonstrated. SHA-256 is its recommended successor. Most browsers and certificate authorities no longer accept SHA-1 certificates.
SHA-256 vs SHA-512
SHA-512 produces a 512-bit hash and is faster on 64-bit processors due to its use of 64-bit arithmetic. On 32-bit systems, SHA-256 is typically faster. For most applications, SHA-256 provides more than enough security, but SHA-512 may be preferred in high-security environments.
SHA-256 vs SHA-3
SHA-3 (Keccak) uses a fundamentally different construction (sponge function) from SHA-2 (Merkle-Damgard). SHA-3 provides a backup in case weaknesses are ever found in SHA-2, but no such weaknesses exist as of 2026.
Security Considerations
- Salt your hashes: When hashing passwords or sensitive data, always use a unique random salt. This prevents rainbow table attacks and ensures that identical inputs produce different hashes.
- Use a key derivation function: For password storage, use PBKDF2, bcrypt, scrypt, or Argon2 instead of raw SHA-256. These functions add key stretching to slow down brute force attacks.
- Avoid hash length extension attacks: Raw SHA-256 is vulnerable to length extension attacks. Use HMAC-SHA-256 when you need a keyed hash for message authentication.
- Verify hash comparisons securely: Use constant-time comparison functions when comparing hashes to prevent timing attacks.
SHA-256 in Blockchain Technology
SHA-256 is fundamental to blockchain technology. In Bitcoin, it is used in three key places: mining (proof of work), address generation (RIPEMD-160(SHA-256(public_key))), and the Merkle tree structure that organizes transactions within a block. The security of Bitcoin's proof-of-work relies entirely on the preimage resistance and puzzle-friendliness properties of SHA-256.
Best Practices
- Default to SHA-256 for any new project that needs a hash function. It is widely supported, well-understood, and secure.
- Never roll your own crypto. Use well-tested libraries (OpenSSL, libsodium, bcrypt) instead of implementing hash functions yourself.
- Stay informed about security research. While SHA-256 is secure today, follow NIST guidelines for hash function recommendations.
- Use HMAC-SHA-256 for message authentication. Do not use raw SHA-256 with a concatenated key for MAC construction.
- Generate SHA-256 checksums for all distributed software. Publish them alongside your releases so users can verify file integrity.
Try our free online tool to get results instantly in your browser.
Frequently Asked Questions
What is SHA-256?
SHA-256 is a cryptographic hash function that produces a 256-bit (64-character hexadecimal) hash from any input. It was designed by the NSA and is the industry standard for digital signatures, certificates, and blockchain technology.
Is SHA-256 secure?
Yes, SHA-256 is considered cryptographically secure as of 2026. No practical attack has been found that compromises its preimage, second preimage, or collision resistance. It is approved by NIST for all security applications.
What is the difference between SHA-256 and MD5?
SHA-256 produces a 256-bit hash while MD5 produces a 128-bit hash. SHA-256 is cryptographically secure while MD5 is broken. SHA-256 is the recommended choice for any security application.
Can SHA-256 be reversed?
No, SHA-256 is a one-way function. It is computationally infeasible to determine the original input from its hash output. The 2^256 search space makes brute force reversal impossible with current technology.
How do I generate a SHA-256 hash?
On Linux: echo -n 'text' | sha256sum. In Python: hashlib.sha256(b'text').hexdigest(). Or use our free online SHA-256 generator tool for instant results in your browser.