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

    hashlib模块

    hashlib是一个加密模块,内置了很多算法

    ​ -MD5 :不可解密的算法(2018之前)

    ​ -sha_256(了解)

    摘要算法:

    ​ 摘要是从某个内容中获取的加密字符串,摘要一样,内容就一样:保证唯一性

    ​ -密文密码就是一个摘要

    import hashlib
    
    md5_obj = hashlib.md5()
    print(type(md5_obj))
    >> <class '_hashlib.HASH'>
    str1 = '1234'
    #update 中一定要传入bytes类型数据
    md5_obj.update(str1.encode('utf-8'))
    
    #得到一个加密后的字符串
    res = md5_obj.hexdigest()
    print(res)
    >>81dc9bdb52d04dc20036dbd8313ed055
    
    
    #以上操作撞库有可能会被破解密码  
    #防止撞库 :加盐
    import hashlib
    
    
    def pwd_md5(pwd)
    	md5_obj = hashlib.md5()
        str1 = pwd
        md5_obj.update(str.encode('utf-8'))
        #创造盐
        sal = '尼玛嗨尼玛嗨'
        #加盐
        md5_obj.update(sal.encode('utf-8'))
        
        res = md5_obj.hexdigest()
        return res
    
    password = pwd_md5(123)
    print(password)
    >>>4100f0a6cc2b178ab3a0ffca546e76b8
    
    #储存用户加密密码
    user_str1 = f'lzn:123'
    user_str2 = f'lzn:{password}'
    with open('nmb.txt','w',encoding = 'utf-8')as f:
        f.write(user_str2)
    
    ###模拟用户登录操作
    import hashlib
    #加密函数
    def pwd_md5(pwd):
    	md5_obj = hashlib.md5()
        str1 = pwd
        md5_obj.update(str1.encode('utf-8'))
        #创造盐
        sal = '尼玛嗨尼玛嗨'
        #加盐
        md5_obj.update(sal.encode('utf-8'))
        
        res = md5_obj.hexdigest()
        return res
    
    
    with open('nmb.txt','r',encoding='utf-8')as f:
        user_str = f.read()
    
    file_user,file_pwd = user_str.strip().split(':')
    
    #用户输入用户名密码
    username = input('input your username:').strip()
    password = input('input your password:').strip()
    
    #校验用户名与密码是否一致
    if username == file_user and file_pwd == pwd_md5(password):
        print('success')
    else:
    	print('fail')
    
  • 相关阅读:
    制作keil5的pack
    【转】链接脚本(1)
    mongodb数据到MySQL数据库 的迁移步骤
    mongo副本集设置主库权重,永远为主
    mongodb副本集的从库永久性设置setSlaveOk
    Ubuntu系统查看mongo得慢日志,及一些操作
    Ubuntu系统下手动释放内存
    linux下面得小数计算
    Syncthing搭建
    ubuntu搭建ftp服务器
  • 原文地址:https://www.cnblogs.com/littleb/p/11892262.html
Copyright © 2011-2022 走看看