<s> """<STR_LIT>""" <EOL> from . cryptomath import * <EOL> from . import xmltools <EOL> from . ASN1Parser import ASN1Parser <EOL> from . RSAKey import * <EOL> class Python_RSAKey ( RSAKey ) : <EOL> def __init__ ( self , n = <NUM_LIT:0> , e = <NUM_LIT:0> , d = <NUM_LIT:0> , p = <NUM_LIT:0> , q = <NUM_LIT:0> , dP = <NUM_LIT:0> , dQ = <NUM_LIT:0> , qInv = <NUM_LIT:0> ) : <EOL> if ( n and not e ) or ( e and not n ) : <EOL> raise AssertionError ( ) <EOL> self . n = n <EOL> self . e = e <EOL> self . d = d <EOL> self . p = p <EOL> self . q = q <EOL> self . dP = dP <EOL> self . dQ = dQ <EOL> self . qInv = qInv <EOL> self . blinder = <NUM_LIT:0> <EOL> self . unblinder = <NUM_LIT:0> <EOL> def hasPrivateKey ( self ) : <EOL> return self . d != <NUM_LIT:0> <EOL> def hash ( self ) : <EOL> s = self . writeXMLPublicKey ( '<STR_LIT>' ) <EOL> return hashAndBase64 ( s . strip ( ) ) <EOL> def _rawPrivateKeyOp ( self , m ) : <EOL> if not self . blinder : <EOL> self . unblinder = getRandomNumber ( <NUM_LIT:2> , self . n ) <EOL> self . blinder = powMod ( invMod ( self . unblinder , self . n ) , self . e , <EOL> self . n ) <EOL> m = ( m * self . blinder ) % self . n <EOL> c = self . _rawPrivateKeyOpHelper ( m ) <EOL> c = ( c * self . unblinder ) % self . n <EOL> self . blinder = ( self . blinder * self . blinder ) % self . n <EOL> self . unblinder = ( self . unblinder * self . unblinder ) % self . n <EOL> return c <EOL> def _rawPrivateKeyOpHelper ( self , m ) : <EOL> s1 = powMod ( m , self . dP , self . p ) <EOL> s2 = powMod ( m , self . dQ , self . q ) <EOL> h = ( ( s1 - s2 ) * self . qInv ) % self . p <EOL> c = s2 + self . q * h <EOL> return c <EOL> def _rawPublicKeyOp ( self , c ) : <EOL> m = powMod ( c , self . e , self . n ) <EOL> return m <EOL> def acceptsPassword ( self ) : return False <EOL> def write ( self , indent = '<STR_LIT>' ) : <EOL> if self . d : <EOL> s = indent + '<STR_LIT>' <EOL> else : <EOL> s = indent + '<STR_LIT>' <EOL> s += indent + '<STR_LIT>' % numberToBase64 ( self . n ) <EOL> s += indent + '<STR_LIT>' % numberToBase64 ( self . e ) <EOL> if self . d : <EOL> s += indent + '<STR_LIT>' % numberToBase64 ( self . d ) <EOL> s += indent + '<STR_LIT>' % numberToBase64 ( self . p ) <EOL> s += indent + '<STR_LIT>' % numberToBase64 ( self . q ) <EOL> s += indent + '<STR_LIT>' % numberToBase64 ( self . dP ) <EOL> s += indent + '<STR_LIT>' % numberToBase64 ( self . dQ ) <EOL> s += indent + '<STR_LIT>' % numberToBase64 ( self . qInv ) <EOL> s += indent + '<STR_LIT>' <EOL> else : <EOL> s += indent + '<STR_LIT>' <EOL> if indent != '<STR_LIT>' : <EOL> s += '<STR_LIT:\n>' <EOL> return s <EOL> def writeXMLPublicKey ( self , indent = '<STR_LIT>' ) : <EOL> return Python_RSAKey ( self . n , self . e ) . write ( indent ) <EOL> def generate ( bits ) : <EOL> key = Python_RSAKey ( ) <EOL> p = getRandomPrime ( bits / <NUM_LIT:2> , False ) <EOL> q = getRandomPrime ( bits / <NUM_LIT:2> , False ) <EOL> t = lcm ( p - <NUM_LIT:1> , q - <NUM_LIT:1> ) <EOL> key . n = p * q <EOL> key . e = <NUM_LIT:3> <EOL> key . d = invMod ( key . e , t ) <EOL> key . p = p <EOL> key . q = q <EOL> key . dP = key . d % ( p - <NUM_LIT:1> ) <EOL> key . dQ = key . d % ( q - <NUM_LIT:1> ) <EOL> key . qInv = invMod ( q , p ) <EOL> return key <EOL> generate = staticmethod ( generate ) <EOL> def parsePEM ( s , passwordCallback = None ) : <EOL> """<STR_LIT>""" <EOL> start = s . find ( "<STR_LIT>" ) <EOL> if start != - <NUM_LIT:1> : <EOL> end = s . find ( "<STR_LIT>" ) <EOL> if end == - <NUM_LIT:1> : <EOL> raise SyntaxError ( "<STR_LIT>" ) <EOL> s = s [ start + len ( "<STR_LIT>" ) : end ] <EOL> bytes = base64ToBytes ( s ) <EOL> return Python_RSAKey . _parsePKCS8 ( bytes ) <EOL> else : <EOL> start = s . find ( "<STR_LIT>" ) <EOL> if start != - <NUM_LIT:1> : <EOL> end = s . find ( "<STR_LIT>" ) <EOL> if end == - <NUM_LIT:1> : <EOL> raise SyntaxError ( "<STR_LIT>" ) <EOL> s = s [ start + len ( "<STR_LIT>" ) : end ] <EOL> bytes = base64ToBytes ( s ) <EOL> return Python_RSAKey . _parseSSLeay ( bytes ) <EOL> raise SyntaxError ( "<STR_LIT>" ) <EOL> parsePEM = staticmethod ( parsePEM ) <EOL> def parseXML ( s ) : <EOL> element = xmltools . parseAndStripWhitespace ( s ) <EOL> return Python_RSAKey . _parseXML ( element ) <EOL> parseXML = staticmethod ( parseXML ) <EOL> def _parsePKCS8 ( bytes ) : <EOL> p = ASN1Parser ( bytes ) <EOL> version = p . getChild ( <NUM_LIT:0> ) . value [ <NUM_LIT:0> ] <EOL> if version != <NUM_LIT:0> : <EOL> raise SyntaxError ( "<STR_LIT>" ) <EOL> rsaOID = p . getChild ( <NUM_LIT:1> ) . value <EOL> if list ( rsaOID ) != [ <NUM_LIT:6> , <NUM_LIT:9> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:5> , <NUM_LIT:0> ] : <EOL> raise SyntaxError ( "<STR_LIT>" ) <EOL> privateKeyP = p . getChild ( <NUM_LIT:2> ) <EOL> privateKeyP = ASN1Parser ( privateKeyP . value ) <EOL> return Python_RSAKey . _parseASN1PrivateKey ( privateKeyP ) <EOL> _parsePKCS8 = staticmethod ( _parsePKCS8 ) <EOL> def _parseSSLeay ( bytes ) : <EOL> privateKeyP = ASN1Parser ( bytes ) <EOL> return Python_RSAKey . _parseASN1PrivateKey ( privateKeyP ) <EOL> _parseSSLeay = staticmethod ( _parseSSLeay ) <EOL> def _parseASN1PrivateKey ( privateKeyP ) : <EOL> version = privateKeyP . getChild ( <NUM_LIT:0> ) . value [ <NUM_LIT:0> ] <EOL> if version != <NUM_LIT:0> : <EOL> raise SyntaxError ( "<STR_LIT>" ) <EOL> n = bytesToNumber ( privateKeyP . getChild ( <NUM_LIT:1> ) . value ) <EOL> e = bytesToNumber ( privateKeyP . getChild ( <NUM_LIT:2> ) . value ) <EOL> d = bytesToNumber ( privateKeyP . getChild ( <NUM_LIT:3> ) . value ) <EOL> p = bytesToNumber ( privateKeyP . getChild ( <NUM_LIT:4> ) . value ) <EOL> q = bytesToNumber ( privateKeyP . getChild ( <NUM_LIT:5> ) . value ) <EOL> dP = bytesToNumber ( privateKeyP . getChild ( <NUM_LIT:6> ) . value ) <EOL> dQ = bytesToNumber ( privateKeyP . getChild ( <NUM_LIT:7> ) . value ) <EOL> qInv = bytesToNumber ( privateKeyP . getChild ( <NUM_LIT:8> ) . value ) <EOL> return Python_RSAKey ( n , e , d , p , q , dP , dQ , qInv ) <EOL> _parseASN1PrivateKey = staticmethod ( _parseASN1PrivateKey ) <EOL> def _parseXML ( element ) : <EOL> try : <EOL> xmltools . checkName ( element , "<STR_LIT>" ) <EOL> except SyntaxError : <EOL> xmltools . checkName ( element , "<STR_LIT>" ) <EOL> xmltools . getReqAttribute ( element , "<STR_LIT>" , "<STR_LIT>" ) <EOL> xmltools . checkNoMoreAttributes ( element ) <EOL> n = base64ToNumber ( xmltools . getText ( xmltools . getChild ( element , <NUM_LIT:0> , "<STR_LIT:n>" ) , xmltools . base64RegEx ) ) <EOL> e = base64ToNumber ( xmltools . getText ( xmltools . getChild ( element , <NUM_LIT:1> , "<STR_LIT:e>" ) , xmltools . base64RegEx ) ) <EOL> d = <NUM_LIT:0> <EOL> p = <NUM_LIT:0> <EOL> q = <NUM_LIT:0> <EOL> dP = <NUM_LIT:0> <EOL> dQ = <NUM_LIT:0> <EOL> qInv = <NUM_LIT:0> <EOL> if element . childNodes . length >= <NUM_LIT:3> : <EOL> d = base64ToNumber ( xmltools . getText ( xmltools . getChild ( element , <NUM_LIT:2> , "<STR_LIT:d>" ) , xmltools . base64RegEx ) ) <EOL> p = base64ToNumber ( xmltools . getText ( xmltools . getChild ( element , <NUM_LIT:3> , "<STR_LIT:p>" ) , xmltools . base64RegEx ) ) <EOL> q = base64ToNumber ( xmltools . getText ( xmltools . getChild ( element , <NUM_LIT:4> , "<STR_LIT:q>" ) , xmltools . base64RegEx ) ) <EOL> dP = base64ToNumber ( xmltools . getText ( xmltools . getChild ( element , <NUM_LIT:5> , "<STR_LIT>" ) , xmltools . base64RegEx ) ) <EOL> dQ = base64ToNumber ( xmltools . getText ( xmltools . getChild ( element , <NUM_LIT:6> , "<STR_LIT>" ) , xmltools . base64RegEx ) ) <EOL> qInv = base64ToNumber ( xmltools . getText ( xmltools . getLastChild ( element , <NUM_LIT:7> , "<STR_LIT>" ) , xmltools . base64RegEx ) ) <EOL> return Python_RSAKey ( n , e , d , p , q , dP , dQ , qInv ) <EOL> _parseXML = staticmethod ( _parseXML ) </s>
