哈希表 哈希函数 时间_哈希函数的简单说明

哈希表 哈希函数 时间

首先,什么是哈希函数? (First, what is a hash function?)

Simply speaking, a hash function is a mathematical function that takes any input size and convert it to a fixed output size. Consider this simple hash function H(X) = Last digit of (X)

简而言之,哈希函数是一种数学函数,它可以接受任何输入大小并将其转换为固定的输出大小。 考虑这个简单的哈希函数H(X)=(X)的最后一位

  • H(1234) = 4.

    高(1234)= 4。
  • H(12567) = 7.

    高(12567)= 7。
  • H(127) = 7.

    高(127)= 7。
  • H(1111111111) = 1.

    H(1111111111)= 1。
  • H(24)=4.

    H(24)= 4。
  • H(24)=4.

    H(24)= 4。

So no matter what is the input and its size, we’re returning a one digit output. Another important property is that the same input will always give you the same output. H(24) will always be 4.

因此,无论输入内容和大小如何,我们都将返回一位数字的输出。 另一个重要的特性是相同的输入将始终为您提供相同的输出。 H(24)将始终为4。

In summary:

综上所述:

  • A hash function generates a fixed size output (called digest or hash)

    哈希函数生成固定大小的输出(称为摘要或哈希)
  • For any input size or length, we get always the same fixed output size

    对于任何输入大小或长度,我们总是得到相同的固定输出大小
  • Totally deterministic: for the same input we get always the same output

    完全确定性的:对于相同的输入,我们总是得到相同的输出
  • It’s not a complicated operation to get the hash of any input (very easy)

    获取任何输入的哈希值并不复杂(非常简单)
  • Several inputs can have the same hash (like here 127 and 12567 both have 7 as hash). That’s called a hash collision. It’s natural since the output has a fixed size, and the input can be of ANY size, there are infinite inputs for a fixed number of outputs. For this hashing example, there can be only 10 possible outputs (1 digit — from 0 to 9). So it will be super easy to find a hash collision, giving there are infinite numbers as inputs.

    多个输入可以具有相同的哈希值(例如此处的127和12567都具有7作为哈希值)。 这就是所谓的哈希冲突。 这是自然的,因为输出具有固定的大小,并且输入可以是任何大小,对于固定数量的输出,存在无限的输入。 对于此哈希示例,只能有10个可能的输出(1位-从0到9)。 因此,如果有无限个数字作为输入,找到哈希冲突将非常容易。

第二,什么是密码安全哈希函数? (Second, what is a cryptographic secure hash function?)

The first example I gave you, is not really a cryptographic hash function, why? because you can easily reverse it. AKA: finding an input for a given output.

我给您的第一个示例不是真正的密码哈希函数,为什么? 因为您可以轻松地将其反转。 又名:找到给定输出的输入。

Basically if i tell you what input gives a hash of 6? Any number ending with 6 is a valid answer. Simple: 123456 is a candidate, 556 is another candidate. 96 is a third candidate. The ability to reverse a hash function is a bad Cryptographic property.

基本上,如果我告诉你什么输入给出6的哈希值? 任何以6结尾的数字都是有效答案。 简单:123456是一个候选者,556是另一个候选者。 96是第三位候选人。 反转哈希函数的能力是不良的加密属性。

So cryptographic hash functions have, in addition, the following properties:

因此,加密哈希函数还具有以下属性:

  • It should be very hard, starting from a certain output, to get back one of the valid inputs.

    从某个输出开始,要返回一个有效输入应该非常困难。
  • It should be very hard and rare to find a collision (2 inputs generating the same hash)

    很难找到冲突(2个输入产生相同的哈希值)

One famous cryptographic hash functions is the SHA256. For any input size, it generates 256 bits of digest. It’s so easy for any input to get the output, you can test it yourself here: https://passwordsgenerator.net/sha256-hash-generator/

SHA256是一种著名的加密哈希函数。 对于任何输入大小,它都会生成256位摘要。 任何输入获取输出都很容易,您可以在这里自己测试: https : //passwordsgenerator.net/sha256-hash-generator/

Image for post
Test1234 has a hash 07480FB9E85B9396AF06F006CF1C95024AF2531C65FB505CFBD0ADD1E2F31573
Test1234的哈希值为07480FB9E85B9396AF06F006CF1C95024AF2531C65FB505CFBD0ADD1E2F31573

Go to this website and try the text Test1234 with capital “T”, you should get the following hash: 07480FB9E85B9396AF06F006CF1C95024AF2531C65FB505CFBD0ADD1E2F31573

转到该网站 ,尝试使用大写字母 “ T”的文本Test1234 ,您将获得以下哈希值: 07480FB9E85B9396AF06F006CF1C95024AF2531C65FB505CFBD0ADD1E2F31573

Anyone can easily validate that the text “Test1234” has actually that hash.

任何人都可以轻松地验证文本“ Test1234”实际上具有该哈希。

十六进制表示 (Hexadecimal representation)

Note that the hash is written using 64 characters just as a compact way to represents the 256 bits. This is called HEX representation, standing for hexadecimal base. The way it works is the following:

请注意,哈希是使用64个字符写入的,就像表示256位的紧凑方式一样。 这称为十六进制表示,代表十六进制。 它的工作方式如下:

Image for post
Hex to binary
十六进制到二进制

You have 16 characters in hex base ( the 0 -> 9 are 10 characters +6 letters: A,B,C,D,E,F = 16 characters in total), each can encode one of the combination of 4 bits.

您有16个十六进制字符(0-> 9是10个字符+6个字母:A,B,C,D,E,F =总共16个字符),每个字符都可以编码4位组合之一。

So when you get 64 hex for sha256, you’re getting directly 64 x 4 = 256 bits.

因此,当您为sha256获得64十六进制时,您将直接获得64 x 4 = 256位。

As you can see, a 0 in hex is actually 0000 in binary. 7 is 0111. So the hash of Test1234 is the following in binary:

如您所见,十六进制的0实际上是二进制的0000。 7是0111。因此Test1234的哈希值如下所示:

Replace 0 by 0000. Replace 7 by: 0111. Replace 4 by 0100. Replace 8 by: 1000. Replace 0 by 0000. Replace F by 1111. Replace B by 1011…

将0替换为0000。将7替换为:0111。将4替换为0100。将8替换为:1000。将0替换为0000。将F替换为1111。将B替换为1011…

So instead of : 0 7 4 8 0 F B … in Hex

因此,用十六进制代替:0 7 4 8 0 FB…

you get: 0000 0111 0100 1000 0000 1111 1011 … in Binary

您会得到:0000 0111 0100 1000 0000 1111 1011…in Binary

In summary, each hex digit is just a direct mapping of 4 bits in binary.

总而言之,每个十六进制数字只是二进制形式的4位直接映射。

反转安全哈希有多难? (How hard it it to reverse a secure hash?)

Image for post

A secure hash function should act like a random number generator. So if you change a tiny bit the input, half of the bits will change randomly at the output.

安全散列函数的作用应类似于随机数生成器。 因此,如果您更改一点输入,则一半的位将在输出处随机更改。

Actually even just changing one character from the input. Let’s say we try test1234 with small t instead of capital T.

实际上,甚至只是从输入中更改一个字符。 假设我们尝试用小t代替大写T来测试test1234。

You get the following hash of test1234:

您得到以下test1234的哈希值:

937E8D5FBB48BD4949536CD65B8D35C426B80D2F830C5C308E2CDEC422AE2244

937E8D5FBB48BD4949536CD65B8D35C426B80D2F830C5C308E2CDEC422AE2244

Nothing to do with the first hash of Test1234

与Test1234的第一个哈希表无关

07480FB9E85B9396AF06F006CF1C95024AF2531C65FB505CFBD0ADD1E2F31573

07480FB9E85B9396AF06F006CF1C95024AF2531C65FB505CFBD0ADD1E2F31573

So if a hash is very secure that it mixes well the bits before you get an output, it will cost you 2^bits to break the hash.

因此,如果散列非常安全,可以在获得输出之前很好地混合比特,那么将花费2比特来破坏散列。

So for 256 bits hash, you would need to try 2²⁵⁶ times that’s equal to 1.1x10⁷⁷ tries before you find an input (Almost the number of atoms in the universe!)

因此,对于256位哈希,您将需要尝试2²倍的次数,等于1.1x10倍的尝试次数,然后才能找到输入(几乎是宇宙中的原子数!)

If you don’t believe me, here is a challenge for you: try to find the text that generates the following hash, go to this website and try, type any random text as input, and see how close you can get to the following hash:

如果您不相信我,这对您来说是一个挑战:尝试查找生成以下哈希的文本, 访问此网站并尝试 ,输入任意随机文本作为输入,并查看离以下内容有多接近杂凑:

76B7EA4ADA84E973F394C9F5E6CDA618FC6CC246B2130B0BD2FE6092493D84A4

76B7EA4ADA84E973F394C9F5E6CDA618FC6CC246B2130B0BD2FE6092493D84A4

I would say, good luck! If you just get the starting 76B7E you would be very lucky! Let me know in the comments how many hex you manage to break?

我会说, 祝你好运 ! 如果您只是获得入门的76B7E,您将非常幸运! 在评论中让我知道您设法破解了多少个十六进制?

翻译自: https://medium.com/@assaad.moawad/hash-functions-explained-in-a-simple-way-bb08d508932

哈希表 哈希函数 时间

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值