The use of hashlib and hmac encryption module in python

      

hashlib

Used for encryption-related operations, instead of the md5 module and the sha module, mainly providing SHA1, SHA224, SHA256, SHA384, SHA512, MD5 algorithms

#!/usr/bin/env python
# _*_ coding:utf-8 _*_
__author__ = 'junxi'

import hashlib

# ######## md5 ########
hash = hashlib.md5()
# help(hash.update)
hash.update('admin'.encode('utf-8'))
print(hash.hexdigest())
print(hash.digest())


# ######## sha1 ########
hash = hashlib.sha1()
hash.update('admin'.encode('utf-8'))
print(hash.hexdigest())


# ######## sha256 ########
hash = hashlib.sha256()
hash.update('admin'.encode('utf-8'))
print(hash.hexdigest())


# ######## sha384 ########
hash = hashlib.sha384()
hash.update('admin'.encode('utf-8'))
print(hash.hexdigest())


# ######## sha512 ########
hash = hashlib.sha512()
hash.update('admin'.encode('utf-8'))
print(hash.hexdigest())

Although the above encryption algorithms are still very powerful, there are sometimes defects, that is, the solution can be reversed through credential stuffing. Therefore, it is necessary to add a custom key to the encryption algorithm before encrypting.

import hashlib

# ######## md5 ########
hash = hashlib.md5('898oaFs09f'.encode("utf-8"))
hash.update('admin'.encode("utf-8"))
print(hash.hexdigest())

There is also a built-in hmac module in python, which internally performs further processing on the key and content we create and then encrypts it

import hmac

h = hmac.new('898oaFs09f'.encode("utf-8"))
h.update('admin'.encode("utf-8"))
print(h.hexdigest())

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326871347&siteId=291194637