当前位置:首页 > 操作系统 > MacOs

hashlib和hmac模块

一、hashlib模块

1.0.1 hash是什么

hash是一种算法(Python3.版本里使用hashlib模块代替了md5模块和sha模块,主要提供 SHA1、SHA224、SHA256、SHA384、SHA512、MD5 算法),该算法接受传入的内容,经过运算得到一串hash值。

hash值的特点:

  1. 只要传入的内容一样,得到的hash值一样,可用于非明文密码传输时密码校验
  2. 不能由hash值返解成内容,即可以保证非明文密码的安全性
  3. 只要使用的hash算法不变,无论校验的内容有多大,得到的hash值长度是固定的,可以用于对文本的哈希处理

hash算法其实可以看成如下图所示的一座工厂,工厂接收你送来的原材料,经过加工返回的产品就是hash值

hashlib和hmac模块 - 文章图片

<code>1<br/></code>
<code>import hashlib<br/></code>
<code>1<br/>2<br/>3<br/>4<br/></code>
<code>m = hashlib.md5()<br/><br/>m.update('hello'.encode('utf8'))<br/>print(m.hexdigest())<br/></code>
<code>1<br/></code>
<code>5d41402abc4b2a76b9719d911017c592<br/></code>
<code>1<br/>2<br/></code>
<code>m.update('hash'.encode('utf8'))<br/>print(m.hexdigest())<br/></code>
<code>1<br/></code>
<code>97fa850988687b8ceb12d773347f7712<br/></code>
<code>1<br/>2<br/>3<br/></code>
<code>m2 = hashlib.md5()<br/>m2.update('hellohash'.encode('utf8'))<br/>print(m2.hexdigest())<br/></code>
<code>1<br/></code>
<code>97fa850988687b8ceb12d773347f7712<br/></code>
<code>1<br/>2<br/>3<br/></code>
<code>m3 = hashlib.md5()<br/>m3.update('hello'.encode('utf8'))<br/>print(m3.hexdigest())<br/></code>
<code>1<br/></code>
<code>5d41402abc4b2a76b9719d911017c592<br/></code>

1.0.2 撞库破解hash算法加密

hash加密算法虽然看起来很厉害,但是他是存在一定缺陷的,即可以通过撞库可以反解,如下代码所示。

<code>1<br/>2<br/>3<br/>4<br/>5<br/>6<br/>7<br/>8<br/>9<br/>10<br/>11<br/>12<br/>13<br/>14<br/>15<br/>16<br/>17<br/>18<br/>19<br/>20<br/>21<br/>22<br/>23<br/>24<br/>25<br/>26<br/>27<br/>28<br/>29<br/>30<br/></code>
<code>import hashlib<br/><br/># 假定我们知道hash的微信会设置如下几个密码<br/>pwd_list = [<br/>    'hash3714',<br/>    'hash1313',<br/>    'hash94139413',<br/>    'hash123456',<br/>    '123456hash',<br/>    'h123ash',<br/>]<br/><br/><br/>def make_pwd_dic(pwd_list):<br/>    dic = {}<br/>    for pwd in pwd_list:<br/>        m = hashlib.md5()<br/>        m.update(pwd.encode('utf-8'))<br/>        dic[pwd] = m.hexdigest()<br/>    return dic<br/><br/><br/>def break_code(hash_pwd, pwd_dic):<br/>    for k, v in pwd_dic.items():<br/>        if v == hash_pwd:<br/>            print('hash的微信的密码是===>%s' % k)<br/><br/><br/>hash_pwd = '0562b36c3c5a3925dbe3c4d32a4f2ba2'<br/>break_code(hash_pwd, make_pwd_dic(pwd_list))<br/></code>
<code>1<br/></code>
<code>hash的微信的密码是===>hash123456<br/></code>

为了防止密码被撞库,我们可以使用python中的另一个hmac 模块,它内部对我们创建key和内容做过某种处理后再加密。

如果要保证hmac模块最终结果一致,必须保证:

  1. hmac.new括号内指定的初始key一样
  2. 无论update多少次,校验的内容累加到一起是一样的内容
<code>1<br/>2<br/>3<br/>4<br/>5<br/>6<br/>7<br/></code>
<code>import hmac<br/><br/># 注意hmac模块只接受二进制数据的加密<br/>h1 = hmac.new(b'hash')<br/>h1.update(b'hello')<br/>h1.update(b'world')<br/>print(h1.hexdigest())<br/></code>
<code>1<br/></code>
<code>905f549c5722b5850d602862c34a763e<br/></code>
<code>1<br/>2<br/>3<br/></code>
<code>h2 = hmac.new(b'hash')<br/>h2.update(b'helloworld')<br/>print(h2.hexdigest())<br/></code>
<code>1<br/></code>
<code>905f549c5722b5850d602862c34a763e<br/></code>
<code>1<br/>2<br/></code>
<code>h3 = hmac.new(b'hashhelloworld')<br/>print(h3.hexdigest())<br/></code>
<code>1<br/></code>
<code>a7e524ade8ac5f7f33f3a39a8f63fd25<br/></code>

【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐