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   
11  if pycryptoLoaded: 
12   
13      from Crypto.PublicKey import RSA 
14   
15 - class PyCrypto_RSAKey(RSAKey):
16 - def __init__(self, n=0, e=0, d=0, p=0, q=0, dP=0, dQ=0, qInv=0):
17 if not d: 18 self.rsa = RSA.construct( (n, e) ) 19 else: 20 self.rsa = RSA.construct( (n, e, d, p, q) )
21
22 - def __getattr__(self, name):
23 return getattr(self.rsa, name)
24
25 - def hasPrivateKey(self):
26 return self.rsa.has_private()
27
28 - def _rawPrivateKeyOp(self, m):
29 s = numberToString(m) 30 byteLength = numBytes(self.n) 31 if len(s)== byteLength: 32 pass 33 elif len(s) == byteLength-1: 34 s = '\0' + s 35 else: 36 raise AssertionError() 37 c = stringToNumber(self.rsa.decrypt((s,))) 38 return c
39
40 - def _rawPublicKeyOp(self, c):
41 s = numberToString(c) 42 byteLength = numBytes(self.n) 43 if len(s)== byteLength: 44 pass 45 elif len(s) == byteLength-1: 46 s = '\0' + s 47 else: 48 raise AssertionError() 49 m = stringToNumber(self.rsa.encrypt(s, None)[0]) 50 return m
51
52 - def generate(bits):
53 key = PyCrypto_RSAKey() 54 def f(numBytes): 55 return bytesToString(getRandomBytes(numBytes))
56 key.rsa = RSA.generate(bits, f) 57 return key
58 generate = staticmethod(generate) 59