SHA-256 (Secure Hash Algorithm, 256-bit)
SHA-256 is a cryptographic hash function from the SHA-2 family that turns any input into a fixed 256-bit digest, usually written as 64 hexadecimal characters. The same input always gives the same output, and it is infeasible to work backwards from the digest.
What it does
A hash function reads input of any length and produces a short, fixed-size fingerprint. SHA-256 always outputs 32 bytes:
SHA-256("hello") =
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
A good cryptographic hash has these properties:
- Deterministic. The same input always produces the same digest.
- Avalanche effect. Changing one bit of input changes about half the output bits.
- Preimage resistance. Given a digest, you cannot find an input that produces it.
- Collision resistance. You cannot find two different inputs with the same digest.
Where it is used
- File integrity checks. Download pages publish a SHA-256 so you can confirm the file was not altered.
- Certificate fingerprints. Browsers identify a certificate by the SHA-256 of its DER bytes.
- HMAC-SHA-256, which is the
HS256algorithm for signing JWTs. - Subresource Integrity, which lets a page verify a script it loads.
- Content addressing and version control, in systems that name data by its hash.
How it compares
| Algorithm | Digest size | Status |
|---|---|---|
| MD5 | 128 bits | Broken for collisions. Fine only for non-security checksums |
| SHA-1 | 160 bits | Practical collisions demonstrated in 2017. Avoid for security |
| SHA-256 | 256 bits | Current general-purpose choice |
| SHA-512 | 512 bits | Same family, larger digest, often faster on 64-bit CPUs |
SHA-3 is a separate design with a different internal structure, not a stronger SHA-2.
Common pitfalls
- Using it to store passwords. SHA-256 is designed to be fast, which is the wrong property for passwords. Use a slow, salted algorithm such as Argon2id, scrypt, bcrypt or PBKDF2.
- Confusing hashing with encryption. A hash cannot be reversed to recover the input. It also cannot be “decrypted”.
- Hidden newlines.
echo hello | sha256sumhasheshello\n, which gives a different digest fromhello. Useecho -norprintf. - Encoding differences. Text must be turned into bytes before hashing, and UTF-8 versus UTF-16 give different digests for the same string.
- Raw hash as a keyed MAC. Never build a message authentication code as
SHA-256(secret + message). Use HMAC.
Doing it in a browser
The Web Crypto API provides it natively: crypto.subtle.digest('SHA-256', bytes). No library is needed, which is how the generator linked below computes it locally.
Related terms
- X.509 certificate — An X.509 certificate is a digitally signed document that binds a public key to an identity, such as a website's domain name. It is the foundation of HTTPS and most other public-key infrastructure, and the Internet profile is defined in RFC 5280.
- JWT (JSON Web Token) — A JSON Web Token is a compact, URL-safe string that carries a set of claims as JSON, usually signed so the receiver can detect tampering. It is defined in RFC 7519 and is widely used for API and session authentication.
- UUID (Universally Unique Identifier) — A UUID is a 128-bit identifier written as 32 hexadecimal digits in an 8-4-4-4-12 pattern, designed so that values can be generated independently anywhere with a negligible chance of collision. The current specification is RFC 9562.
References
Ads on this page
Non-personalized ads help keep Vaultools free — Google decides where they appear on the page.
Go Pro to remove them →