From f86ba7283dd12946ea0cf48e0712b0884ee8366b Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Wed, 4 Sep 2019 09:27:57 -0700 Subject: [PATCH] Implement signing for RSA - mainly for use in `secio` w/ RSA-based identities b/t peers --- libp2p/crypto/rsa.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/libp2p/crypto/rsa.py b/libp2p/crypto/rsa.py index ed8c215b..82644469 100644 --- a/libp2p/crypto/rsa.py +++ b/libp2p/crypto/rsa.py @@ -1,5 +1,7 @@ +from Crypto.Hash import SHA256 import Crypto.PublicKey.RSA as RSA from Crypto.PublicKey.RSA import RsaKey +from Crypto.Signature import pkcs1_15 from libp2p.crypto.keys import KeyPair, KeyType, PrivateKey, PublicKey @@ -20,7 +22,12 @@ class RSAPublicKey(PublicKey): return KeyType.RSA def verify(self, data: bytes, signature: bytes) -> bool: - raise NotImplementedError + h = SHA256.new(data) + try: + pkcs1_15.new(self.impl).verify(h, signature) + except (ValueError, TypeError): + return False + return True class RSAPrivateKey(PrivateKey): @@ -39,7 +46,8 @@ class RSAPrivateKey(PrivateKey): return KeyType.RSA def sign(self, data: bytes) -> bytes: - raise NotImplementedError + h = SHA256.new(data) + return pkcs1_15.new(self.impl).sign(h) def get_public_key(self) -> PublicKey: return RSAPublicKey(self.impl.publickey())