• 20-hashlib 模块


    一、什么是哈希hash

       hash一类算法,该算法接受传入的内容,经过运算得到一串hash值
    hash值的特点:
    I 只要传入的内容一样,得到的hash值必然一样
    II 不能由hash值返解成内容
    III 不管传入的内容有多大,只要使用的hash算法不变,得到的hash值长度是一定

    二、hash的用途

    用途1:特点II用于密码密文传输与验证

    用途2:特点I、III用于文件完整性校验

    三、hash的应用

    3.1 hash的使用

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

    import hashlib
    
    m=hashlib.md5()
    m.update('hello'.encode('utf-8'))
    m.update('world'.encode('utf-8'))
    res=m.hexdigest() # 'helloworld'
    print(res)
    
    m1=hashlib.md5('he'.encode('utf-8'))
    m1.update('llo'.encode('utf-8'))
    m1.update('w'.encode('utf-8'))
    m1.update('orld'.encode('utf-8'))
    res=m1.hexdigest()# 'helloworld'
    print(res)

    3.2 hash的应用(密文、大文件校验)

    1、密文

    # 模拟撞库
    cryptograph='aee949757a2e698417463d47acac93df'
    import hashlib
    
    # 制作密码字段
    passwds=[
        'alex3714',
        'alex1313',
        'alex94139413',
        'alex123456',
        '123456alex',
        'a123lex',
    ]
    
    dic={}
    for p in passwds:
        res=hashlib.md5(p.encode('utf-8'))
        dic[p]=res.hexdigest()
    
    # 模拟撞库得到密码
    for k,v in dic.items():
        if v == cryptograph:
            print('撞库成功,明文密码是:%s' %k)
            break
    
    
    # 提升撞库的成本=>密码加盐
    import hashlib
    
    m=hashlib.md5()
    
    m.update('天王'.encode('utf-8'))
    m.update('alex3714'.encode('utf-8'))
    m.update('盖地虎'.encode('utf-8'))
    print(m.hexdigest())

    2、大文件校验

    # 验证文件完整性,传入文件大小,在八处取数据进行md5哈希计算,得到哈希值。
    def check(size):
        with open("test.ini",mode="rb") as f:
            m = hashlib.md5()
            content = f.readline()
            point = 0
            while point < size:
                m.update(content)
                point += size//8
                f.seek(point,0)
        code = m.hexdigest()
        return code
    
    print(check(864))
  • 相关阅读:
    如何利用docker制作自己的镜像
    python 获取当前运行的 class 和 方法的名字
    UnitTest-case参数化
    值得推崇的一种图书出版的方式----开源图书
    Django中对数据查询、删除、修改
    wireshark网络实战分析二
    wireshark网络抓包分析
    邮件传输协议实战抓包分析
    socket服务端给客户端发送数据第一次不成功,第二次成功
    转载:Wireshark抓包详解
  • 原文地址:https://www.cnblogs.com/zhubincheng/p/12609523.html
Copyright © 2020-2023  润新知