Quantcast
Channel: JS Tricks » Python
Viewing all articles
Browse latest Browse all 25

Python SHA Hash: Python Tutorial

$
0
0

The Secure Hash Algorithm ( SHA ) is a family of cryptographic hash functions. In this lesson we will learn about  Python SHA Hash.

Python SHA Hash

The hashlib module which is included in Python’s Standard library contains an some of the most popular hashing algorithms. The algorithms_available method will list all of the algorithms available in the system, including the ones which are available trough OpenSSL. So, in this case you may see duplicate names in the list. The algorithms_guaranteed method will list the algorithms which are present in the module. hashlib contains sha1,  sha224, sha256, sha384, sha512 these methods for SHA family.

Example of Python SHA Hash

SHA1

import hashlib
mystring = "Any string you want"
object = hashlib.sha1( mystring )
print( object.hexdigest() )

When you run this script you will see an output like this:

467c58340a2510deaf60d3b236be54d45cff2a02

 

SHA224

import hashlib
mystring = "Any string you want"
object = hashlib.sha224( mystring )
print( object.hexdigest() )

The output will look like this:

2dadd0eb3342acbc7aed10d2c2d489b69f5e2b1c7fa4c2511f21f06a

 

SHA256

import hashlib
mystring = "Any string you want"
object = hashlib.sha256( mystring )
print( object.hexdigest() )

The output will look like this:

cb5dcdbd102df950ca3817a87ad6a1e10ec4a11a676fb24c9c7b57081283077d

 

SHA384

import hashlib
mystring = "Any string you want"
object = hashlib.sha384( mystring )
print( object.hexdigest() )

The output will look like this:

78f8a9ba5f30ca3e5688b29402dd16a943577141bcbacff4b48e5563cb58df93b0d66e5ccd14973b8348e1d3f740f516

 

SHA512

import hashlib
mystring = "Any string you want"
object = hashlib.sha512( mystring )
print( object.hexdigest() )

The output will look like this:

c9483c55361cb37821bac8f8c0e762e3f5a77e58c0c3b6ffb5d9c123207600ae002f175077e39831c0459ad69c64e8ab8ed1830a2a264e32f050269fc2223090

 

These methods for Hashing will help you to protect sensitive informations ( ex. Password ). Hope you will play with Python’s hashlib more.

The post Python SHA Hash: Python Tutorial appeared first on JS Tricks.


Viewing all articles
Browse latest Browse all 25

Trending Articles