zoukankan      html  css  js  c++  java
  • python_day6 hashlib模块

    hash:一种算法 ,3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法
    三个特点:
    1.内容相同则hash运算结果相同,内容稍微改变则hash值则变
    2.不可逆推
    3.相同算法:无论校验多长的数据,得到的哈希值长度固定。

    import hashlib

    m=hashlib.md5()# m=hashlib.sha256()

    m.update('hello'.encode('utf8'))
    print(m.hexdigest()) #5d41402abc4b2a76b9719d911017c592

    m.update('alvin'.encode('utf8'))

    print(m.hexdigest()) #92a7e713c30abbb0319fa07da2a5c4af

    m2=hashlib.md5()
    m2.update('helloalvin'.encode('utf8'))
    print(m2.hexdigest()) #92a7e713c30abbb0319fa07da2a5c4af

    '''
    注意:把一段很长的数据update多次,与一次update这段长数据,得到的结果一样
    但是update多次为校验大文件提供了可能。
    '''

    以上加密算法虽然依然非常厉害,但时候存在缺陷,即:通过撞库可以反解。所以,有必要对加密算法中添加自定义key再来做加密。

    import hashlib

    # ######## 256 ########

    hash = hashlib.sha256('898oaFs09f'.encode('utf8'))
    hash.update('alvin'.encode('utf8'))
    print (hash.hexdigest())#e79e68f070cdedcfe63eaf1a2e92c83b4cfb1b5c6bc452d214c1b7e77cdfd1c7

    ########撞库破解密码

    import hashlib
    passwds=[
    'alex3714',
    'alex1313',
    'alex94139413',
    'alex123456',
    '123456alex',
    'a123lex',
    ]
    def make_passwd_dic(passwds):
    dic={}
    for passwd in passwds:
    m=hashlib.md5()
    m.update(passwd.encode('utf-8'))
    dic[passwd]=m.hexdigest()
    return dic

    def break_code(cryptograph,passwd_dic):
    for k,v in passwd_dic.items():
    if v == cryptograph:
    print('密码是===>33[46m%s33[0m' %k)

    cryptograph='aee949757a2e698417463d47acac93df'
    break_code(cryptograph,make_passwd_dic(passwds))

    模拟撞库破解密码

    python 还有一个 hmac 模块,它内部对我们创建 key 和 内容 进行进一步的处理然后再加密:

    1 import hmac
    2 h = hmac.new('alvin'.encode('utf8'))
    3 h.update('hello'.encode('utf8'))
    4 print (h.hexdigest())#320df9832eab4c038b6c1d7ed73a5940
  • 相关阅读:
    linux驱动程序设计的硬件基础,王明学learn
    linux设备驱动概述,王明学learn
    应用程序调试工具gdb,王明学learn
    usb设备驱动描述,王明学learn
    OK6410移植madplay播放器,王明学learn
    bootstrap使用入门(bootstrap4.2.1版本)
    IntelliJ Idea 常用快捷键列表
    javaFX 多窗口编程
    Spring Boot框架入门教程(快速学习版)
    BindingNavigator 控件
  • 原文地址:https://www.cnblogs.com/onda/p/6950468.html
Copyright © 2011-2022 走看看