zoukankan      html  css  js  c++  java
  • Python标准库之hashlib模块与hmac模块

    hashlib模块用于加密相关的操作。在Python 3.x里代替了md5模块和sha模块,主要提供 SHA1、SHA224、SHA256、SHA384、SHA512 、MD5 算法。如果包含中文字符的话,给字符加上编码.encode('utf-8')

    md5加密

    import hashlib
    
    h = hashlib.md5()
    h.update(b'123456')
    print(h.hexdigest())
    #输出
    e10adc3949ba59abbe56e057f20f883e
    

      

    注意:update是更新的意思,都存储在h对象里面。

    import hashlib
    
    h = hashlib.md5()
    h.update(b'123')
    print(h.hexdigest()) #md5加密123
    h.update(b'456')
    print(h.hexdigest())  #MD5加密123456
    #输出
    202cb962ac59075b964b07152d234b70
    e10adc3949ba59abbe56e057f20f883e
    

      

    sha1

    sha1加密不安全,需要注意。

    h = hashlib.sha1()
    h.update(b'123456')
    print(h.hexdigest())
    #输出
    7c4a8d09ca3762af61e59520943dc26494f8941b
    

      

    sha256

    h = hashlib.sha256()
    h.update(b'123456')
    print(h.hexdigest())
    #输出
    8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92
    

      

    hmac模块

    import hmac
    
    h = hmac.new(b"user","测试账号".encode('utf-8'))
    print(h.digest())
    print(h.hexdigest())

      

  • 相关阅读:
    C# 文件类的操作---删除
    C#实现Zip压缩解压实例
    UVALIVE 2431 Binary Stirling Numbers
    UVA 10570 meeting with aliens
    UVA 306 Cipher
    UVA 10994 Simple Addition
    UVA 696 How Many Knights
    UVA 10205 Stack 'em Up
    UVA 11125 Arrange Some Marbles
    UVA 10912 Simple Minded Hashing
  • 原文地址:https://www.cnblogs.com/endust/p/12312549.html
Copyright © 2011-2022 走看看