diff --git a/README_en.md b/README_en.md index 46ccff6..9820a90 100644 --- a/README_en.md +++ b/README_en.md @@ -10,6 +10,7 @@ The project uses the Chinese national standard cryptography algorithm to impleme . ├── basedockerfile (being used to build base iamge) ├── dockerfile (being used to build application) +├── doc (development documents) ├── include (gmssl header) ├── lib (gmssl shared object) ├── LICENSE diff --git a/basedockerfile b/basedockerfile index 622815f..960c6ca 100644 --- a/basedockerfile +++ b/basedockerfile @@ -1,6 +1,8 @@ FROM python:3.11 -COPY src /app +COPY requirements.txt /app/ + +COPY lib/* /lib/ WORKDIR /app diff --git a/src/README_app.md b/doc/README_app.md similarity index 100% rename from src/README_app.md rename to doc/README_app.md diff --git a/doc/README_app_en.md b/doc/README_app_en.md new file mode 100644 index 0000000..174da99 --- /dev/null +++ b/doc/README_app_en.md @@ -0,0 +1,7 @@ +# APP Doc + +## Client router + +/request_node +get method +pr diff --git a/src/README_tpre.md b/doc/README_tpre.md similarity index 100% rename from src/README_tpre.md rename to doc/README_tpre.md diff --git a/src/README_tpre_en.md b/doc/README_tpre_en.md similarity index 100% rename from src/README_tpre_en.md rename to doc/README_tpre_en.md diff --git a/requirements.txt b/requirements.txt index 049ec0e..9551179 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,3 @@ -gmssl-python \ No newline at end of file +gmssl-python +fastapi +uvicorn \ No newline at end of file diff --git a/src/README_app_en.md b/src/README_app_en.md deleted file mode 100644 index e69de29..0000000 diff --git a/src/demo.py b/src/demo.py index f68dc8a..b8a2b46 100644 --- a/src/demo.py +++ b/src/demo.py @@ -1,30 +1,50 @@ from tpre import * +import time # 1 +start_time = time.time() pk_a, sk_a = GenerateKeyPair() m = b"hello world" +end_time = time.time() +elapsed_time = end_time - start_time +print(f"代码块1运行时间:{elapsed_time}秒") # 2 +start_time = time.time() capsule_ct = Encrypt(pk_a, m) +end_time = time.time() +elapsed_time = end_time - start_time +print(f"代码块2运行时间:{elapsed_time}秒") # 3 pk_b, sk_b = GenerateKeyPair() -N = 70 -T = 49 +N = 10 +T = 5 # 5 +start_time = time.time() rekeys = GenerateReKey(sk_a, pk_b, N, T) +end_time = time.time() +elapsed_time = end_time - start_time +print(f"代码块5运行时间:{elapsed_time}秒") # 7 +start_time = time.time() cfrag_cts = [] for rekey in rekeys: cfrag_ct = ReEncrypt(rekey, capsule_ct) cfrag_cts.append(cfrag_ct) +end_time = time.time() +elapsed_time = end_time - start_time +print(f"代码块7运行时间:{elapsed_time}秒") # 9 +start_time = time.time() cfrags = mergecfrag(cfrag_cts) m = DecryptFrags(sk_b, pk_b, pk_a, cfrags) - +end_time = time.time() +elapsed_time = end_time - start_time +print(f"代码块9运行时间:{elapsed_time}秒") print(m) diff --git a/src/server.py b/src/server.py index e69de29..5a25696 100644 --- a/src/server.py +++ b/src/server.py @@ -0,0 +1,44 @@ +from fastapi import FastAPI +from fastapi.encoders import jsonable_encoder +from fastapi.responses import JSONResponse +from typing import Tuple, Callable + +app = FastAPI() + +@app.get("/server/get_node") +async def get_node(ip: str) -> int: + ''' + 中心服务器与节点交互, 节点发送ip, 中心服务器接收ip存入数据库并将ip转换为int作为节点id返回给节点 + params: + ip: node ip + return: + id: ip按点分割成四部分, 每部分转二进制后拼接再转十进制作为节点id + ''' + # ip存入数据库, id = hash(int(ip)) + + ip_parts = ip.split(".") + ip_int = 0 + for i in range(4): + ip_int += int(ip_parts[i]) << (24 - (8 * i)) + return ip_int + +@app.get("/server/delete_node") +async def delete_node(ip: str) -> None: + # 按照节点ip遍历数据库, 删除该行数据 + +@app.post("/server/send_nodes_list") +async def send_nodes_list(count: int) -> JSONResponse: + ''' + 中心服务器与客户端交互, 客户端发送所需节点个数, 中心服务器从数据库中顺序取出节点封装成json格式返回给客户端 + params: + count: 所需节点个数 + return: + JSONResponse: {id: ip,...} + ''' + nodes_list = {} + for i in range(count): + # 访问数据库取出节点数据 + node = (id, ip) + nodes_list[node[0]] = node[1] + json_result = jsonable_encoder(nodes_list) + return JSONResponse(content=json_result) \ No newline at end of file