-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathB01_hash.py
32 lines (21 loc) · 1018 Bytes
/
B01_hash.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# PBKDF2 HASH memakai hashlib
import hashlib, binascii, os # Digunakan untuk generate angka random
def hashPassword(password):
# Membuat salt berdasarkan nomor random yang diubah menjadi ASCII
salt = hashlib.sha256(os.urandom(32)).hexdigest().encode('ascii')
# Hash password dengan PBKDF2
key = hashlib.pbkdf2_hmac("sha512", password.encode('ascii'), salt, 100000)
# Mengubah hash menjadi bentuk hexadesimal
key = binascii.hexlify(key)
# Memberi output password yang sudah dihash dan saltnya dalam ASCII
return (salt + key).decode('ascii')
def verifyPassword(password, inputPass):
# Salt
salt = password[:64]
# Menghilangkan salt dari password
password = password[64:]
# Hash input password.
hashedInput = hashlib.pbkdf2_hmac('sha512', inputPass.encode('ascii'), salt.encode('ascii'), 100000)
# Mengubah hashed input menjadi benduk hexadesimal
hashedInput = binascii.hexlify(hashedInput).decode('ascii')
return password == hashedInput