为什么散列输出的长度是固定的? [英] why is hash output fixed in length?

查看:191
本文介绍了为什么散列输出的长度是固定的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哈希函数总是产生一个固定长度的输出,而不管输入如何(即MD5 >> 128位,SHA-256 >> 256位),但为什么?

我知道设计师是如何设计它们的,但为什么他们设计的输出具有相同的长度?
为了能够以一致的方式存储它?更容易比较?不太复杂?

解决方案

因为这就是哈希的定义。请参阅维基百科


散列函数是任何函数,可用于将任意大小的数字数据
映射到固定大小的数字数据。


如果您的问题涉及为何有用详尽的列表):


  • 哈希通常将较大(通常是任意大小)的输入编码为较小的大小,通常以有损的方式进行编码,压缩函数,您无法通过反转该过程来重构哈希值中的输入。
  • 具有固定大小的输出非常方便,尤其是对于设计用作查找键的哈希值而言。

  • 您可以预测(预)为散列值分配存储空间,并将它们索引到连续内存段(如数组)中。 本地字大小,例如16,32和64位整数值,您可以进行非常快的相等和排序比较。
  • 任何使用散列值的算法都可以使用一组固定大小的操作来生成和处理它们。

  • 您可以预测地将在不同散列函数中生成的散列组合在一起,例如: 布隆过滤器

  • 您无需浪费任何空间来编码散列值有多大。



确实存在特殊的散列函数,能够产生输出散列例如所谓的


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?

解决方案

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.

这篇关于为什么散列输出的长度是固定的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆