> On Feb 8, 2016, at 10:52 PM, Daniel Monteiro Basso <dan...@basso.inf.br> 
> wrote:
> 
> On Tue, 2016-02-09 at 03:11 +0000, Pitucha, Stanislaw Izaak wrote:
>> You probably need to use hazmat, but it has a pretty nice API.
>> There's a short example in the docs:
>> https://cryptography.io/en/latest/hazmat/primitives/asymmetric/rsa/#s
>> igning
> 
> And to build the key he should do something like this:
> 
> from cryptography.hazmat.primitives.asymmetric import rsa
> from cryptography.hazmat.backends import default_backend
> 
> def generate_key(modulus_b64, exp_b64):
>     [...]
>     pub_num = rsa.RSAPublicNumbers(exp, mod)
>     return pub_num.public_key(default_backend())


Thanks, I think the fog is clearing...


from cryptography.exceptions import InvalidSignature
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import rsa, padding

def generate_key(exp_b64, modulus_b64):
    exp = int(b64decode(exp_b64).encode('hex'), 16)
    mod = int(b64decode(modulus_b64).encode('hex'), 16)
    pub_num = rsa.RSAPublicNumbers(exp, mod)
    return pub_num.public_key(default_backend())

def verify(expected_proof, signed_proof, public_key):
    verifier = public_key.verifier(
       signed_proof, padding.PKCS1v15(), hashes.SHA256())
    verifier.update(expected_proof)
    try:
        verifier.verify()
    except InvalidSignature:
        return False
    return True

_______________________________________________
Cryptography-dev mailing list
Cryptography-dev@python.org
https://mail.python.org/mailman/listinfo/cryptography-dev

Reply via email to