Python取证-哈希函数
散列函数 被定义为在大量数据上映射到一个指定长度的固定值的函数。这个函数确保相同的输入会产生相同的输出,这实际上被定义为哈希和。哈希和包括一个具有特定信息的特征。
这个函数实际上是不可能被还原的。因此,任何第三方的攻击,如暴力攻击,实际上是不可能的。此外,这种算法被称为 单向加密算法。
一个理想的密码学哈希函数有四个主要特性
- 对于任何给定的输入,它必须容易计算出哈希值。
- 从其散列值生成原始输入必须是不可行的。
- 在不改变哈希值的情况下修改输入必须是不可行的。
- 找到两个具有相同哈希值的不同输入必须是不可行的。
例子
请看下面这个例子,它有助于用十六进制的字符来匹配密码。
import uuid
import hashlib
def hash_password(password):
# userid is used to generate a random number
salt = uuid.uuid4().hex #salt is stored in hexadecimal value
return hashlib.sha256(salt.encode() + password.encode()).hexdigest() + ':' + salt
def check_password(hashed_password, user_password):
# hexdigest is used as an algorithm for storing passwords
password, salt = hashed_password.split(':')
return password == hashlib.sha256(salt.encode()
+ user_password.encode()).hexdigest()
new_pass = raw_input('Please enter required password ')
hashed_password = hash_password(new_pass)
print('The string to store in the db is: ' + hashed_password)
old_pass = raw_input('Re-enter new password ')
if check_password(hashed_password, old_pass):
print('Yuppie!! You entered the right password')
else:
print('Oops! I am sorry but the password does not match')
流程图
我们已经借助以下流程图解释了这个程序的逻辑 —
输出
我们的代码将产生以下输出 –
输入两次的密码与哈希函数相匹配。这确保了两次输入的密码是准确的,这有助于收集有用的数据,并以加密的形式保存它们。