目录

Hash Function

hash function定义为将大量数据映射到具有指定长度的固定值的函数。 此函数确保相同的输入产生相同的输出,实际上定义为哈希和。 哈希和包括具有特定信息的特征。

这个功能几乎不可能恢复。 因此,任何第三方攻击都像蛮力攻击几乎是不可能的。 此外,这种算法称为one-way cryptographic algorithm

理想的加密哈希函数有四个主要属性 -

  • 必须很容易计算任何给定输入的哈希值。
  • 从哈希生成原始输入必须是不可行的。
  • 在不更改哈希值的情况下修改输入是不可行的。
  • 找到具有相同散列的两个不同输入是不可行的。

例子 (Example)

请考虑以下示例,该示例有助于使用十六进制格式的字符匹配密码。

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')

流程图(Flowchart)

我们借助以下流程图解释了该程序的逻辑 -

哈希函数流程图

输出 (Output)

我们的代码将产生以下输出 -

散列函数输出

输入的密码两次与哈希函数匹配。 这可确保输入两次的密码准确无误,这有助于收集有用数据并以加密格式保存。

↑回到顶部↑
WIKI教程 @2018