Tuesday, December 18, 2018

Using hashlib in python

First install the module cryptography using PIP
C:\Users\username>pip install cryptography

Now you can verify by importing hashlib and check what all hashlib algorithms are available
>>> import hashlib
>>> hashlib.algorithms_available
{'blake2b', 'MD5', 'blake2s256', 'sha3_256', 'sha512', 'sha1', 'md4', 'blake2s', 'MDC2', 'sha224', 'sha3_384', 'ripemd160', 'sha384', 'SHA1', 'shake_256', 'MD5-SHA1', 'blake2b512', 'shake_128', 'sha3_512', 'md5', 'RIPEMD160', 'whirlpool', 'BLAKE2b512', 'md5-sha1', 'sha256', 'SHA512', 'SHA256', 'SHA384', 'BLAKE2s256', 'sha3_224', 'MD4', 'mdc2', 'SHA224'}

Let us use sha3_256() to encrypt a string

>>> h=hashlib.sha3_256()
>>> h.update(b"Hello")
>>> h.hexdigest()

And the output will be as follows :
'8ca66ee6b2fe4bb928a8e3cd2f508de4119c0895f22e011117e22cf9b13de7ef'

Let us check by a slight change in the string

>>> h.update(b"hello")
>>> h.hexdigest()
'e52212c71ea57784000b60cae4d0d6a8ab08e17ad72902525a2cbe7e87f77ab6'


No comments:

Post a Comment