chore: encrypt_secret.py
This commit is contained in:
@@ -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]))
|
||||
Reference in New Issue
Block a user