Implement signing for RSA

- mainly for use in `secio` w/ RSA-based identities b/t peers
This commit is contained in:
Alex Stokes 2019-09-04 09:27:57 -07:00
parent 25f504ad35
commit f86ba7283d
No known key found for this signature in database
GPG Key ID: 51CE1721B245C086

View File

@ -1,5 +1,7 @@
from Crypto.Hash import SHA256
import Crypto.PublicKey.RSA as RSA import Crypto.PublicKey.RSA as RSA
from Crypto.PublicKey.RSA import RsaKey from Crypto.PublicKey.RSA import RsaKey
from Crypto.Signature import pkcs1_15
from libp2p.crypto.keys import KeyPair, KeyType, PrivateKey, PublicKey from libp2p.crypto.keys import KeyPair, KeyType, PrivateKey, PublicKey
@ -20,7 +22,12 @@ class RSAPublicKey(PublicKey):
return KeyType.RSA return KeyType.RSA
def verify(self, data: bytes, signature: bytes) -> bool: 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): class RSAPrivateKey(PrivateKey):
@ -39,7 +46,8 @@ class RSAPrivateKey(PrivateKey):
return KeyType.RSA return KeyType.RSA
def sign(self, data: bytes) -> bytes: 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: def get_public_key(self) -> PublicKey:
return RSAPublicKey(self.impl.publickey()) return RSAPublicKey(self.impl.publickey())