亲宝软件园·资讯

展开

python hashlib加密实现代码

人气:0

用于加密相关的操作,代替了md5模块和sha模块,主要提供SHA1,SHA224,SHA256,SHA512,MD5算法。

以下是算法示例:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import hashlib
#=================MD5()加密=====================
hhb = hashlib.md5(bytes("jahiuhfdakj", encoding='utf-8')) #加盐处理
ret = hhb.update(bytes("123", encoding='utf-8'))  #“123”是需要加密的字符串
print("MD5加密:", hhb.hexdigest())


#=================SHA1()加密=====================
hhb = hashlib.sha1()
ret = hhb.update(bytes("123", encoding='utf-8'))
print("SHA1加密:", hhb.hexdigest())


#=================SHA224()加密=====================
hhb = hashlib.sha224()
ret = hhb.update(bytes("123", encoding='utf-8'))
print("SHA224加密:", hhb.hexdigest())

#=================SHA256()加密=====================
hhb = hashlib.sha256()
ret = hhb.update(bytes("123", encoding='utf-8'))
print("SHA256加密:", hhb.hexdigest())

#=================SHA512()加密=====================
hhb = hashlib.sha512()
ret = hhb.update(bytes("123", encoding='utf-8'))
print("SHA512加密:", hhb.hexdigest())

#=================SHA3_256()加密=====================
hhb = hashlib.sha3_256()
ret = hhb.update(bytes("123", encoding='utf-8'))
print("SHA3_256加密:", hhb.hexdigest())

#=================SHA3_384()加密=====================
hhb = hashlib.sha3_384()
ret = hhb.update(bytes("123", encoding='utf-8'))
print("SHA3_384加密:", hhb.hexdigest())

#=================SHA3_512()加密=====================
hhb = hashlib.sha3_512()
ret = hhb.update(bytes("123", encoding='utf-8'))
print("SHA3_512加密:", hhb.hexdigest())

算法示例的结果:

MD5加密: 113ccbcda570622489c183f6a6e0121e
SHA1加密: 40bd001563085fc35165329ea1ff5c5ecbdbbeef
SHA224加密: 78d8045d684abd2eece923758f3cd781489df3a48e1278982466017f
SHA256加密: a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3
SHA512加密: 3c9909afec25354d551dae21590bb26e38d53f2173b8d3dc3eee4c047e7ab1c1eb8b85103e3be7ba613b31bb5c9c36214dc9f14a42fd7a2fdb84856bca5c44c2
SHA3_256加密: a03ab19b866fc585b5cb1812a2f63ca861e7e7643ee5d43fd7106b623725fd67
SHA3_384加密: 9bd942d1678a25d029b114306f5e1dae49fe8abeeacd03cfab0f156aa2e363c988b1c12803d4a8c9ba38fdc873e5f007
SHA3_512加密: 48c8947f69c054a5caa934674ce8881d02bb18fb59d5a63eeaddff735b0e9801e87294783281ae49fc8287a0fd86779b27d7972d3e84f0fa0d826d7cb67dfefc

基于hashlib.md5()对密码进行加密实现用户登录验证的示例:

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import hashlib
#基于MD5对密码进行加密实现用户登录验证

def md5(arg):
  """
  使用hashlib.md5()对密码进行加密处理
  :param arg: 需要加密处理的密码
  :return:
  """
  hash = hashlib.md5(bytes("我在学习python", encoding='utf-8')) #加盐,使用密码加密更有保障
  hash.update(bytes(arg, encoding='utf-8'))
  return hash.hexdigest()

def login(username, password):
  """
  用于用户登录验证
  :param username: 用户名
  :param password: 密码
  :return: True,登录成功;False,登录失败。
  """
  with open("user.txt", "r", encoding="utf-8") as f:
    for line in f:
      line = line.strip() #默认strip无参数,会去掉首尾空格、换行符;有参数则去除指定值
      line_list = line.split("|")  #以|符号提取用户名和密码
      if username == line_list[0] and md5(password) == line_list[1]:
        return True
      else:
        return False

def register(username, password):
  """
  用户注册
  :param username:用户名
  :param password:密码
  :return: True,注册成功
  """
  with open("user.txt", "a", encoding="utf-8") as f:
    temp = "\n" + username + "|" + md5(password)  # "\n"换行符
    f.write(temp)
  return True

def user_exsit(username):
  """
  注册时,判断用户名是否存在
  :param username:用户名
  :return:True, 用户名已存在
  """
  with open("user.txt", "r", encoding="utf-8") as f:
    for line in f:
      line = line.strip()
      line_list = line.split("|")
      if username == line_list[0]:
        return True
    return False

def main():
  print("欢迎您使用本系统,请输入你进行操作选项。")
  inp = input("1.登录;2.注册。请输入编号: ")
  if inp == "1":
    times = 1
    while True:
      if times == 4:
        print("输入3次用户名或密码不正确,请在一小时后再重试。")
        break
      user = input("请输入你的用户名:")
      pwd = input("请输入你的密码:")
      is_login = login(user, pwd)
      if is_login:
        print("恭喜您!系统登录成功。")
        break
      else:
        print("用户名或密码不正确。")
      times += 1
  if inp == "2":
    user = input("请输入你的用户名:")
    pwd = input("请输入你的密码:")
    if user_exsit(user):
      print("用户名已经存在,注册失败!")
    else:
      ret = register(user, pwd)
      if ret:
        print("注册成功!")
      else:
        print("注册失败!")

main()

加密后的user.txt的文件内容。

您可能感兴趣的文章:

加载全部内容

相关教程
猜你喜欢
用户评论