1import hashlib
2hash_object = hashlib.sha256(b'Hello World')
3hex_dig = hash_object.hexdigest()
4print(hex_dig)
5
1# Hash Function
2# SHA hash algorithms.
3
4import hashlib
5
6# initializing string
7str = "TYCS"
8
9# encoding TYCS using encode()
10# then sending to SHA1()
11result = hashlib.sha1(str.encode())
12
13# printing the equivalent hexadecimal value.
14print("The hexadecimal equivalent of SHA1 is : ")
15print(result.hexdigest())
16
17# encoding TYCS using encode()
18# then sending to SHA224()
19result = hashlib.sha224(str.encode())
20
21# printing the equivalent hexadecimal value.
22print("The hexadecimal equivalent of SHA224 is : ")
23print(result.hexdigest())
24
25# encoding TYCS using encode()
26# then sending to SHA256()
27result = hashlib.sha256(str.encode())
28
29# printing the equivalent hexadecimal value.
30print("The hexadecimal equivalent of SHA256 is : ")
31print(result.hexdigest())
32
33# initializing string
34str = "TYCS"
35
36# encoding TYCS using encode()
37# then sending to SHA384()
38result = hashlib.sha384(str.encode())
39
40# printing the equivalent hexadecimal value.
41print("The hexadecimal equivalent of SHA384 is : ")
42print(result.hexdigest())
43
44# initializing string
45str = "TYCS"
46
47# initializing string
48str = "TYCS"
49
50# encoding TYCS using encode()
51# then sending to SHA512()
52result = hashlib.sha512(str.encode())
53
54# printing the equivalent hexadecimal value.
55print("The hexadecimal equivalent of SHA512 is : ")
56print(result.hexdigest())
1# hash for integer unchanged
2print('Hash for 181 is:', hash(181))
3# hash for decimal
4print('Hash for 181.23 is:',hash(181.23))
5# hash for string
6print('Hash for Python is:', hash('Python'))
7
1Hashing implementation at this link:
2
3https://github.com/shreyasvedpathak/Data-Structure-Python/tree/master/Hashing
1import uuid
2import hashlib
3
4def hash_password(password):
5 # uuid is used to generate a random number
6 salt = uuid.uuid4().hex
7 return hashlib.sha256(salt.encode() + password.encode()).hexdigest() + ':' + salt
8
9def check_password(hashed_password, user_password):
10 password, salt = hashed_password.split(':')
11 return password == hashlib.sha256(salt.encode() + user_password.encode()).hexdigest()
12
13new_pass = input('Please enter a password: ')
14hashed_password = hash_password(new_pass)
15print('The string to store in the db is: ' + hashed_password)
16old_pass = input('Now please enter the password again to check: ')
17if check_password(hashed_password, old_pass):
18 print('You entered the right password')
19else:
20 print('I am sorry but the password does not match')
21