zoukankan      html  css  js  c++  java
  • hashlib 文件校验,MD5动态加盐返回加密后字符

    hashlib 文件校验

    # for循环校验
    import hashlib
    def check_md5(file):
        ret = hashlib.md5()
        with open(file, mode='rb') as f1:
            for line in f1:
                ret.update(line)
            return ret.hexdigest()
    
    
    print(check_md5('rock1'))
    print(check_md5('rock'))
    print(check_md5('rock1') == check_md5('rock'))
    
    # while循环每次读取1024字节校验
    
    import hashlib
    
    
    def check_md5(file):
        ret = hashlib.md5()
        with open(file, mode='rb') as f1:
            while 1:
                content = f1.read(1024)
                if content:
                    ret.update(content)
                else:
                    break
            return ret.hexdigest()
    
    
    print(check_md5('rock1'))
    print(check_md5('rock'))
    print(check_md5('rock1') == check_md5('rock'))

    用户名动态加盐校验

    import hashlib
    
    
    def check_md5(s=''):
        # ret = hashlib.md5('w'.encode('utf-8'))
        # print(s[::-2])
        ret = hashlib.md5()
        ret.update(s[::-2].encode('utf-8'))
        ret.update(s.encode('utf-8'))
        return ret.hexdigest()
    
    
    print(check_md5('146123'))
    print(check_md5('146123'))
    print(check_md5('rock'))
    import hashlib
    
    
    def check_md5(s=''):
        # ret = hashlib.md5('w'.encode('utf-8'))
        # print(s[::-2])
        ret = hashlib.md5(s[::-2].encode('utf-8'))
        # ret = hashlib.md5()
        # ret.update(s[::-2].encode('utf-8'))
        ret.update(s.encode('utf-8'))
        return ret.hexdigest()
    
    
    # print(check_md5('146123'))
    # print(check_md5('146123'))
    print(check_md5('rock') == '3169b9c85d4ae320c1dcb79043fe6f02')    # 3169b9c85d4ae320c1dcb79043fe6f02
  • 相关阅读:
    Python入门:局部变量与全局变量2
    Python入门:局部变量与全局变量1
    Python入门:函数参数1
    Python入门:文件操作1
    Python入门:集合操作
    Python入门:用字典实现三级菜单
    Python入门:购物车实例
    Python:循环
    git 提交指定提交时用户名
    mysql 判断null 和 空字符串
  • 原文地址:https://www.cnblogs.com/chen55555/p/10246688.html
Copyright © 2011-2022 走看看