Skip to content

Commit

Permalink
0.9.55 新增 fernet 加密解密方法
Browse files Browse the repository at this point in the history
  • Loading branch information
zengbin93 committed Jul 11, 2024
1 parent 5f4244b commit 1ba9b42
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 1 deletion.
4 changes: 4 additions & 0 deletions czsc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@

optuna_study,
optuna_good_params,

generate_fernet_key,
fernet_encrypt,
fernet_decrypt,
)

# 交易日历工具
Expand Down
1 change: 1 addition & 0 deletions czsc/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from .oss import AliyunOSS
from .optuna import optuna_study, optuna_good_params
from .events import overlap
from .fernet import generate_fernet_key, fernet_encrypt, fernet_decrypt


sorted_freqs = [
Expand Down
58 changes: 58 additions & 0 deletions czsc/utils/fernet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# -*- coding: utf-8 -*-
"""
author: zengbin93
email: [email protected]
create_dt: 2024/07/11 12:39
describe: Fernet 加密解密
"""
import os
from typing import Union

from cryptography.fernet import Fernet


def generate_fernet_key():
"""生成 Fernet key
等价于:base64.urlsafe_b64encode(os.urandom(32))
"""
key = Fernet.generate_key()
return key.decode()


def fernet_encrypt(data: Union[dict, str], key: str = None) -> str:
"""加密文本/字典
:param data: 需要加密的文本、字典
:param key: Fernet key must be 32 url-safe base64-encoded bytes.
推荐使用 generate_fernet_key() 生成
:return: 加密后的文本
"""
key = key or os.getenv("FERNET_KEY")
cipher_suite = Fernet(key.encode())
encrypted_text = cipher_suite.encrypt(str(data).encode()).decode()
return encrypted_text


def fernet_decrypt(data: str, key: str = None, is_dict=False) -> str:
"""解密文本
:param data: 需要解密的文本
:param key: Fernet key must be 32 url-safe base64-encoded bytes.
推荐使用 generate_fernet_key() 生成
:param is_dict: 是否解密字典数据
:return: 解密后的文本
"""
key = key or os.getenv("FERNET_KEY")
cipher_suite = Fernet(key.encode())
decrypted_text = cipher_suite.decrypt(data.encode()).decode()
return eval(decrypted_text) if is_dict else decrypted_text


def test():
key = generate_fernet_key()
# key = 'HYtUW7y0HOMQySGmOiDHztUGaHC-WnBVh-yqn11Tszw='
text = {"account": "admin", "password": "123456"}
encrypted = fernet_encrypt(text, key)
decrypted = fernet_decrypt(encrypted, key, is_dict=True)
assert text == decrypted, f"{text} != {decrypted}"
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ streamlit
redis
oss2
statsmodels
optuna
optuna
cryptography
10 changes: 10 additions & 0 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ def test_x_round():
assert utils.x_round(1.000342, 5) == 1.00034


def test_fernet():
from czsc.utils.fernet import generate_fernet_key, fernet_encrypt, fernet_decrypt

key = generate_fernet_key()
text = {"account": "admin", "password": "123456"}
encrypted = fernet_encrypt(text, key)
decrypted = fernet_decrypt(encrypted, key, is_dict=True)
assert text == decrypted, f"{text} != {decrypted}"


def test_subtract_fee():
from czsc.utils.stats import subtract_fee

Expand Down

0 comments on commit 1ba9b42

Please sign in to comment.