069 hmac 模块

hmac模块

  • hmac模块:对密码加密,可以加盐

    为了防止密码被撞库,我们可以使用python中的另一个hmac 模块,它内部对我们创建key和内容做过某种处理后再加密。

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

    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

猜你喜欢

转载自www.cnblogs.com/xichenHome/p/11366357.html
069