初始版本:基于 DHT 的去中心化聊天客户端
- 纯标准库实现 Kademlia DHT(UDP + asyncio),报文 Ed25519 签名认证 - 超过 1200 字节的载荷自动分片与重组,上限 1 MiB - 公共聊天室:成员公告 + DHT 日志回放,迟到入群者也能补到历史 - 一对一私聊:X25519 + HKDF + AEAD 端到端加密,支持离线信箱 - Tkinter 图形界面,asyncio 运行在后台线程,事件经队列回传 UI - 局域网组播自动发现 + 手动引导地址(-b)两种互联方式 - run.cmd / start.bat 启动脚本,自动寻找 py -3 或 python - 48 个单元测试覆盖密码学原语、DHT 路由/协议与聊天层 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
commit
e002ba9f62
19 files changed
+5064
No files matched your search
@@ -0,0 +1,79 @@
|
||||
"""Local node identity: persistent Ed25519 key pair plus derived node id."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
|
||||
from . import crypto
|
||||
|
||||
DEFAULT_STATE_DIR = Path.home() / ".dhtchat"
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Identity:
|
||||
"""A local identity. ``node_id`` is SHA1 of the public key."""
|
||||
|
||||
seed: bytes
|
||||
public: bytes
|
||||
|
||||
@property
|
||||
def node_id(self) -> bytes:
|
||||
return crypto.node_id_from_public(self.public)
|
||||
|
||||
@property
|
||||
def node_id_hex(self) -> str:
|
||||
return self.node_id.hex()
|
||||
|
||||
@property
|
||||
def public_hex(self) -> str:
|
||||
return self.public.hex()
|
||||
|
||||
@property
|
||||
def fingerprint(self) -> str:
|
||||
return crypto.fingerprint(self.public)
|
||||
|
||||
def sign(self, message: bytes) -> bytes:
|
||||
return crypto.ed25519_sign(self.seed, message)
|
||||
|
||||
def verify(self, public: bytes, message: bytes, signature: bytes) -> bool:
|
||||
return crypto.ed25519_verify(public, message, signature)
|
||||
|
||||
def shared_secret_with(self, peer_public: bytes) -> bytes:
|
||||
return crypto.shared_secret(self.seed, peer_public)
|
||||
|
||||
# -- persistence -----------------------------------------------------
|
||||
@classmethod
|
||||
def generate(cls) -> "Identity":
|
||||
seed = crypto.generate_private_key()
|
||||
return cls(seed=seed, public=crypto.ed25519_public_key(seed))
|
||||
|
||||
@classmethod
|
||||
def load_or_create(cls, path: Path | str) -> "Identity":
|
||||
path = Path(path)
|
||||
if path.exists():
|
||||
try:
|
||||
data = json.loads(path.read_text(encoding="utf-8"))
|
||||
seed = bytes.fromhex(data["seed"])
|
||||
if len(seed) != crypto.PRIVATE_KEY_LEN:
|
||||
raise ValueError("bad seed length")
|
||||
return cls(seed=seed, public=crypto.ed25519_public_key(seed))
|
||||
except (ValueError, KeyError, OSError):
|
||||
pass
|
||||
identity = cls.generate()
|
||||
identity.save(path)
|
||||
return identity
|
||||
|
||||
def save(self, path: Path | str) -> None:
|
||||
path = Path(path)
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
payload = json.dumps({"seed": self.seed.hex()}, indent=2)
|
||||
tmp = path.with_suffix(path.suffix + ".tmp")
|
||||
tmp.write_text(payload, encoding="utf-8")
|
||||
os.replace(tmp, path)
|
||||
try:
|
||||
os.chmod(path, 0o600)
|
||||
except OSError:
|
||||
pass
|
||||
Reference in new issue
Block a user