Skip to content

Instantly share code, notes, and snippets.

@hasandiwan
Last active January 5, 2022 08:26
Show Gist options
  • Save hasandiwan/bb5522f6ad9921ae178c10dda9919698 to your computer and use it in GitHub Desktop.
Save hasandiwan/bb5522f6ad9921ae178c10dda9919698 to your computer and use it in GitHub Desktop.
Python script to sign the contents and verify PGP-signed content on your Windows/Mac/Linux clipboard
import argparse
import os
import sys
import gnupg
import pyperclip
parser = argparse.ArgumentParser(
description="GPG clearsign whatever is on your clipboard and copy the signed data back"
)
parser.add_argument(
"--verbose",
action="store_true",
help="be verbose, show additional output -- on STDERR -- to not interfere with function",
)
parser.add_argument(
"--gpg",
action="store",
type=str,
help='Location of GPG (1.4.x and 2.x supported), defaults to "/usr/local/opt/gpg2/bin/gpg"',
)
parser.add_argument(
"--key",
action="store",
type=str,
help="Key id to use, defaults to first in your secret keyring",
)
parser.add_argument(
"--passphrase", action="store", type=str, help="Passphrase for signing"
)
parsed = parser.parse_args()
if not parsed.gpg:
parsed.gpg = "/usr/local/opt/gpg2/bin/gpg"
if not parsed.key:
parsed.key = None
message = pyperclip.paste()
os.environ["GPG_TTY"] = os.ttyname(sys.stdout.fileno())
if not message:
print("Must have text on the clipboard to use", file=sys.stderr)
sys.exit()
gpg = gnupg.GPG(gpgbinary=parsed.gpg, use_agent=True, verbose=parsed.verbose)
gpg.encoding = "utf-8"
if message.startswith("-----BEGIN PGP SIGNED MESSAGE-----"):
verification = gpg.verify(message)
if verification:
print("verified")
else:
print("Does not verify")
else:
signed_data = gpg.sign(
message, clearsign=True, keyid=parsed.key, passphrase=parsed.passphrase
)
if parsed.verbose:
print(signed_data, file=sys.stderr)
pyperclip.copy(str(signed_data))
@hasandiwan
Copy link
Author

Made verbose default to False.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment