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

    hashlib

    用于加密相关的操作,代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法

     

    import hashlib

    update()方法用于对字符串"abc"做MD5加密
    hexdigest()方法用于查看hash值
     
    # 生成加密对象
    h = hashlib.md5()
    
    h.update(b"byby")
    print(h.hexdigest()) # 16进制格式hash
    h.update(b"toto")
    print(h.hexdigest())
    python2.7里面 update 传的是字符串可以

    python3 一定穿的是是字节bytes类型 才可以

    import hashlib
    
    h = hashlib.md5()
    h.update("byby")
    print(h.hexdigest()) # 16进制格式hash
    
    
    '''
    Traceback (most recent call last):
      File "C:/Users/Administrator/PycharmProjects/ss/SSS/day5/hashlib 模块/hashlib mod 3.6 传的是字节.py", line 9, in <module>
        h.update("byby")
    TypeError: Unicode-objects must be encoded before hashing
    
    '''
    
    

    在字符串 前面加上 b 

    import hashlib
    
    h = hashlib.md5()
    h.update(b"byby")
    print(h.hexdigest()) # 16进制格式hash
    
    
    # 01130a5b7b2173871250ca9227d1965c

    或者用encode

    
    
    import hashlib
    
    h = hashlib.md5()
    h.update("byby".encode("utf-8"))
    print(h.hexdigest()) # 16进制格式hash
    
    
    # 01130a5b7b2173871250ca9227d1965c
    
    
    
    还有bytes("字符串", encoding="utf-8")
    import hashlib
    
    h = hashlib.md5()
    h.update(bytes("byby",encoding="utf-8"))
    print(h.hexdigest()) # 16进制格式hash
    
    
    # 01130a5b7b2173871250ca9227d1965c
    
    
    







  • 相关阅读:
    冲刺第一天(补发)
    进度条05
    npm start问题
    Spring Boot 默认配置无法访问静态资源
    Spring Boot 返回Html界面
    阿里云配置tomcat后不能访问问题
    Spring Boot Web开发中Thymeleaf模板引擎的使用
    tomcat官网改版后下载方式
    Ubuntu16.04进入无限登录状态的解决办法
    Ubuntu16.04安装MySql5.7
  • 原文地址:https://www.cnblogs.com/mingerlcm/p/8037794.html
Copyright © 2011-2022 走看看