哈希加密Python实现

一、代码

from cryptography.fernet import Fernet
import os
import bcrypt

# 密钥管理和对称加密相关
def save_key_to_file(key: bytes, key_path: str):
    with open(key_path, 'wb') as file:
        file.write(key)

def load_key_from_file(key_path: str) -> bytes:
    if not os.path.exists(key_path):
        raise FileNotFoundError(f"密钥文件 {
     key_path} 不存在")
    with open(key_path, 'rb') as file:
        return file.read()

def encrypt_data(data: str, key: bytes) -> bytes:
    fernet = Fernet(key)
    encrypted_data = fernet.encrypt(data.encode())
    return encrypted_data

def decrypt_data(encrypted_data: bytes, key: bytes) -> str:
    fernet = Fernet(key)
    decrypted_data = fernet.decrypt(encrypted_data).decode()
    return decrypted_data

# 密码哈希相关
def hash_password(password: str) -> str:
    salt = bcrypt.gensalt()
    hashed_password = bcrypt.hashpw(password.encode(), salt)
    return hashed_password.decode('utf-8')

def verify_password(password: str, stored_hash: str) -> bool:
    return bcrypt.checkpw(password.encode(), stored_hash.encode())

# 示例数据处理
def main():
    # 对称加密部分
    sensitive_data = "mysecretpassword"
    key_path = 'secret_key.key'

    # 生成并保存密钥
    key = Fernet.generate_key()
    save_key_to_file(key, key_path)

    # 加载已保存的密钥
    loaded_key = load_key_from_file(key_path)

    encrypted_data = encrypt_data(sensitive_data, loaded_key)
    print("Encrypted data:", encrypted_data)

    decrypted_data = decrypt_data(encrypted_data, loaded_key)
    print("Decrypted data:", decrypted_data)

    # 密码哈希部分
    password = "mysecretpassword"
    hashed_password = hash_password(password)
    print("Hashed password:", hashed_password)

    is_valid = verify_password(password, hashed_password)
    print("Password verification:", is_valid)

if __name__ == "__main__":
    main()

二、运行结果:

在这里插入图片描述

三、报错解决

出现 ModuleNotFoundError: No module named ‘bcrypt’ 错误是因为Python环境尚未安装名为bcrypt的库,该库用于密码哈希。要解决这个问题,你需要在你的Python环境中安装bcrypt库。在终端(对于Linux/Mac用户)或命令提示符(对于Windows用户)中执行以下命令:

Shell

pip install bcrypt

如果你使用的是虚拟环境(如venv或conda环境),请确保激活对应的虚拟环境后再运行上述命令。

对于某些系统,可能需要通过特定包管理器或者使用特定的Python版本的pip来安装bcrypt,例如在Ubuntu上可能需要先安装libffi-dev和python3-dev等依赖,然后使用pip3来安装bcrypt:

Shell

sudo apt-get install libffi-dev python3-dev
pip3 install bcrypt

安装完成后,再尝试运行你的Python脚本,bcrypt模块就应该可以正常导入和使用了。

相关推荐

  1. python算法实现

    2024-02-07 02:26:01       39 阅读
  2. C#加密和解密、

    2024-02-07 02:26:01       16 阅读
  3. Python查找法

    2024-02-07 02:26:01       8 阅读
  4. 「数据结构」表2:实现

    2024-02-07 02:26:01       37 阅读
  5. STL容器之的补充——实现

    2024-02-07 02:26:01       18 阅读
  6. C++简单实现查找

    2024-02-07 02:26:01       18 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-07 02:26:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-07 02:26:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-07 02:26:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-07 02:26:01       20 阅读

热门阅读

  1. js 基础

    js 基础

    2024-02-07 02:26:01      25 阅读
  2. 关于自动驾驶概念的学习和一些理解

    2024-02-07 02:26:01       32 阅读
  3. CSS选择器及其优先级

    2024-02-07 02:26:01       33 阅读
  4. docker安装gitlab-runner

    2024-02-07 02:26:01       29 阅读
  5. 2.5学习总结9

    2024-02-07 02:26:01       28 阅读
  6. DBA不仅仅是管理数据库--也要管理中间件

    2024-02-07 02:26:01       37 阅读
  7. 记录 | python isinstance()用法

    2024-02-07 02:26:01       39 阅读