+

SHA in Python

# Python 3 code to check
# available algorithms

 

import hashlib

 
# prints all available algorithms

print ( " The available algorithms are: " , end = "")

print (hashlib.algorithms_guaranteed)

Output:

 The available algorithms are: {`sha256`,` sha384`, `sha224`,` sha512`, `sha1`,` md5`} 

First, let`s first discuss the functions that which will be used in this article.

Related Functions:

  • encode (): converts a string to bytes, so that it is acceptable for a hash function.
  • hexdigest (): returns encoded data in hexadecimal.

SHA Hash

The various SHA hash functions are described below.

  • SHA256: This hash function belongs to the SHA-2 hash class with an inner block size of 32 bits.
  • SHA384: This hash function belongs to the SHA-2 hash class, which has an inner block size of 32 bits. This is one of the truncated versions.
  • SHA224: This hash function belongs to the SHA-2 hash class, whose internal block size is 32 bits. This is one of the truncated versions.
  • SHA512: This hash function belongs to the SHA-2 hash class, which has an internal block size of 64 bits.
  • SHA1: A 160-bit hash function that resembles an MD5 hash function and has been discontinued due to security vulnerabilities.

The code below implements these hash functions.

# Python 3 demo code
# SHA hashing algorithms.

 

import hashlib

 
# initializing string

str = "GeeksforGeeks"

  
# encode Python.Engineering using encode ()
# then send in SHA256 ( )

result = hashlib.sha256 ( str . encode ())

 
# Prints the equivalent hexadecimal value.

print ( "The hexadecimal equivalent of SHA256 is:" )

print (result.hexdigest ())

 

print ( "" )

 
# initialization string

str = "GeeksforGeeks"

 
# encode Python.Engineering using encode ()
# then send to SHA384 ()

result = hashlib.sha384 ( str . encode ())

 
# Prints the equivalent hexadecimal value.

print ( "The hexadecimal equivalent of SHA384 is:" )

print (result.hexdigest ( ))

 

print ( "" )

 
# initializing string

str = "GeeksforGeeks"

 
# Encode Python.Engineering using encode ()
# then send to SHA224 ()

result = hashlib.sha224 ( str . encode ())

 
# Prints the equivalent hexadecimal value.

print ( "The hexadecimal equivalent of SHA224 is: " )

print (result.hexdigest ())

 

print ( "" )

 
# initialization string

str = "GeeksforGeeks"

 
# encode Python.Engineering using encode ()
# then submit to SHA512 ()

result = hashlib.sha512 ( str . encode ())

  
# Prints the equivalent hexadecimal value.

print ( "The hexadecimal equivalent of SHA512 is:" )

print (result.hexdigest ())

 

print ( "" )

 
# initialization string

str = "GeeksforGeeks"

  
# encode Python.Engineering using encode ()
# then send in SHA1 ( )

result  = hashlib.sha1 ( str . encode ())

 
# Prints the equivalent hexadecimal value.

print ( "The hexadecimal equivalent of SHA1 is:" )

print (result.hexdigest ())

Output:

 The hexadecimal equivalent of SHA256 is: f6071725e7ddeb434fb6b32b8ec4a2b14dd7db0d785347b2fb48f9975126 The hexadecimal equivalent of SHA384 is: d1e67b8819b009ec7929933b6fc1928dd64b5df31bcde6381b9d3f90488d253240490460c0a5a1a873da8236c12ef9b3 The hexadecimal equivalent of SHA224 is: 173994f309f727ca939bb185086cd7b36e66141c9e52ba0bdcfd145d The hexadecimal equivalent of SHA512 is: 0d8fb9370a5bf7b892be4865cdf8b65 8a82209624e33ed71cae353b0df254a75db63d1baa35ad99f26f1b399c31f3c666a7fc67ecef3bdcdb7d60e8ada90b722 The hexadecimal equivalent of SHA1 is: 4175a37afd561152fb60c305d4fa6026b7e79856  

 Cause: The above code takes a string and converts it into a byte equivalent using the encode (), so it can be adopted hash function. The SHA hash functions encode it and then use hexdigest () to output the hexadecimal equivalent encoded string.

Get Solution for free from DataCamp guru