From 551f957d57cb930bfcfd46e763a6556b2e50ec72 Mon Sep 17 00:00:00 2001 From: Pasha Date: Sun, 17 Dec 2023 19:37:05 +0000 Subject: initial commit --- gpg_websocket.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 gpg_websocket.py (limited to 'gpg_websocket.py') diff --git a/gpg_websocket.py b/gpg_websocket.py new file mode 100644 index 0000000..2a18eb1 --- /dev/null +++ b/gpg_websocket.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python + +# gpg_websocket - websocket server for gpg_over_web +# +# Copyright (C) 2023 Salahuddin +# +# This program is free software: you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation, either version 3 of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along with this +# program. If not, see . + +# +# There are two different gnupg bindings. This script is using python3-gpg +# +# python3-gpg - Python interface to the GPGME GnuPG encryption library (Python 3) +# python3-gnupg - Python wrapper for the GNU Privacy Guard (Python 3.x) + + +import gpg +import asyncio +from websockets.server import serve + +async def gpg_over_web(websocket): + ctx = gpg.Context() + async for message in websocket: + if len(message) == 0: + return "" + try: + plaintext, result, verify_result = ctx.decrypt(message.encode()) + except gpg.errors.GPGMEError as e: + plaintext = "--- gpg error ---".encode() + print(e) + await websocket.send(plaintext.decode("utf-8")) + +async def main(): + async with serve(gpg_over_web, "localhost", 8765): + await asyncio.Future() + +asyncio.run(main()) -- cgit v1.2.1