Skip to content

Instantly share code, notes, and snippets.

@goors
Created June 1, 2024 16:30
Show Gist options
  • Save goors/6fa1e7e8b2d76742fcbd820e4e00add4 to your computer and use it in GitHub Desktop.
Save goors/6fa1e7e8b2d76742fcbd820e4e00add4 to your computer and use it in GitHub Desktop.
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