for cryptographic purposes.
SHA-1
SHA-1 is a cryptographic hash function that takes a message of arbitrary length as input and generates a hash value that is 160-bit in size. Using the hashlib module in Python, we can generate the SHA-1 hash of a message in the following way:
import hashlib # Calculate SHA1 hash message = "Hello".encode() message_hash = hashlib.sha1(message).digest() print(message_hash) message_hash = hashlib.sha1(message).hexdigest() print(message_hash)
hashlib.sha1() also takes bytes as input and generates the output in bytes if digest() is used. Otherwise, we can get hexadecimal digest using the hexdigest() function.
SHA-256
SHA-256 is a cryptographic hash function that takes a message of any length as input and generates a hash value that is 256-bit in size. Using the hashlib module in Python, we can compute the SHA-256 hash value of a message in the following way:
import hashlib # Calculate SHA256 hash message = "Hello".encode() message_hash = hashlib.sha256(message).digest() print(message_hash) message_hash = hashlib.sha256(message).hexdigest() print(message_hash)
SHA-512
SHA-512 is a cryptographic hash function that takes a message of arbitrary length as input and generates a hash value that is 512-bit in size. We can use the hashlib module in Python to generate the SHA-512 hash of a message in the following way:
# Calculate SHA512 hash message = "Hello".encode() message_hash = hashlib.sha512(message).digest() print(message_hash) message_hash = hashlib.sha512(message).hexdigest() print(message_hash)
I hope this helps. However, readers who want to know more about how different cryptographic algorithms work and how they are used in various secure network protocols can refer to the book “Cryptography And Public Key Infrastructure.”






0 Comments