hashlib、time模块

发布时间 2024-01-04 16:31:01作者: Formerly0^0
加密算法和摘要算法
# 摘要算法 : 摘要算法又称哈希算法、散列算法。
# 它通过一个函数,把任意长度的数据转换为一个长度固定的数据串(通常用16进制的字符串表示)。
# 1 --> 32位的加密串
# 12 ---> 32位的加密串


# 加密算法 :我有一把主钥匙  - 私钥
# 复制出去的备份 叫 公钥
# 公钥加密 ---> 私钥解密
# 公钥加密 ---> 公钥不能解密


# 非对称加密
# 公钥很多份
# 私钥只有一份

# 【1】验签 :目的就就是为了确保数据的完整性,没有被篡改过
# 传输视频数据的时候 :加密一个串 mp4 --> 1.ts  2.ts 3.ts ->> 带过来的加密串 mp4
# 【2】加密:对我们的密码进行加密 ---》 加密串


# 摘要算法 :单向加密 --> 没有办法解密  能作为验证的理由是因为每次加密得到的结果都是一样的
# 加密算法 :加密 <--> 解密

# hashlib 摘要算法
import hashlib
import json
import random

# 【1】创建md5对象
md5 = hashlib.md5()
# print(md5)
# <md5 _hashlib.HASH object @ 0x000002928188BD70>
# 【2】获取到原始的数据 + 转为二进制数据
data = '你真的太帅啦!'
data = data.encode('utf8')
# 【3】加密数据
md5.update(data)
# 【4】获取到加密后的值
result = md5.digest()
print(result)  # b']A@*\xbcK*v\xb9q\x9d\x91\x10\x17\xc5\x92'
result = md5.hexdigest()
print(result)  # e10adc3949ba59abbe56e057f20f883e


# 摘要算法加密的特点是 : 相同的数据加密出来的加密串是一样的,不同的数据的加密串肯定是不一样的
# 自己的网站会有一个数据库 : 数据库里面存了这些特别简单的密码的加密串
# 破解方法:撞库
# WiFi万能钥匙 , 暴力破解WiFi密码 (python代码会运行很长时间) # 6位开始拼密码 7位 8 9 10 11 12


def get_verify_code(n):
    code = ''
    for i in range(n):
        random_int = str(random.randint(0, 9))  # 0-9之间的整数
        random_upper = chr(random.randint(65, 90))  # A-Z之间的字母
        random_lower = chr(random.randint(97, 122))  # a-z之间的字母
        temp = random.choice([random_int, random_upper, random_lower])
        code += temp
    return code


def encry_data(data, salt):
    old_data = str(data) + str(salt)
    md5 = hashlib.md5()
    md5.update(old_data.encode('utf8'))
    return md5.hexdigest()


def save_data(data):
    with open('user_data.json', 'a', encoding='utf8') as fp:
        json.dump(data, fp, ensure_ascii=False)


def register():
    username = input("用户名 :>>>> ").strip()
    password = input("密码 :>>>> ").strip()
    # 注册的时候需要输入的验证码
    code_tep = get_verify_code(6)
    print(code_tep)
    code_input = input("验证码 :>>>> ").strip()
    if code_tep.upper() != code_input.upper():
        print(f"验证码错误")
    # 加密密码时需要的盐
    salt = get_verify_code(4)
    password = encry_data(data=password, salt=salt)
    # password = password + '|' + salt
    user_data = {username: {'username': username, 'password': password, 'salt': salt}}
    save_data(data=user_data)


def register_one():
    username = input("用户名 :>>>> ").strip()
    password = input("密码 :>>>> ").strip()
    # 注册的时候需要输入的验证码
    code_tep = get_verify_code(6)
    print(code_tep)
    code_input = input("验证码 :>>>> ").strip()
    if code_tep.upper() != code_input.upper():
        print(f"验证码错误")
    # 加密密码时需要的盐
    salt = get_verify_code(4)
    password = encry_data(data=password, salt=salt)
    password += salt
    user_data = {username: {'username': username, 'password': password}}
    save_data(data=user_data)



def read_data():
    with open('user_data.json', 'r', encoding='utf8') as fp:
        data = json.load(fp)
    return data


def login():
    username = input("用户名 :>>>> ").strip()
    password = input("密码 :>>>> ").strip()
    data = read_data()
    user_data = data.get(username)
    old_password = user_data.get('password')
    # 加密的盐
    salt = old_password[-4:]
    # 加密后的密码串
    encry_password = old_password[:-4]
    # 将输入的密码进行加密
    password = encry_data(data=password, salt=salt)
    # 输入的密码的加密串 和 原来的加密串进行比对
    if password == encry_password:
        print(f"{username} :>>> 登录成功!")
login()

time模块

# time 模块
import time

# 【1】时间戳:从1970年01月01日00时00分00秒开始到现在
# 可以用来计算程序运行从开始到结束的时间
# print(time.time()) # 1702786546.878736

# 【2】时间戳转换为时间元组(UTC国际时间)
time_time = time.time()
print(time.gmtime(time_time))
# time.struct_time(tm_year=2023, tm_mon=12, tm_mday=17, tm_hour=4, tm_min=14, tm_sec=18, tm_wday=6, tm_yday=351, tm_isdst=0)

# 【3】时间戳转换为时间元组(当地时间)
time_time = time.time()
print(time.localtime(time_time))
# time.struct_time(tm_year=2023, tm_mon=12, tm_mday=17, tm_hour=12, tm_min=17, tm_sec=20, tm_wday=6, tm_yday=351, tm_isdst=0)

# 【4】时间字符串
print(time.strftime("%Y-%m-%d %X"))
print(time.strftime("%Y-%m-%d %H:%M:%S"))
# 2023-12-17 12:19:31

# 【5】时间元组
print(time.localtime())


print(time.time())  # 1700833503.6196976
time_tuple = time.localtime(time.time())
time_str = time.mktime(time_tuple)
print(time_str)  # 1700833503.0


# 格式化当前时间
time_str = time.strftime("%Y-%m-%d %X")
print(time_str)  # 2023-11-24 21:47:28

print(time.localtime(time.time()))
# time.struct_time(tm_year=2023, tm_mon=11, tm_mday=24, tm_hour=21, tm_min=48, tm_sec=13, tm_wday=4, tm_yday=328, tm_isdst=0)

# 格式化当前时间元组
time_local = time.strftime("%Y-%m-%d", time.localtime(time.time()))
print(time_local)  # 2023-11-24


time_str = time.strptime("2025-11-24", "%Y-%m-%d")
print(time_str)
# time.struct_time(tm_year=2023, tm_mon=11, tm_mday=24, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=328, tm_isdst=-1)

time_local = time.strptime("11/24/2023", "%m/%d/%Y")
print(time_local)

# 【需要记住的】
# 时间戳 :time.time()
# 时间元组 : time.localtime()
# 格式化输出时间 : time.strftime(format,时间元组)
# 指定日期和时间转换为时间元组 : time.strptime(自定义时间,时间格式)