卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章18455本站已运行340

你在Python中做过最酷的程序是什么?

你在Python中做过最酷的程序是什么?

我做过的最酷的 Python 程序是 Python 密码哈希器。我们先来了解一下什么是Python密码哈希。

什么是密码哈希?

Python 密码哈希是一种高级加密形式,可用于安全地在线存储密码。在当今万物互联的世界中,用户密码是互联网上最容易受到攻击的敏感信息之一。使用不同的哈希算法将密码字符串转换为随机字符字符串,这些算法已在我的程序中使用。系统会指示用户输入密码字符串,然后选择要使用的适当的哈希算法。然后显示输出哈希,该哈希可以在线存储。

使用的步骤

  • 为不同的哈希方法创建函数

  • 接受用户输入的密码字符串

  • 接受用户输入以选择哈希方法

  • 转换字符串并提供输出

第 1 步:为不同的哈希方法创建函数

首先,我们创建不同的函数,将密码字符串作为参数并将其转换为密文形式。密文实际上是数据经过哈希处理后的形式。不同的函数包含不同的哈希算法。

语法

'
def hash_with_MD5(message):
   print ("MD5:", hashlib.md5(message).hexdigest()) 

这里函数将消息作为参数,并使用 MD5 哈希算法将其转换为密文。然后为用户打印哈希摘要。如果不使用MD5,而是使用其他哈希算法,则语法是相同的,只是哈希函数的调用会发生变化。

算法

第 1 步 - 为不同的哈希算法定义不同的函数

第 2 步 - 将用户输入的字符串作为函数的参数

第 3 步 - 在函数主体中,打印哈希密码的十六进制摘要

示例

'
def hash_with_MD5(message):
   encoded=message.encode()
   print ("Hashed with MD5:", hashlib.md5(encoded).hexdigest())
def hash_with_SHA(message):
   encoded=message.encode()
   print ("Hashed with SHA:", hashlib.sha256(encoded).hexdigest())
def hash_with_blake(message):
   encoded=message.encode()
   print ("Hashed with blake2b:",   hashlib.blake2b(encoded).hexdigest())
message='tutorialspoint'
hash_with_MD5(message)
hash_with_SHA(message)
hash_with_blake(message)

输出

'
Hashed with MD5: 6c60b3cfe5124f982eb629e00a98f01f
Hashed with SHA:
15e6e9ddbe43d9fe5745a1348bf1535b0456956d18473f5a3d14d6ab06737770
Hashed with blake2b:
109f6f017d7a77bcf57e4b48e9c744280ae7f836477c16464b27a3fe62e1353c70ec4c7f938080
60ee7c311094eede0235a43151c3d2b7401a3cb5a8f8ab3fbb 

第 2 步:获取用户输入的密码字符串

下一步是从用户那里获取需要存储的密码的输入。出于安全原因,必须对要存储的密码进行哈希处理,并且在进行哈希处理之前,必须对用户输入的密码进行编码,以确保其适合传递给哈希函数。此编码操作由encode()函数执行。

语法

'
password=input("message").encode()

我们使用 input() 函数从用户那里收到的密码不能用于散列,因此它是使用encode() 函数进行编码的。此处将这两个步骤组合在一个命令中,以便于编码和简化。

算法

第 1 步 - 使用 input() 函数接收用户输入

第 2 步- 将输入转换为编码格式

示例

'
password=input(“Enter the password for hashing: ”).encode()

输出

'
Enter the password for hashing: Python 

第 3 步:接受用户输入以选择哈希方法

我们将向用户提供选择,以决定我们将使用哪种哈希算法来安全地对密码进行哈希处理。不同的方法有不同的优点和缺点,因此我们让用户选择最适合特定密码的方法。这里我们使用一个简单的 If-else 结构来确定用户输入的选择。

语法

'
while True:
   choice = input("Enter choice(1/2/3): ")
      if choice in ('1', '2', '3'):
      try:
      ………………… 

在这里,我们询问用户他们执行的哈希类型以及选项列表。然后在有效的输入列表中检查输入,如果为真,则执行所要求的操作。否则,程序控制将跳出循环。

算法

第 1 步 − 要求用户输入

第 2 步- 检查用户输入是否有效

第 3 步 - 执行所选操作

第 4 步 - 询问是否要执行更多操作

示例

'
import hashlib
def hash_with_MD5(password):
   
   #encoded=password.encode()
   print ("Hashed with MD5:", hashlib.md5(password).hexdigest())
def hash_with_SHA(password):
   
   #encoded=password.encode()
   print ("Hashed with SHA:", hashlib.sha256(password).hexdigest())
def hash_with_blake(password):
   
   #encoded=password.encode()
   print ("Hashed with blake2b:", hashlib.blake2b(password).hexdigest())
print("Select hashing operation.") 
print("1.MD5")
print("2.SHA")
print("3.blake")
while True:
   
   # take input from the user
   choice = input("Enter choice(1/2/3): ")
   
   # check if choice is one of the four options
   if choice in ('1', '2', '3'):
      try:
         password=input('Enter the password for hashing: ').encode()
      except ValueError:
         print("Invalid input. Please enter a string.")
         continue
      if choice == '1':
         hash_with_MD5(password)
      elif choice == '2':
         hash_with_SHA(password)
      elif choice == '3':
         hash_with_blake(password)
            
            # checking if user wants another calculation
      # break the while loop if answer is no
      next_calculation = input("Let's do next calculation? (yes/no): ")
      if next_calculation == "no":
         break
      else:
         print("Invalid Input")

输出

'
Select hashing operation.
1.MD5
2.SHA
3.blake
Enter choice(1/2/3): 2
Enter the password for hashing:Python
Hashed with SHA:
18885f27b5af9012df19e496460f9294d5ab76128824c6f993787004f6d9a7db
Let's do next calculation? (yes/no): yes
Enter choice(1/2/3): 1
Enter the password for hashing:Tutorialspoint 
Hashed with MD5: da653faa9f00528be9a57f3474f0e437
Let's do next calculation? (yes/no): no 

结论

因此,我们在这里构建了用于散列用户密码并返回以进行安全存储的程序。程序成功运行并解决了一个重要的目的。还可以进行进一步的修改以实现更新的功能,我们将在稍后进行。

卓越飞翔博客
上一篇: C# 中的文件权限
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏