2222import abc
2323import sys
2424import typing
25- from optparse import OptionParser
25+ import optparse
2626
2727import rsa
2828import rsa .key
2929import rsa .pkcs1
3030
3131HASH_METHODS = sorted (rsa .pkcs1 .HASH_METHODS .keys ())
32+ Indexable = typing .Union [typing .Tuple , typing .List [str ]]
3233
3334
34- def keygen ():
35+ def keygen () -> None :
3536 """Key generator."""
3637
3738 # Parse the CLI options
38- parser = OptionParser (usage = 'usage: %prog [options] keysize' ,
39+ parser = optparse . OptionParser (usage = 'usage: %prog [options] keysize' ,
3940 description = 'Generates a new RSA keypair of "keysize" bits.' )
4041
4142 parser .add_option ('--pubout' , type = 'string' ,
@@ -104,21 +105,22 @@ class CryptoOperation(metaclass=abc.ABCMeta):
104105
105106 key_class = rsa .PublicKey # type: typing.Type[rsa.key.AbstractKey]
106107
107- def __init__ (self ):
108+ def __init__ (self ) -> None :
108109 self .usage = self .usage % self .__class__ .__dict__
109110 self .input_help = self .input_help % self .__class__ .__dict__
110111 self .output_help = self .output_help % self .__class__ .__dict__
111112
112113 @abc .abstractmethod
113- def perform_operation (self , indata , key , cli_args ):
114+ def perform_operation (self , indata : bytes , key : rsa .key .AbstractKey ,
115+ cli_args : Indexable ):
114116 """Performs the program's operation.
115117
116118 Implement in a subclass.
117119
118120 :returns: the data to write to the output.
119121 """
120122
121- def __call__ (self ):
123+ def __call__ (self ) -> None :
122124 """Runs the program."""
123125
124126 (cli , cli_args ) = self .parse_cli ()
@@ -133,13 +135,13 @@ def __call__(self):
133135 if self .has_output :
134136 self .write_outfile (outdata , cli .output )
135137
136- def parse_cli (self ):
138+ def parse_cli (self ) -> typing . Tuple [ optparse . Values , typing . List [ str ]] :
137139 """Parse the CLI options
138140
139141 :returns: (cli_opts, cli_args)
140142 """
141143
142- parser = OptionParser (usage = self .usage , description = self .description )
144+ parser = optparse . OptionParser (usage = self .usage , description = self .description )
143145
144146 parser .add_option ('-i' , '--input' , type = 'string' , help = self .input_help )
145147
@@ -158,7 +160,7 @@ def parse_cli(self):
158160
159161 return cli , cli_args
160162
161- def read_key (self , filename , keyform ) :
163+ def read_key (self , filename : str , keyform : str ) -> rsa . key . AbstractKey :
162164 """Reads a public or private key."""
163165
164166 print ('Reading %s key from %s' % (self .keyname , filename ), file = sys .stderr )
@@ -167,7 +169,7 @@ def read_key(self, filename, keyform):
167169
168170 return self .key_class .load_pkcs1 (keydata , keyform )
169171
170- def read_infile (self , inname ) :
172+ def read_infile (self , inname : str ) -> bytes :
171173 """Read the input file"""
172174
173175 if inname :
@@ -176,9 +178,9 @@ def read_infile(self, inname):
176178 return infile .read ()
177179
178180 print ('Reading input from stdin' , file = sys .stderr )
179- return sys .stdin .read ()
181+ return sys .stdin .buffer . read ()
180182
181- def write_outfile (self , outdata , outname ) :
183+ def write_outfile (self , outdata : bytes , outname : str ) -> None :
182184 """Write the output file"""
183185
184186 if outname :
@@ -200,9 +202,10 @@ class EncryptOperation(CryptoOperation):
200202 operation_past = 'encrypted'
201203 operation_progressive = 'encrypting'
202204
203- def perform_operation (self , indata , pub_key , cli_args = None ):
205+ def perform_operation (self , indata : bytes , pub_key : rsa .key .AbstractKey ,
206+ cli_args : Indexable = ()):
204207 """Encrypts files."""
205-
208+ assert isinstance ( pub_key , rsa . key . PublicKey )
206209 return rsa .encrypt (indata , pub_key )
207210
208211
@@ -217,9 +220,10 @@ class DecryptOperation(CryptoOperation):
217220 operation_progressive = 'decrypting'
218221 key_class = rsa .PrivateKey
219222
220- def perform_operation (self , indata , priv_key , cli_args = None ):
223+ def perform_operation (self , indata : bytes , priv_key : rsa .key .AbstractKey ,
224+ cli_args : Indexable = ()):
221225 """Decrypts files."""
222-
226+ assert isinstance ( priv_key , rsa . key . PrivateKey )
223227 return rsa .decrypt (indata , priv_key )
224228
225229
@@ -239,8 +243,10 @@ class SignOperation(CryptoOperation):
239243 output_help = ('Name of the file to write the signature to. Written '
240244 'to stdout if this option is not present.' )
241245
242- def perform_operation (self , indata , priv_key , cli_args ):
246+ def perform_operation (self , indata : bytes , priv_key : rsa .key .AbstractKey ,
247+ cli_args : Indexable ):
243248 """Signs files."""
249+ assert isinstance (priv_key , rsa .key .PrivateKey )
244250
245251 hash_method = cli_args [1 ]
246252 if hash_method not in HASH_METHODS :
@@ -264,8 +270,10 @@ class VerifyOperation(CryptoOperation):
264270 expected_cli_args = 2
265271 has_output = False
266272
267- def perform_operation (self , indata , pub_key , cli_args ):
273+ def perform_operation (self , indata : bytes , pub_key : rsa .key .AbstractKey ,
274+ cli_args : Indexable ):
268275 """Verifies files."""
276+ assert isinstance (pub_key , rsa .key .PublicKey )
269277
270278 signature_file = cli_args [1 ]
271279
0 commit comments