Security is important part of the today IT and gains more respect from IT world. Hashing is a security measure to protect and check our data. In this tutorial we will look how to use hash with tables and strings.
What is Hash
Hash is a function where provided data will be converted into another expressions format and can not be recovered back with normal functions.
MD5 is very popular hashing algorithm created long time ago. It is very popular in IT world. But it have security issues which makes it unsecure for public usage.
SHA1 / SHA224 / SHA256 / SHA384 / SHA512
SHA is a hash algorithm family where different size of hashes can be created. We can create hash from 128 byte to 512 byte. SHA is recent and popular algorithm which can be used securely in our applications and system.
OpenSSL is popular library which provides cryptographic functions. We can use different encryption and hash algorithms. There is also OpenSSL library and module for Python too.
Hashlib is the builtin library provided by Python. This library mainly provides diffent type of hash libraries those we have explained previously. We can import
hashlib module like below.
Now we will look how to hash given value into an MD5 hash. We will use
md5() function which is provided by
hashlib. In this example we will hash the string
poftut.com. In order to create a hash we have to encode given string with
Print Hash Hexadecimal Format
We can print created hash in hexadecimal format. We just need to use
hexdigest() function after hash function.
We can also create SHA256 hash of given value with
sha256() function. In this example we will hash the string
poftut.com. As we will see this hash function will provide different values the MD5. The create hash will be longer than MD5 which makes it more secure than MD5
SHA512 is the most secure version of the SHA family. Using this hash will make our implementations more secure. We can use SHA512 like below. As we will see it will create more longer hash result than previously implemented MD5 and SHA256.
Hash Multiple Values
If we have a lot of objects those need to hashed doing this one by one is very tedious task. We can use loops or
map function to implement hash. In this example we will provide 3 strings to hash with
sha256 and print them to the console in hexadecimal format.
for h in map(hashlib.sha256,['poftut.com'.encode(),'ismail'.encode(),'ali'.encode()]): h.hexdigest()