Merge pull request 'main' (#16) from dqy/mimajingsai:main into main

Reviewed-on: sangge/mimajingsai#16
This commit is contained in:
sangge 2023-10-23 14:10:12 +08:00
commit 063d13a405
2 changed files with 7 additions and 12 deletions

View File

@ -2,11 +2,9 @@ from fastapi import FastAPI
from fastapi.encoders import jsonable_encoder from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse from fastapi.responses import JSONResponse
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
from typing import Tuple, Callable
import sqlite3 import sqlite3
import asyncio import asyncio
import time import time
import random
@asynccontextmanager @asynccontextmanager
async def lifespan(app: FastAPI): async def lifespan(app: FastAPI):
@ -28,7 +26,7 @@ cursor.execute('''CREATE TABLE IF NOT EXISTS nodes (
)''') )''')
def init(): def init():
task = asyncio.create_task(receive_heartbeat_internal()) asyncio.create_task(receive_heartbeat_internal())
def clean_env(): def clean_env():
# 关闭游标和连接 # 关闭游标和连接
@ -93,16 +91,13 @@ async def receive_heartbeat(ip: str):
cursor.execute("UPDATE nodes SET last_heartbeat = ? WHERE ip = ?", (time.time(), ip)) cursor.execute("UPDATE nodes SET last_heartbeat = ? WHERE ip = ?", (time.time(), ip))
return {"status": "received"} return {"status": "received"}
async def receive_heartbeat_internal() -> int: async def receive_heartbeat_internal():
while 1: while 1:
print('successful delete1') timeout = 70
timeout = 10
# 删除超时的节点 # 删除超时的节点
cursor.execute("DELETE FROM nodes WHERE last_heartbeat < ?", (time.time() - timeout,)) cursor.execute("DELETE FROM nodes WHERE last_heartbeat < ?", (time.time() - timeout,))
conn.commit() conn.commit()
print('successful delete')
await asyncio.sleep(timeout) await asyncio.sleep(timeout)
return 1
@app.get("/server/send_nodes_list") @app.get("/server/send_nodes_list")
async def send_nodes_list(count: int) -> JSONResponse: async def send_nodes_list(count: int) -> JSONResponse:

View File

@ -272,7 +272,7 @@ def f(x: int, f_modulus: list, T: int) -> int:
return res return res
def GenerateReKey(sk_A: int, pk_B: point, N: int, T: int) -> list: def GenerateReKey(sk_A: int, pk_B: point, N: int, T: int, id_tuple: Tuple[int,...]) -> list:
""" """
param: param:
skA, pkB, N(节点总数), T(阈值) skA, pkB, N(节点总数), T(阈值)
@ -288,7 +288,7 @@ def GenerateReKey(sk_A: int, pk_B: point, N: int, T: int) -> list:
# d是Bob的密钥对与临时密钥对的非交互式Diffie-Hellman密钥交换的结果 # d是Bob的密钥对与临时密钥对的非交互式Diffie-Hellman密钥交换的结果
d = hash3((X_A, pk_B, multiply(pk_B, x_A))) d = hash3((X_A, pk_B, multiply(pk_B, x_A)))
# 计算多项式系数, 确定代理节点的ID(一个点) # 计算多项式系数
f_modulus = [] f_modulus = []
# 计算f0 # 计算f0
# f0 = (sk_A * inv(d, G.P)) % G.P # f0 = (sk_A * inv(d, G.P)) % G.P
@ -305,10 +305,10 @@ def GenerateReKey(sk_A: int, pk_B: point, N: int, T: int) -> list:
for i in range(N): for i in range(N):
y = random.randint(0, sm2p256v1.N - 1) y = random.randint(0, sm2p256v1.N - 1)
Y = multiply(g, y) Y = multiply(g, y)
s_x = hash5(i, D) # id需要设置 s_x = hash5(id_tuple[i], D) # id需要设置
r_k = f(s_x, f_modulus, T) r_k = f(s_x, f_modulus, T)
U1 = multiply(U, r_k) U1 = multiply(U, r_k)
kFrag = (i, r_k, X_A, U1) kFrag = (id_tuple[i], r_k, X_A, U1)
KF.append(kFrag) KF.append(kFrag)
return KF return KF