Package tlslite :: Package utils :: Module pycrypto_rsakey
[hide private]
[frames] | no frames]

Source Code for Module tlslite.utils.pycrypto_rsakey

 1  # Author: Trevor Perrin 
 2  # See the LICENSE file for legal information regarding use of this file. 
 3   
 4  """PyCrypto RSA implementation.""" 
 5   
 6  from .cryptomath import * 
 7   
 8  from .rsakey import * 
 9  from .python_rsakey import Python_RSAKey 
10  from .compat import compatLong 
11   
12  if pycryptoLoaded: 
13   
14      from Crypto.PublicKey import RSA 
15   
16 - class PyCrypto_RSAKey(RSAKey):
17 - def __init__(self, n=0, e=0, d=0, p=0, q=0, dP=0, dQ=0, qInv=0):
18 if not d: 19 self.rsa = RSA.construct((compatLong(n), compatLong(e))) 20 else: 21 self.rsa = RSA.construct((compatLong(n), compatLong(e), 22 compatLong(d), compatLong(p), 23 compatLong(q)))
24
25 - def __getattr__(self, name):
26 return getattr(self.rsa, name)
27
28 - def hasPrivateKey(self):
29 return self.rsa.has_private()
30
31 - def _rawPrivateKeyOp(self, m):
32 c = self.rsa.decrypt((m,)) 33 return c
34
35 - def _rawPublicKeyOp(self, c):
36 m = self.rsa.encrypt(c, None)[0] 37 return m
38
39 - def generate(bits):
40 key = PyCrypto_RSAKey() 41 def f(numBytes): 42 return bytes(getRandomBytes(numBytes))
43 key.rsa = RSA.generate(bits, f) 44 return key
45 generate = staticmethod(generate) 46