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

    • hashlib模块是一个提供摘要算法的模块
    • 对相同的字符串使用同一个算法进行摘要,所得到的值总是不变的
    • 使用不同的算法进行摘要,得到的值应该是不同的
    • 一般用md5,密码的密文存储、文件的一致性检验
    • 为防止撞库 ,对md5加盐
    #静态加盐
    import hashlib
    md5 = hashlib.md5()
    md5 = hashlib.md5(bytes("slat",encoding="utf-8")) #加盐后的写法 “slat” 为随意字符串
    md5.update(b"abc123")
    print(md5.hexdigest())
    

    动态加盐

    #未完成
    s1 = ""
    def file1_md5():
        with open("test.bak","r",encoding="utf-8") as fr:
            for line in fr.read(1024):
                md5 = hashlib.md5()
                md5.update(line.encode("utf-8"))
                yield md5.hexdigest()
    g1 = file1_md5()
    for i in g1:
        s1 = s1 + i
    print(s1)
    s2 = ""
    def file2_md5():
        with open("test.bak","rb") as fr:
            for line in fr:
                line = line.strip()
                md5 = hashlib.md5()
                md5.update(line)
                yield md5.hexdigest()
    g2 = file2_md5()
    for i in g2:
        s2 = s2 + i
    print(s2)
    print(s1 == s2)
    
  • 相关阅读:
    Bzoj3339 Rmq Problem
    Bzoj3509 [CodeChef] COUNTARI
    浅析python日志重复输出问题
    mysql练习题
    python学习之思维导图
    python面向对象编程练习
    Python常见下划线
    内置方法
    类的绑定方法与非绑定方法
    封装
  • 原文地址:https://www.cnblogs.com/meilong/p/Python-hashlib-mo-kuai.html
Copyright © 2011-2022 走看看