main #18

Merged
ccyj merged 10 commits from sangge/tpre-python:main into main 2023-10-20 17:59:38 +08:00
10 changed files with 81 additions and 5 deletions

View File

@ -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

View File

@ -1,6 +1,8 @@
FROM python:3.11
COPY src /app
COPY requirements.txt /app/
COPY lib/* /lib/
WORKDIR /app

7
doc/README_app_en.md Normal file
View File

@ -0,0 +1,7 @@
# APP Doc
## Client router
/request_node
get method
pr

View File

@ -1 +1,3 @@
gmssl-python
gmssl-python
fastapi
uvicorn

View File

View File

@ -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)

View File

@ -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)