cryptography
Crypto加密和散列
概述 cryptography
加密库是用于加密操作的领先 Python 包。它通过旨在防止常见错误的干净、安全的 API 提供高级配方(Fernet 对称加密)和低级加密原语(散列、密钥派生、数字签名、HMAC)。
PyRun通过Pyodide的WebAssembly构建支持加密库。您可以使用 SHA-256 对字符串进行哈希处理,使用 Fernet 加密和解密数据,并探索密钥派生函数——所有这些都在您的浏览器中进行。它是学习应用密码学概念的绝佳工具,无需任何本地设置。
代码和执行输出
SHA-256 散列和 Fernet 对称加密。
Cryptography: Hash & Encrypt在编辑器中运行
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.backends import default_backend
from cryptography.fernet import Fernet
# SHA-256 hashing
digest = hashes.Hash(hashes.SHA256(), backend=default_backend())
digest.update(b"Hello, PyRun!")
hash_bytes = digest.finalize()
print("SHA-256:", hash_bytes.hex())
# Fernet symmetric encryption
key = Fernet.generate_key()
f = Fernet(key)
message = b"Secret message from PyRun"
token = f.encrypt(message)
print("\nEncrypted:", token[:40], "...")
decrypted = f.decrypt(token)
print("Decrypted:", decrypted.decode())相关套餐
推荐的 Python 资源
通过相关的交互式教程、备忘单和代码比较来扩展您的知识。
Python教程
Python 循环:For 和 While 循环解释
了解如何使用 Python 循环来迭代数据。通过交互式示例掌握 for 循环、while 循环、break、continue 和循环最佳实践。
查看资源
操作指南
如何在 Python 中对列表进行排序(升序和降序)
了解如何在 Python 中使用 sort() 方法和sorted() 函数对列表进行排序。发现自定义键排序和逆序示例。
查看资源
备忘单
Python 字符串方法备忘单
Python 字符串操作的完整参考指南。掌握格式化、搜索、拆分、替换和检查字符串属性。
查看资源
语言比较
Python 与 JavaScript:哪种编程语言最好?
Python 和 JavaScript 的全面比较。探索语法差异、性能、用例(后端与前端)和编码示例。
查看资源