Last active
February 3, 2023 08:59
-
-
Save nileshtrivedi/96973cdb9f809d43c621ce795a6a400d to your computer and use it in GitHub Desktop.
ed25519 digital signature methods in PostgreSQL via PL/Python
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
CREATE EXTENSION IF NOT EXISTS plpythonu; | |
CREATE FUNCTION py_create_ed25519_keypair () | |
RETURNS varchar[] | |
AS $$ | |
import axolotl_curve25519 as curve | |
import os | |
import base64 | |
randm32 = os.urandom(32) | |
private_key = curve.generatePrivateKey(randm32) | |
public_key = curve.generatePublicKey(private_key) | |
return [base64.b64encode(private_key), base64.b64encode(public_key)] | |
$$ LANGUAGE plpythonu; | |
CREATE FUNCTION py_create_ed25519_signature (message varchar, private_key varchar) | |
RETURNS varchar | |
AS $$ | |
import axolotl_curve25519 as curve | |
import os | |
import base64 | |
randm64 = os.urandom(64) | |
signature = curve.calculateSignature(randm64, base64.b64decode(private_key), message) | |
return base64.b64encode(signature) | |
$$ LANGUAGE plpythonu; | |
CREATE FUNCTION py_verify_ed25519_signature (message varchar, signature varchar, public_key varchar) | |
RETURNS boolean | |
AS $$ | |
import axolotl_curve25519 as curve | |
import base64 | |
valid = (curve.verifySignature(base64.b64decode(public_key), message, base64.b64decode(signature)) == 0) | |
return valid | |
$$ LANGUAGE plpythonu; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment