From 727dcf01767e46e766b3fdcea0a8ab3a93327f66 Mon Sep 17 00:00:00 2001 From: dqy <1016751306@qq.com> Date: Fri, 20 Oct 2023 16:57:28 +0800 Subject: [PATCH 1/6] =?UTF-8?q?feat:=20=E7=BC=96=E5=86=99=E4=B8=AD?= =?UTF-8?q?=E5=BF=83=E6=9C=8D=E5=8A=A1=E5=99=A8=E4=BB=A3=E7=A0=81=E6=A1=86?= =?UTF-8?q?=E6=9E=B6=EF=BC=9B=E6=95=B0=E6=8D=AE=E5=BA=93=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E5=BE=85=E8=A1=A5=E5=85=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/server.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) 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 From fa039b3f2450154e9564f2e3efce55acdb2dbee2 Mon Sep 17 00:00:00 2001 From: sangge <2251250136@qq.com> Date: Fri, 20 Oct 2023 17:14:22 +0800 Subject: [PATCH 2/6] fix: update base image --- basedockerfile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 From 7e3359fe786fdec2be12b136e326efaf2b603205 Mon Sep 17 00:00:00 2001 From: sangge <2251250136@qq.com> Date: Fri, 20 Oct 2023 17:14:43 +0800 Subject: [PATCH 3/6] feat: add app requirements --- requirements.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 From 4225ae52fc217870455953fe4a9895fb5480bdb8 Mon Sep 17 00:00:00 2001 From: sangge <2251250136@qq.com> Date: Fri, 20 Oct 2023 17:15:31 +0800 Subject: [PATCH 4/6] feat: add timer --- src/demo.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) 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) From 8e961eda8ed66ab966275826d2eae1886c478503 Mon Sep 17 00:00:00 2001 From: sangge <2251250136@qq.com> Date: Fri, 20 Oct 2023 17:17:02 +0800 Subject: [PATCH 5/6] doc: create doc folder --- {src => doc}/README_app.md | 0 doc/README_app_en.md | 7 +++++++ {src => doc}/README_tpre.md | 0 {src => doc}/README_tpre_en.md | 0 src/README_app_en.md | 0 5 files changed, 7 insertions(+) rename {src => doc}/README_app.md (100%) create mode 100644 doc/README_app_en.md rename {src => doc}/README_tpre.md (100%) rename {src => doc}/README_tpre_en.md (100%) delete mode 100644 src/README_app_en.md 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/src/README_app_en.md b/src/README_app_en.md deleted file mode 100644 index e69de29..0000000 From ebbac117781753b4fc2f1283e89a1ac490d35e7c Mon Sep 17 00:00:00 2001 From: sangge <2251250136@qq.com> Date: Fri, 20 Oct 2023 17:18:27 +0800 Subject: [PATCH 6/6] doc: update doc folder --- README_en.md | 1 + 1 file changed, 1 insertion(+) 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