In this article we will see how to encode a string in MD5 with Python.
We can take advantage of the methods and features offered by the hashlib
core module.
import hashlib
def md5_encode_string(string_to_encode=None):
if string_to_encode is None:
return None
return hashlib.md5(string_to_encode.encode()).hexdigest()
The bytes obtained from the input string with the encode()
method must be passed to the md5()
constructor. So to get the final string in hexadecimal format we need to invoke the hexdigest()
method.