069 hmac 模块

hmac模块

  • hmac模块:对密码加密,能够加盐python

    为了防止密码被撞库,咱们能够使用python中的另外一个hmac 模块,它内部对咱们建立key和内容作过某种处理后再加密。加密

    若是要保证hmac模块最终结果一致,必须保证:code

    1. hmac.new括号内指定的初始key同样
    2. 不管update多少次,校验的内容累加到一块儿是同样的内容
    import hmac
    
    # 注意hmac模块只接受二进制数据的加密
    h1 = hmac.new(b'hash')
    h1.update(b'hello')
    h1.update(b'world')
    print(h1.hexdigest())
    
    # 905f549c5722b5850d602862c34a763e
    h2 = hmac.new(b'hash')
    h2.update(b'helloworld')
    print(h2.hexdigest())
    
    # 905f549c5722b5850d602862c34a763e
    h3 = hmac.new(b'hashhelloworld')
    print(h3.hexdigest())
    
    # a7e524ade8ac5f7f33f3a39a8f63fd25
相关文章
相关标签/搜索