Created
June 1, 2024 16:30
-
-
Save goors/6fa1e7e8b2d76742fcbd820e4e00add4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from base64 import b64encode | |
from nacl import encoding, public | |
def encrypt(public_key: str, secret_value: str) -> str: | |
"""Encrypt a Unicode string using the public key.""" | |
public_key = public.PublicKey(public_key.encode("utf-8"), encoding.Base64Encoder()) | |
sealed_box = public.SealedBox(public_key) | |
encrypted = sealed_box.encrypt(secret_value.encode("utf-8")) | |
return b64encode(encrypted).decode("utf-8") | |
# Extract command-line arguments | |
if len(sys.argv) != 3: | |
print("Usage: python script.py <public_key> <secret_file>") | |
sys.exit(1) | |
public_key = sys.argv[1] | |
secret_file = sys.argv[2] | |
# Read the secret value from the file | |
try: | |
with open(secret_file, "r") as file: | |
secret_value = file.read().strip() | |
except FileNotFoundError: | |
print(f"Error: File '{secret_file}' not found.") | |
sys.exit(1) | |
# Encrypt the secret | |
encrypted_secret = encrypt(public_key, secret_value) | |
print(encrypted_secret) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment