Python取证哈希函数
A 哈希函数 定义为将大量数据映射到具有指定长度的固定值的函数。该函数确保相同的输入导致相同的输出,实际上定义为哈希和。哈希和包括具有特定信息的特征。
这个功能实际上是不可能恢复的。因此,任何像蛮力攻击这样的第三方攻击几乎是不可能的。此外,这种算法被称为 单向密码算法 .
一个理想的密码散列函数有四个主要性质:
- 必须很容易计算任何给定输入的哈希值。
- 从其哈希生成原始输入一定是不可行的。
- 在不更改散列的情况下修改输入一定是不可行的。
- 找到具有相同哈希的两个不同输入一定是不可行的。
例子
考虑以下示例,该示例有助于使用十六进制格式的字符匹配密码。
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')
流程图
我们已经借助下面的流程图解释了这个程序的逻辑:
输出
我们的代码将产生以下输出:
输入两次的密码与哈希函数匹配。这样可以确保输入两次的密码是准确的,这有助于收集有用的数据并以加密格式保存它们。