<s> """<STR_LIT>""" <EOL> from . cryptomath import * <EOL> class RSAKey : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , n = <NUM_LIT:0> , e = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def __len__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return numBits ( self . n ) <EOL> def hasPrivateKey ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def hash ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def getSigningAlgorithm ( self ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" <EOL> def hashAndSign ( self , bytes ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( bytes , type ( "<STR_LIT>" ) ) : <EOL> bytes = bytesToString ( bytes ) <EOL> hashBytes = stringToBytes ( sha1 ( bytes ) . digest ( ) ) <EOL> prefixedHashBytes = self . _addPKCS1SHA1Prefix ( hashBytes ) <EOL> sigBytes = self . sign ( prefixedHashBytes ) <EOL> return sigBytes <EOL> def hashAndVerify ( self , sigBytes , bytes ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( bytes , type ( "<STR_LIT>" ) ) : <EOL> bytes = bytesToString ( bytes ) <EOL> hashBytes = stringToBytes ( sha1 ( bytes ) . digest ( ) ) <EOL> prefixedHashBytes = self . _addPKCS1SHA1Prefix ( hashBytes ) <EOL> return self . verify ( sigBytes , prefixedHashBytes ) <EOL> def sign ( self , bytes ) : <EOL> """<STR_LIT>""" <EOL> if not self . hasPrivateKey ( ) : <EOL> raise AssertionError ( ) <EOL> paddedBytes = self . _addPKCS1Padding ( bytes , <NUM_LIT:1> ) <EOL> m = bytesToNumber ( paddedBytes ) <EOL> if m >= self . n : <EOL> raise ValueError ( ) <EOL> c = self . _rawPrivateKeyOp ( m ) <EOL> sigBytes = numberToBytes ( c ) <EOL> return sigBytes <EOL> def verify ( self , sigBytes , bytes ) : <EOL> """<STR_LIT>""" <EOL> paddedBytes = self . _addPKCS1Padding ( bytes , <NUM_LIT:1> ) <EOL> c = bytesToNumber ( sigBytes ) <EOL> if c >= self . n : <EOL> return False <EOL> m = self . _rawPublicKeyOp ( c ) <EOL> checkBytes = numberToBytes ( m ) <EOL> return checkBytes == paddedBytes <EOL> def encrypt ( self , bytes ) : <EOL> """<STR_LIT>""" <EOL> paddedBytes = self . _addPKCS1Padding ( bytes , <NUM_LIT:2> ) <EOL> m = bytesToNumber ( paddedBytes ) <EOL> if m >= self . n : <EOL> raise ValueError ( ) <EOL> c = self . _rawPublicKeyOp ( m ) <EOL> encBytes = numberToBytes ( c ) <EOL> return encBytes <EOL> def decrypt ( self , encBytes ) : <EOL> """<STR_LIT>""" <EOL> if not self . hasPrivateKey ( ) : <EOL> raise AssertionError ( ) <EOL> c = bytesToNumber ( encBytes ) <EOL> if c >= self . n : <EOL> return None <EOL> m = self . _rawPrivateKeyOp ( c ) <EOL> decBytes = numberToBytes ( m ) <EOL> if ( len ( decBytes ) != numBytes ( self . n ) - <NUM_LIT:1> ) : <EOL> return None <EOL> if decBytes [ <NUM_LIT:0> ] != <NUM_LIT:2> : <EOL> return None <EOL> for x in range ( len ( decBytes ) - <NUM_LIT:1> ) : <EOL> if decBytes [ x ] == <NUM_LIT:0> : <EOL> break <EOL> else : <EOL> return None <EOL> return decBytes [ x + <NUM_LIT:1> : ] <EOL> def _rawPrivateKeyOp ( self , m ) : <EOL> raise NotImplementedError ( ) <EOL> def _rawPublicKeyOp ( self , c ) : <EOL> raise NotImplementedError ( ) <EOL> def acceptsPassword ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def write ( self , password = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def writeXMLPublicKey ( self , indent = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> return Python_RSAKey ( self . n , self . e ) . write ( indent ) <EOL> def generate ( bits ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> generate = staticmethod ( generate ) <EOL> def _addPKCS1SHA1Prefix ( self , bytes ) : <EOL> prefixBytes = createByteArraySequence ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:9> , <NUM_LIT:6> , <NUM_LIT:5> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT> , <NUM_LIT:5> , <NUM_LIT:0> , <NUM_LIT:4> , <NUM_LIT:20> ] ) <EOL> prefixedBytes = prefixBytes + bytes <EOL> return prefixedBytes <EOL> def _addPKCS1Padding ( self , bytes , blockType ) : <EOL> padLength = ( numBytes ( self . n ) - ( len ( bytes ) + <NUM_LIT:3> ) ) <EOL> if blockType == <NUM_LIT:1> : <EOL> pad = [ <NUM_LIT> ] * padLength <EOL> elif blockType == <NUM_LIT:2> : <EOL> pad = createByteArraySequence ( [ ] ) <EOL> while len ( pad ) < padLength : <EOL> padBytes = getRandomBytes ( padLength * <NUM_LIT:2> ) <EOL> pad = [ b for b in padBytes if b != <NUM_LIT:0> ] <EOL> pad = pad [ : padLength ] <EOL> else : <EOL> raise AssertionError ( ) <EOL> padding = createByteArraySequence ( [ blockType ] + pad + [ <NUM_LIT:0> ] ) <EOL> paddedBytes = padding + bytes <EOL> return paddedBytes </s>
