Skip to content

Commit f2241db

Browse files
committed
perf: 优化magic-dash-pro模板公钥、私钥加载效率
1 parent 5f0855e commit f2241db

File tree

2 files changed

+8
-21
lines changed

2 files changed

+8
-21
lines changed

magic_dash/templates/magic-dash-pro/utils/crypto_utils.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,14 @@
1313
from configs import BaseConfig
1414

1515

16-
def load_rsa_public_key():
17-
"""加载RSA公钥文件内容
16+
# 模块级别加载RSA公钥和私钥(只加载一次)
17+
with open(BaseConfig.rsa_public_key_path, "r", encoding="utf-8") as f:
18+
rsa_public_key = f.read()
1819

19-
从项目根目录读取public_key.pem文件
20-
21-
Returns:
22-
str: 公钥PEM格式字符串
23-
None: 加载失败时返回None
24-
"""
25-
26-
with open(BaseConfig.rsa_public_key_path, "r", encoding="utf-8") as f:
27-
return f.read()
20+
with open(BaseConfig.rsa_private_key_path, "rb") as f:
21+
private_key = serialization.load_pem_private_key(
22+
f.read(), password=None, backend=default_backend()
23+
)
2824

2925

3026
def decrypt_password(encrypted_base64: str) -> str:
@@ -49,12 +45,6 @@ def decrypt_password(encrypted_base64: str) -> str:
4945
return None
5046

5147
try:
52-
# 读取私钥
53-
with open(BaseConfig.rsa_private_key_path, "rb") as f:
54-
private_key = serialization.load_pem_private_key(
55-
f.read(), password=None, backend=default_backend()
56-
)
57-
5848
# Base64解码加密数据
5949
encrypted_data = base64.b64decode(encrypted_base64)
6050

magic_dash/templates/magic-dash-pro/views/login.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from feffery_dash_utils.style_utils import style
55

66
from configs import BaseConfig, LayoutConfig
7-
from utils.crypto_utils import load_rsa_public_key
7+
from utils.crypto_utils import rsa_public_key
88

99
# 令绑定的回调函数子模块生效
1010
import callbacks.login_c # noqa: F401
@@ -13,9 +13,6 @@
1313
def render():
1414
"""渲染用户登录页面"""
1515

16-
# 加载RSA公钥
17-
rsa_public_key = load_rsa_public_key()
18-
1916
return fac.AntdRow(
2017
[
2118
# 左侧半边

0 commit comments

Comments
 (0)