I'm using the SHA1/2 family of algorithms for a particular project. I was wondering if all the SHA algorithms return a fixed length hash regardless of the length of the data.
4 Answers
Essentially yes, they do.
Depending on the exact hash function you choose depends on the length of output you'd expect. For example, SHA256 produces 256 bits of output.
This does then beg the question "but the length of the hash is fixed and there are infinite possible inputs??!!". That's correct, except that $2^{256}$ is 115792089237316195423570985008687907853269984665640564039457584007913129639936. That's an awful lot of unique possible inputs which may be passed in.
You may be interested to know the same concept is used in file systems - they're called bitmaps and provide a block to bit mapping so the file system can quickly find free blocks. The numbers do scale :)
Yes. By the definition in FIPS 180-4, there are exactly
160 bits in the output of SHA-1
224 bits in the output of SHA-224
256 bits in the output of SHA-256
384 bits in the output of SHA-384
512 bits in the output of SHA-512
224 bits in the output of SHA-512/224
256 bits in the output of SHA-512/256
- 149,326
- 13
- 324
- 622
I'm was wondering if all the SHA algos return a fixed length hash regardless of the length of the data?
Others have noted that the output is indeed a constant, fixed size, so let me nit-pick one point just for completeness:
It is worth noting that these hash functions do have a limit to the input size of their data. So that input length itself has an upper bound.
From FIPS 180-4:
SHA-1, SHA-224, SHA-256: Input length is bounded to $2^{64}$ bits.
SHA-384, SHA-512, SHA-512/224, SHA-512/256: Input length is bounded to $2^{128}$ bits.
These are insanely large sizes (and it seems likely that only the ones for SHA-1, SHA-224, and SHA-256 could practically apply to electromagnetic storage). But in theory $2^{64}$ bit (approx. 1 exbibyte) restriction could prohibit calculating one hash from something like a very large database or distributed filesystem.
But for most developer concerns, these input size restrictions are practically nonexistent.
- 6,196
- 1
- 31
- 45