我用 Python 为人类构建了一个加密记忆层

发布: (2026年2月8日 GMT+8 06:43)
2 分钟阅读
原文: Dev.to

Source: Dev.to

快速开始

vault = MemoryVault(encryption_key="your-secret-key")

捕获层 — 获取数据

手动输入现在,AI 代理以后。

合成层 — AI 增强

摘要、模式检测、情感分析。(将在 v0.2 中推出)

验证层 — 加密核心

这才是有趣的部分。

永久层 — 锚定已验证的哈希

本地存储、Arweave、Ethereum L2 或 IPFS。

我做出的加密决策(以及原因)

密钥派生:HKDF,而非原始 SHA‑256

我最初的实现通过 SHA‑256(master_key + purpose) 派生加密和签名密钥。它能工作,但这并不是严肃加密系统的做法。我改用了 HKDF(RFC 5869),这是 TLS 1.3 和 Signal 协议使用的行业标准。

from cryptography.hazmat.primitives.kdf.hkdf import HKDF
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
import os

def _derive_key(self, purpose: bytes) -> bytes:
    # Example derivation using HKDF
    hkdf = HKDF(
        algorithm=hashes.SHA256(),
        length=32,
        salt=None,
        info=purpose,
    )
    return hkdf.derive(self._master_key)

def encrypt(self, plaintext: str) -> tuple[bytes, bytes]:
    nonce = os.urandom(12)
    aesgcm = AESGCM(self._enc_key)
    ciphertext = aesgcm.encrypt(nonce, plaintext.encode("utf-8"), None)
    return ciphertext, nonce

签名:HMAC‑SHA256

所有签名数据均使用 HMAC‑SHA256 以确保完整性和真实性。

import hmac
import hashlib

def sign(self, data: bytes) -> bytes:
    return hmac.new(self._sign_key, data, hashlib.sha256).digest()

AI 捕获代理 (v0.2)

未来版本将加入自动捕获并标注条目的 AI 代理。

试一试

  • GitHub:
  • 许可证: MIT
  • 测试套件: 28 项测试全部通过
  • 没有风险投资,没有代币——仅仅是我认为应该存在的工具。
0 浏览
Back to Blog

相关文章

阅读更多 »

UX/UI 排版

Typography 是指什么?- 使用哪种字体 - 在什么位置多大 - 多粗 - 行间距 - …