Explaining Bytes Objects within the Context of HashLib Library in Python
hashlib module in Python provides a common interface to many different secure hash and message digest algorithms.
- Included are the FIPS secure hash algorithms SHA1, SHA224, SHA256, SHA384, and SHA512 (defined in FIPS 180–2) as well as RSA’s MD5 algorithm (defined in internet RFC 1321).
- Here’s an example of how you can use the
sha256 method from the
message = 'Hello, World!'
hashed_message = hashlib.sha256(message.encode('utf-8')).hexdigest()
# Output: 7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9
- In this example, we first encode the message string into bytes using the
encode method with
- Then we pass the encoded message to the
sha256 method of the
- Finally, we use the
hexdigest method to get the hexadecimal representation of the hash.
Why are we using
hexdigest method is used to get a hexadecimal string representation of the binary data returned by the
- This can be useful for displaying the hash in a more human-readable format or for storing it in a database or file.
- Hexadecimal representation is more compact than binary representation and can be easily converted back to binary if needed.
b prefix in front of a string in Python signifies that it is a bytes string literal.
- You could also do just this, without the
hash_object = hashlib.sha256(b"firstname.lastname@example.org")
hex_dig = hash_object.hexdigest()
- In other words,
a = email@example.com' and
a = 'firstname.lastname@example.org'.encode('utf-8') are equivalent in Python
- Both create a bytes object with the same content.
- When you use a bytes literal with the
b prefix to create a bytes object, you don't need to specify an encoding because the bytes object is created directly from the literal.
- The characters in the bytes literal are interpreted as ASCII characters and are converted to their corresponding byte values.
- If you use special characters that are not part of the ASCII character set in a bytes literal, you will get a
- To create a bytes object containing special characters that are not part of the ASCII character set, you can use the
encode() method of a string and specify an encoding that supports the special characters:
a = 'email@example.com£'.encode('utf-8')
File "<stdin>", line 1
SyntaxError: bytes can only contain ASCII literal characters.