<s> """<STR_LIT>""" <EOL> import sys , os , getpass , socket <EOL> if getpass . getpass == getpass . unix_getpass : <EOL> try : <EOL> import termios <EOL> termios . tcgetattr , termios . tcsetattr <EOL> except ( ImportError , AttributeError ) : <EOL> sys . modules [ '<STR_LIT>' ] = None <EOL> reload ( getpass ) <EOL> from twisted . conch . ssh import keys <EOL> from twisted . python import filepath , log , usage , randbytes <EOL> class GeneralOptions ( usage . Options ) : <EOL> synopsis = """<STR_LIT>""" <EOL> longdesc = "<STR_LIT>" <EOL> optParameters = [ [ '<STR_LIT>' , '<STR_LIT:b>' , <NUM_LIT> , '<STR_LIT>' ] , <EOL> [ '<STR_LIT:filename>' , '<STR_LIT:f>' , None , '<STR_LIT>' ] , <EOL> [ '<STR_LIT:type>' , '<STR_LIT:t>' , None , '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:C>' , None , '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:N>' , None , '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:P>' , None , '<STR_LIT>' ] ] <EOL> optFlags = [ [ '<STR_LIT>' , '<STR_LIT:l>' , '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:p>' , '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:q>' , '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:y>' , '<STR_LIT>' ] ] <EOL> zsh_actions = { "<STR_LIT:type>" : "<STR_LIT>" } <EOL> def run ( ) : <EOL> options = GeneralOptions ( ) <EOL> try : <EOL> options . parseOptions ( sys . argv [ <NUM_LIT:1> : ] ) <EOL> except usage . UsageError , u : <EOL> print '<STR_LIT>' % u <EOL> options . opt_help ( ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> log . discardLogs ( ) <EOL> log . deferr = handleError <EOL> if options [ '<STR_LIT:type>' ] : <EOL> if options [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> generateRSAkey ( options ) <EOL> elif options [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> generateDSAkey ( options ) <EOL> else : <EOL> sys . exit ( '<STR_LIT>' % options [ '<STR_LIT:type>' ] ) <EOL> elif options [ '<STR_LIT>' ] : <EOL> printFingerprint ( options ) <EOL> elif options [ '<STR_LIT>' ] : <EOL> changePassPhrase ( options ) <EOL> elif options [ '<STR_LIT>' ] : <EOL> displayPublicKey ( options ) <EOL> else : <EOL> options . opt_help ( ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def handleError ( ) : <EOL> from twisted . python import failure <EOL> global exitStatus <EOL> exitStatus = <NUM_LIT:2> <EOL> log . err ( failure . Failure ( ) ) <EOL> reactor . stop ( ) <EOL> raise <EOL> def generateRSAkey ( options ) : <EOL> from Crypto . PublicKey import RSA <EOL> print '<STR_LIT>' <EOL> key = RSA . generate ( int ( options [ '<STR_LIT>' ] ) , randbytes . secureRandom ) <EOL> _saveKey ( key , options ) <EOL> def generateDSAkey ( options ) : <EOL> from Crypto . PublicKey import DSA <EOL> print '<STR_LIT>' <EOL> key = DSA . generate ( int ( options [ '<STR_LIT>' ] ) , randbytes . secureRandom ) <EOL> _saveKey ( key , options ) <EOL> def printFingerprint ( options ) : <EOL> if not options [ '<STR_LIT:filename>' ] : <EOL> filename = os . path . expanduser ( '<STR_LIT>' ) <EOL> options [ '<STR_LIT:filename>' ] = raw_input ( '<STR_LIT>' % filename ) <EOL> if os . path . exists ( options [ '<STR_LIT:filename>' ] + '<STR_LIT>' ) : <EOL> options [ '<STR_LIT:filename>' ] += '<STR_LIT>' <EOL> try : <EOL> key = keys . Key . fromFile ( options [ '<STR_LIT:filename>' ] ) <EOL> obj = key . keyObject <EOL> string = key . blob ( ) <EOL> print '<STR_LIT>' % ( <EOL> obj . size ( ) + <NUM_LIT:1> , <EOL> key . fingerprint ( ) , <EOL> os . path . basename ( options [ '<STR_LIT:filename>' ] ) ) <EOL> except : <EOL> sys . exit ( '<STR_LIT>' ) <EOL> def changePassPhrase ( options ) : <EOL> if not options [ '<STR_LIT:filename>' ] : <EOL> filename = os . path . expanduser ( '<STR_LIT>' ) <EOL> options [ '<STR_LIT:filename>' ] = raw_input ( '<STR_LIT>' % filename ) <EOL> try : <EOL> key = keys . Key . fromFile ( options [ '<STR_LIT:filename>' ] ) . keyObject <EOL> except keys . BadKeyError , e : <EOL> if e . args [ <NUM_LIT:0> ] != '<STR_LIT>' : <EOL> raise <EOL> else : <EOL> if not options [ '<STR_LIT>' ] : <EOL> options [ '<STR_LIT>' ] = getpass . getpass ( '<STR_LIT>' ) <EOL> key = keys . Key . fromFile ( <EOL> options [ '<STR_LIT:filename>' ] , passphrase = options [ '<STR_LIT>' ] ) . keyObject <EOL> if not options [ '<STR_LIT>' ] : <EOL> while <NUM_LIT:1> : <EOL> p1 = getpass . getpass ( '<STR_LIT>' ) <EOL> p2 = getpass . getpass ( '<STR_LIT>' ) <EOL> if p1 == p2 : <EOL> break <EOL> print '<STR_LIT>' <EOL> options [ '<STR_LIT>' ] = p1 <EOL> open ( options [ '<STR_LIT:filename>' ] , '<STR_LIT:w>' ) . write ( <EOL> keys . Key ( key ) . toString ( passphrase = options [ '<STR_LIT>' ] ) ) <EOL> print '<STR_LIT>' <EOL> def displayPublicKey ( options ) : <EOL> if not options [ '<STR_LIT:filename>' ] : <EOL> filename = os . path . expanduser ( '<STR_LIT>' ) <EOL> options [ '<STR_LIT:filename>' ] = raw_input ( '<STR_LIT>' % filename ) <EOL> try : <EOL> key = keys . Key . fromFile ( options [ '<STR_LIT:filename>' ] ) . keyObject <EOL> except keys . BadKeyError , e : <EOL> if e . args [ <NUM_LIT:0> ] != '<STR_LIT>' : <EOL> raise <EOL> else : <EOL> if not options [ '<STR_LIT>' ] : <EOL> options [ '<STR_LIT>' ] = getpass . getpass ( '<STR_LIT>' ) <EOL> key = keys . Key . fromFile ( <EOL> options [ '<STR_LIT:filename>' ] , passphrase = options [ '<STR_LIT>' ] ) . keyObject <EOL> print keys . Key ( key ) . public ( ) . toString ( ) <EOL> def _saveKey ( key , options ) : <EOL> if not options [ '<STR_LIT:filename>' ] : <EOL> kind = keys . objectType ( key ) <EOL> kind = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } [ kind ] <EOL> filename = os . path . expanduser ( '<STR_LIT>' % kind ) <EOL> options [ '<STR_LIT:filename>' ] = raw_input ( '<STR_LIT>' % filename ) . strip ( ) or filename <EOL> if os . path . exists ( options [ '<STR_LIT:filename>' ] ) : <EOL> print '<STR_LIT>' % options [ '<STR_LIT:filename>' ] <EOL> yn = raw_input ( '<STR_LIT>' ) <EOL> if yn [ <NUM_LIT:0> ] . lower ( ) != '<STR_LIT:y>' : <EOL> sys . exit ( ) <EOL> if not options [ '<STR_LIT>' ] : <EOL> while <NUM_LIT:1> : <EOL> p1 = getpass . getpass ( '<STR_LIT>' ) <EOL> p2 = getpass . getpass ( '<STR_LIT>' ) <EOL> if p1 == p2 : <EOL> break <EOL> print '<STR_LIT>' <EOL> options [ '<STR_LIT>' ] = p1 <EOL> keyObj = keys . Key ( key ) <EOL> comment = '<STR_LIT>' % ( getpass . getuser ( ) , socket . gethostname ( ) ) <EOL> filepath . FilePath ( options [ '<STR_LIT:filename>' ] ) . setContent ( <EOL> keyObj . toString ( '<STR_LIT>' , options [ '<STR_LIT>' ] ) ) <EOL> os . chmod ( options [ '<STR_LIT:filename>' ] , <NUM_LIT> ) <EOL> filepath . FilePath ( options [ '<STR_LIT:filename>' ] + '<STR_LIT>' ) . setContent ( <EOL> keyObj . public ( ) . toString ( '<STR_LIT>' , comment ) ) <EOL> print '<STR_LIT>' % options [ '<STR_LIT:filename>' ] <EOL> print '<STR_LIT>' % options [ '<STR_LIT:filename>' ] <EOL> print '<STR_LIT>' <EOL> print keyObj . fingerprint ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> run ( ) </s>
