why is hash output fixed in length?

坚强是说给别人听的谎言 提交于 2019-12-12 11:41:50

问题


Hash functions always produce a fixed length output regardless of the input (i.e. MD5 >> 128 bits, SHA-256 >> 256 bits), but why?

I know that it is how the designer designed them to be, but why they designed the output to have the same length? So that it can be stored in a consistent fashion? easier to be compared? less complicated?


回答1:


Because that is what the definition of a hash is. Refer to wikipedia

A hash function is any function that can be used to map digital data of arbitrary size to digital data of fixed size.

If your question relates to why it is useful for a hash to be a fixed size there are multiple reasons (non-exhaustive list):

  • Hashes typically encode a larger (often arbitrary size) input into a smaller size, generally in a lossy way, i.e. unlike compression functions, you cannot reconstruct the input from the hash value by "reversing" the process.
  • Having a fixed size output is convenient, especially for hashes designed to be used as a lookup key.
  • You can predictably (pre)allocate storage for hash values and index them in a contiguous memory segment such as an array.
  • For hashes of "native word sizes", e.g. 16, 32 and 64 bit integer values, you can do very fast equality and ordering comparisons.
  • Any algorithm working with hash values can use a single set of fixed size operations for generating and handling them.
  • You can predictably combine hashes produced with different hash functions in e.g. a bloom filter.
  • You don't need to waste any space to encode how big the hash value is.

There do exist special hash functions, that are capable of producing an output hash of a specified fixed length, such as so-called sponge functions.




回答2:


As you can see it is the standard.

Also what you want is specified in standard :

Some application may require a hash function with a message digest length different than those provided by the hash functions in this Standard. In such cases, a truncated message digest may be used, whereby a hash function with a larger message digest length is applied to the data to be hashed, and the resulting message digest is truncated by selecting an appropriate number of the leftmost bits.




回答3:


Often it's because you want to use the hash value, or some part of it, to quickly store and look up values in a fixed-size array. (This is how a non-resizable hashtable works, for example.)

And why use a fixed-size array instead of some other, growable data structure (like a linked list or binary tree)? Because accessing them tends to be both theoretically and practically fast: provided that the hash function is good and the fraction of occupied table entries isn't too high, you get O(1) lookups (vs. O(log n) lookups for tree-based data structures or O(n) for lists) on average. And these accesses are fast in practice: after calculating the hash, which usually takes linear time in the size of the key with a low hidden constant, there's often just a bit shift, a bit mask and one or two indirect memory accesses into a contiguous block of memory that (a) makes good use of cache and (b) pipelines well on modern CPUs because few pointer indirections are needed.



来源:https://stackoverflow.com/questions/29599047/why-is-hash-output-fixed-in-length

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!