create account

每天进步一点点:HASH以及HMAC by oflyhigh

View this thread on: hive.blogpeakd.comecency.com
· @oflyhigh ·
$19.39
每天进步一点点:HASH以及HMAC
今天早些时候的帖子提到一个STEEM的菠菜应用Magic Dice,并且我说它解决了透明性和公平性等问题,简单来讲,就是***庄家(服务提供者)无法出老千(控制开奖结果)***。

![](https://steemitimages.com/DQmRkLq6rRew3mHfx4vYGWyqpC8wSebLPeC2iZCXAdpuGkR/image.png)
(图源 :[pixabay](https://pixabay.com/))



在更进一步介绍它如何实现无法出老千这个问题之前,我们先来简单看看和密码学相关的两个概念。因为我不是专家,所以就粗略介绍一下,太深入的讲解就会贻笑大方的。

# HASH

>Hash,一般翻译做“散列”,也有直接音译为“哈希”的,就是把任意长度的输入(又叫做预映射pre-image)通过散列算法变换成固定长度的输出,该输出就是散列值。
——来自百度百科[Hash (散列函数)](https://baike.baidu.com/item/Hash/390310)

这个东西有什么用呢?它的用途之一就是防篡改(其它用途我们暂不讨论)。在我70大寿(Reputation 70)的时候,曾经搞过一次奖励为100个STEEM的抽奖,为了证明抽奖的公平性,中奖数字是已HASH的形式发布到抽奖活动贴里,这样开奖时,公布生成中奖数字的代码,就可以证明我没有作弊。

比如,活动贴中我公布了如下HASH
>💰一等奖抽奖密码: fcc420adc5de61752db7ecfa837564f45c47852b
💰二等将抽奖密码: 69bc4460aaab914869fa8209da3d06f1494ea62d
💰三等将抽奖密码: 0e756e1b5d7dc2bab3d86b3d490d3801b904f929

而开奖贴我公布了如下代码(其中sha1是hash所使用的算法,我们还可以使用MD5、SHA256等等):
>`hashlib.sha1(bytes('一等奖中奖密码:6', 'utf-8')).hexdigest()`
'fcc420adc5de61752db7ecfa837564f45c47852b'
`hashlib.sha1(bytes('二等奖中奖密码:8', 'utf-8')).hexdigest()`
'69bc4460aaab914869fa8209da3d06f1494ea62d'
`hashlib.sha1(bytes('三等奖中奖密码:1', 'utf-8')).hexdigest()`
'0e756e1b5d7dc2bab3d86b3d490d3801b904f929'

如果我想作弊,那么必须用不同的内容,生成相同的HASH,那几乎是不可能的。比特币以及STEEM中签名算法,都用到了HASH函数,这也是防篡改的应用之一吧。

# HMAC

在说HMAC之前,我们在回到HASH上来。以网站常用与身份验证MD5为例,网站一般不保存用户密码,而是保存用户密码的HASH(MD5值),当用户登录授权时,网站必对用户密码以及数据库中的MD5值,判断是否是合法用户。

但是这样做存在一个风险,假设网站包含用户密码MD5值的数据库泄露,那么我用常见密码字典生成一个MD5字典,并于网站数据库中的值进行比对,这样就有很大的可能碰撞出一大堆用户名密码的明文。(彩虹攻击)

那么如何防止这种情况呢?简单的办法是加一个混淆量,比如之前的代码:
>`hashlib.sha1(bytes('一等奖中奖密码:6', 'utf-8')).hexdigest()`

可以改成
>`hashlib.sha1(bytes('mypassword'+'一等奖中奖密码:6', 'utf-8')).hexdigest()`

但是一种更简单更安全的方式是使用HMAC,简单来讲,可以理解为***带密码的HASH***。

所以上述代码可以改写成:
>`hmac.new(b'mypassword', bytes('一等奖中奖密码:6', 'utf-8'), digestmod='SHA1').hexdigest()`

想了解更多详情的,可以移步https://en.wikipedia.org/wiki/HMAC,总之我是看不懂啦。

![](https://cdn.steemitimages.com/DQmT5UpzBvHpwci9Xxp3AYBVnn1gp6KawnV1aTdDX26JNGf/image.png)
(图源 :[pixabay](https://pixabay.com/))

就这样了,再多说就要暴露我其实啥也不懂的事实了,言多必失啊。

----
<center><strong>Vote For Me As Witness</strong>
https://steemit.com/~witnesses type in **`oflyhigh`** and click ***`VOTE`***
[![](https://cdn.steemitimages.com/DQmX5NysqT44FBa3bhuWqQ69nAbseu8Nt5YQPn2pYejPVxA/image.png)](https://steemit.com/~witnesses)
[Vote @oflyhigh via Steemconnect](https://steemconnect.com/sign/account-witness-vote?witness=oflyhigh&approve=1)
<strong>Thank you!</strong></center>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 94 others
properties (23)
authoroflyhigh
permlinkhash-hmac
categorycn
json_metadata{"tags":["cn","hash","hmac","cn-programming"],"image":["https://steemitimages.com/DQmRkLq6rRew3mHfx4vYGWyqpC8wSebLPeC2iZCXAdpuGkR/image.png","https://cdn.steemitimages.com/DQmT5UpzBvHpwci9Xxp3AYBVnn1gp6KawnV1aTdDX26JNGf/image.png","https://cdn.steemitimages.com/DQmX5NysqT44FBa3bhuWqQ69nAbseu8Nt5YQPn2pYejPVxA/image.png"],"links":["https://pixabay.com/","https://baike.baidu.com/item/Hash/390310","https://en.wikipedia.org/wiki/HMAC,总之我是看不懂啦。","https://steemit.com/~witnesses","https://steemconnect.com/sign/account-witness-vote?witness=oflyhigh&approve=1"],"app":"steemit/0.1","format":"markdown"}
created2018-12-16 10:22:33
last_update2018-12-16 10:22:33
depth0
children1
last_payout2018-12-23 10:22:33
cashout_time1969-12-31 23:59:59
total_payout_value14.868 HBD
curator_payout_value4.521 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,368
author_reputation6,360,760,914,385,872
root_title每天进步一点点:HASH以及HMAC
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd0
post_id76,913,796
net_rshares33,255,122,786,873
author_curate_reward""
vote details (158)
@partiko ·
Thank you so much for participating the Partiko Delegation Plan Round 1! We really appreciate your support! As part of the delegation benefits, we just gave you a 3.00% upvote! Together, let’s change the world!
properties (22)
authorpartiko
permlinkre-hash-hmac-20181216t103007
categorycn
json_metadata""
created2018-12-16 10:30:09
last_update2018-12-16 10:30:09
depth1
children0
last_payout2018-12-23 10:30:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length210
author_reputation39,207,160,334,751
root_title每天进步一点点:HASH以及HMAC
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id76,913,966
net_rshares0