• hashlib模块(二十八)


    # 1、什么叫hash:hash是一种算法(3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法),该算法接受传入的内容,经过运算得到一串hash值
    # 2、hash值的特点是:
    #2.1 只要传入的内容一样,得到的hash值必然一样=====>要用明文传输密码文件完整性校验
    #2.2 不能由hash值返解成内容=======》把密码做成hash值,不应该在网络传输明文密码
    #2.3 只要使用的hash算法不变,无论校验的内容有多大,得到的hash值长度是固定的

     hash算法就像一座工厂,工厂接收你送来的原材料(可以用m.update()为工厂运送原材料),经过加工返回的产品就是hash值

    import hashlib
    
    m = hashlib.md5()
    
    m.update("helloworld".encode("utf-8"))
    print(m.hexdigest()) # fc5e038d38a57032085441e7fe7010b0
    
    m.update("abcd".encode("utf-8"))
    print(m.hexdigest()) # 2e6355e9851fc9185320bea99a04338c
    
    m2 = hashlib.md5()
    m2.update("helloworldabcd".encode("utf-8"))
    print(m2.hexdigest()) # 2e6355e9851fc9185320bea99a04338c
    
    '''
    注意:把一段很长的数据update多次,与一次update这段长数据,得到的结果一样
    但是update多次为校验大文件提供了可能
    '''

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

    import hashlib
    
    m = hashlib.md5(bytes('8765m',encoding="utf-8"))
    m.update(bytes('helloworld',encoding="utf-8"))
    print(m.hexdigest())
    import hashlib
    
    passwds = ['hello123','hello234','hello8789',
              '123456hello','hello123456','he123llo']
    
    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(crytograph,passwd_dic):
        for k,v in passwd_dic.items():
            if v == crytograph:
                print("密码是==》33[46m%s33[0m" %k)
    
    crytograph = '04522abf42bb8ad979fe66948402bc0d'
    break_code(crytograph,make_passwd_dic(passwds)) # 密码是==》123456hello
  • 相关阅读:
    【bzoj3083】遥远的国度 树链剖分+线段树
    【bzoj2226】[Spoj 5971] LCMSum 欧拉函数
    xml、json的序列化与反序列化
    什么是安全证书,访问者到底是怎么校验安全证书的,服务端返回安全证书后,客户端再向谁验证呢?
    查看发票组代码后的总结和有感
    网址的正则表达式、常用正则表达式、在线正则表达式检测
    XamlParseException异常
    委托,lambda,匿名方法
    windows中断与共享的连接(samba)
    linux ubuntu 11.04 samba 服务器设置
  • 原文地址:https://www.cnblogs.com/xiangtingshen/p/10447770.html
Copyright © 2020-2023  润新知