什么是哈希函数?
哈希函数用于将一个大数(手机号码)或字符串映射为一个可以作为哈希表索引的较小整数的函数。比如活动开发中经常使用的MD5和SHA都是历史悠久的Hash算法。
哈希简单的理解,就是把一串长的数据变成一串短的数据,它有三个特点:
第一、它具有单向性。
我们只能够把长的数据计算成一个短的哈希,我们不可能把哈希再推导出原来的数据,很多人很好奇区块链是怎么形成,比如区块打包以后,区块就会被计算出一个哈希,哈希会存放到下一个区块当中,下一个区块里面的哈希就把上一个区块给锁定了这就是传统中的哈希锁定,区块链的不可更改就是从这里面来的,因为是前面的区块更改了,后面区块里面存在哈希就不能够对应了,所以区块链它是每一个区块都对上一个区块进行的内容的锁定,这就是哈希锁定。
第二、哈希具有唯一性。
哈希的唯一性就是我们把一个长的数据变成一个哈希数据,每一个长的数据它变成的哈希数据都是唯一与之对应的,但是这句话是有问题的,因为把长数据变成短数据,或者是把长的文件变成一个短的哈希,那在科学上,它是有一定的概率会形成相同的哈希的,只是概率极低极低,哈希有唯一性就是这么来。
第三点、哈希具有离散性。
离散性就是我们两个非常相近的文件,或者是我们两个只相差一位数的一个长数据,我们计算出来的哈希它的数值是天壤之别,是没有任何相似地方的,哈希的离散性主要是为了规避一些有特征的攻击。
How is hash applied in blockchain?In the blockchain,each block has the hash value of the previous block,which is called the parent block of the current block.Since each block has the hash value of the previous block,modifying any data of the current block will cause the hash value of the block to change,which will affect the previous block because it contains the address of the previous block.
For example,there are currently two blocks,one is the current block and the other is the parent block.The current block has the address of the parent block.
If you need to modify the data of the current block,you need to modify the parent blockchain.It is easier to modify data if there are only two blocks,but in fact there are many blocks on the blockchain.
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。