Coverage for agentos/marketplace/skills/encryption/encryption.py: 5%
42 statements
« prev ^ index » next coverage.py v7.14.3, created at 2026-07-06 12:29 +0800
« prev ^ index » next coverage.py v7.14.3, created at 2026-07-06 12:29 +0800
1"""
2encryption — 哈希摘要、Base64 编解码(纯 Python stdlib)。
4Category: security
5"""
8def run(action: str, text: str = "", file_path: str = "", algorithm: str = "sha256") -> str:
9 """加密/哈希工具。action: hash/base64_encode/base64_decode/uuid。algorithm: sha256/md5/sha1/sha512。"""
10 import base64
11 import hashlib
12 import os
13 import uuid
15 def _input():
16 if file_path and os.path.isfile(file_path):
17 with open(file_path, "rb") as f:
18 return f.read()
19 return text.encode("utf-8")
21 try:
22 if action == "hash":
23 data = _input()
24 algo = algorithm.lower()
25 if algo == "md5":
26 h = hashlib.md5(data).hexdigest()
27 elif algo == "sha1":
28 h = hashlib.sha1(data).hexdigest()
29 elif algo == "sha256":
30 h = hashlib.sha256(data).hexdigest()
31 elif algo == "sha512":
32 h = hashlib.sha512(data).hexdigest()
33 else:
34 return f"[encryption] 不支持的算法: {algorithm}, 可用: md5/sha1/sha256/sha512"
35 src = file_path if file_path else f"'{text[:30]}...'" if len(text) > 30 else f"'{text}'"
36 return f"{algo.upper()}({src}) = {h}"
38 if action == "base64_encode":
39 data = _input()
40 encoded = base64.b64encode(data).decode("utf-8")
41 return encoded
43 if action == "base64_decode":
44 data = text.encode("utf-8") if text else _input()
45 try:
46 decoded = base64.b64decode(data).decode("utf-8")
47 except Exception:
48 decoded = base64.b64decode(data).decode("latin-1") # might be binary
49 return decoded
51 if action == "uuid":
52 return f"UUID4: {uuid.uuid4()}\nUUID1: {uuid.uuid1()}"
54 return f"[encryption] 未知操作: {action}, 支持: hash/base64_encode/base64_decode/uuid"
55 except Exception as e:
56 return f"[encryption] 失败: {e}"
59__all__ = ["run"]