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

        hashlib模块

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

        __always_supported = ('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512')

    import hashlib
    m = hashlib.md5()
    m.update(b"Hello")
    m.update(b"It's me")
    print(m.digest())
    m.update(b"It's been a long time since last time we ...")
    print(m.digest())    #二进制格式hash
    print(m.hexdigest())   #16进制格式hash
    '''
    def digest(self, *args, **kwargs): # real signature unknown
        """ Return the digest value as a string of binary data. """
        pass
    
    def hexdigest(self, *args, **kwargs): # real signature unknown
        """ Return the digest value as a string of hexadecimal digits. """
        pass
    
    '''
    
    hash = hashlib.sha512()
    hash.update(b'admin')
    print(hash.hexdigest())

        上面代码过程就是加密的过程,我们可以使用很多方法进行加密。首先确定使用那种方法进行加密,使用update()加密。

        还不够吊?python 还有一个hmac模块,它内部对我们创建 key和内容再进行处理然后再加密

        散列消息鉴别码,简称HMAC,是一种基于消息鉴别码MAC(Message Authentication Code)的鉴别机制。使用HMAC时,消息通讯的双方,通过验证消息中加入的鉴别密钥K来鉴别消息的真伪;

        一般用于网络通信中消息加密,前提是双方先要约定好key,就像接头暗号一样,然后消息发送把用key把消息加密,接收方用key + 消息明文再加密,拿加密后的值 跟 发送者的相对比是否相等,这样就能验证消息的真实性,及发送者的合法性了。

        >>> import hmac
      >>> h = hmac.new(b"pipei",b"alex")
      >>> h.hexdigest()
      '3c7ffaf55fc7a53f678debb2adca855f'
      >>> h = hmac.new(b"pipei")
      >>> h.update(b"alex")
      >>> h.hexdigest()
      '3c7ffaf55fc7a53f678debb2adca855f'

  • 相关阅读:
    [Javascript] Avoid Creating floats if they are not needed
    App_Offline.htm and working around the "IE Friendly Errors" feature
    uninstall vs extension
    Sample page with dom-if and dom-bind in polymer
    4 classes in Bootstrap Grid system
    Suppress user properties/ custom fields when print in Outlook
    Remove Event Handler via Reflection
    Get the type name of a com object
    Row_Number 生成样本数据
    java运行命令行或脚本
  • 原文地址:https://www.cnblogs.com/gengcx/p/6919173.html
Copyright © 2011-2022 走看看