Last active
February 26, 2022 23:27
-
-
Save m-ocean-it/cda33421fce98c7067eff5cb7f817fd0 to your computer and use it in GitHub Desktop.
Encrypt several files individually with GPG
This file contains hidden or 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
#!/bin/python3 | |
''' | |
Encrypt several files individually with GPG. | |
NOTE: The script moves all original files to ./trash | |
Dependencies: | |
- gpg (GnuPG) 2.2.19 | |
- libgcrypt 1.8.5 | |
''' | |
import subprocess | |
import os | |
from getpass import getpass | |
import datetime | |
mode = input('Encrypt or decrypt? (e/d) ').lower().strip() | |
if mode not in ('e', 'd'): | |
raise ValueError('Incorrect mode.') | |
DIR_PATH = input('Directory path: ') | |
passphrase = getpass('Passphrase:') | |
if mode == 'e': | |
COMMAND = f'echo "{passphrase}" | gpg --batch --passphrase-fd 0 -c --no-symkey-cache --cipher-algo AES256' | |
else: | |
COMMAND = f'echo "{passphrase}" | gpg --batch --passphrase-fd 0' | |
file_paths = [os.path.join(DIR_PATH, file_path) | |
for file_path in os.listdir(DIR_PATH)] | |
date_str = datetime.datetime.now().strftime(r'%Y-%m-%d_%Hh%Mm%Ss') | |
for i, path in enumerate(file_paths): | |
subprocess.run(f'{COMMAND} {path}', check=True, shell=True) | |
if i == 0: | |
subprocess.run(f'mkdir -p trash/{date_str}', check=True, shell=True) | |
subprocess.run(f'mv {path} trash/{date_str}/', check=True, shell=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
gpg (GnuPG) 2.2.19
libgcrypt 1.8.5