chore: encrypt_secret.py

This commit is contained in:
tao.chen
2026-09-02 10:10:41 +08:00
committed by tao.chen
parent 49f69129da
commit 75ef8f32a0
5 changed files with 78 additions and 11 deletions
+24
View File
@@ -0,0 +1,24 @@
"""
@Time :2026/8/24
@Author :tao.chen
"""
import base64
import os
import sys
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
def encrypt(plain_text: str, secret_key: str) -> str:
key_bytes = secret_key.encode("utf-8").ljust(32, b"\0")[:32]
cipher = AESGCM(key_bytes)
nonce = os.urandom(12)
ciphertext = cipher.encrypt(nonce, plain_text.encode("utf-8"), None)
payload = base64.b64encode(nonce + ciphertext).decode("utf-8")
return f"ENC({payload})"
if __name__ == "__main__":
if len(sys.argv) < 3:
print("Usage: python encrypt_secret.py <SECRET_KEY> <PLAIN_TEXT>")
sys.exit(1)
print(encrypt(sys.argv[2], sys.argv[1]))