1111"""
1212# pylint: disable=invalid-name, unnecessary-lambda, unnecessary-lambda-assignment, missing-docstring
1313
14+ try :
15+ from typing import Dict , List , Optional , Tuple , Union
16+ except ImportError :
17+ # suppress because typing does not exist on circuitpython
18+ pass
19+
20+
1421# SHA Block size and message digest sizes, in bytes.
1522SHA_BLOCKSIZE = 64
1623SHA_DIGESTSIZE = 32
@@ -42,7 +49,7 @@ def new_shaobject():
4249Gamma1 = lambda x : (S (x , 17 ) ^ S (x , 19 ) ^ R (x , 10 ))
4350
4451# pylint: disable=too-many-statements
45- def sha_transform (sha_info ) :
52+ def sha_transform (sha_info : Dict [ str , Union [ List [ int ], int ]]) -> None :
4653 W = []
4754
4855 d = sha_info ["data" ]
@@ -59,7 +66,9 @@ def sha_transform(sha_info):
5966 ss = sha_info ["digest" ][:]
6067
6168 # pylint: disable=too-many-arguments, line-too-long
62- def RND (a , b , c , d , e , f , g , h , i , ki ):
69+ def RND (
70+ a : int , b : int , c : int , d : int , e : int , f : int , g : int , h : int , i : int , ki : int
71+ ) -> Tuple [int , int ]:
6372 """Compress"""
6473 t0 = h + Sigma1 (e ) + Ch (e , f , g ) + ki + W [i ]
6574 t1 = Sigma0 (a ) + Maj (a , b , c )
@@ -267,7 +276,7 @@ def RND(a, b, c, d, e, f, g, h, i, ki):
267276 sha_info ["digest" ] = dig
268277
269278
270- def sha_init ():
279+ def sha_init () -> Dict [ str , Union [ List [ int ], int ]] :
271280 """Initialize the SHA digest."""
272281 sha_info = new_shaobject ()
273282 sha_info ["digest" ] = [
@@ -287,7 +296,7 @@ def sha_init():
287296 return sha_info
288297
289298
290- def sha224_init ():
299+ def sha224_init () -> Dict [ str , Union [ List [ int ], int ]] :
291300 """Initialize a SHA224 digest."""
292301 sha_info = new_shaobject ()
293302 sha_info ["digest" ] = [
@@ -307,13 +316,15 @@ def sha224_init():
307316 return sha_info
308317
309318
310- def getbuf (string ) :
319+ def getbuf (string : Union [ str , bytes ]) -> bytes :
311320 if isinstance (string , str ):
312321 return string .encode ("ascii" )
313322 return bytes (string )
314323
315324
316- def sha_update (sha_info , buffer ):
325+ def sha_update (
326+ sha_info : Dict [str , Union [List [int ], int ]], buffer : Union [str , bytes ]
327+ ) -> None :
317328 """Update the SHA digest.
318329 :param dict sha_info: SHA Digest.
319330 :param str buffer: SHA buffer size.
@@ -360,7 +371,7 @@ def sha_update(sha_info, buffer):
360371 sha_info ["local" ] = size
361372
362373
363- def sha_final (sha_info ) :
374+ def sha_final (sha_info : Dict [ str , Union [ List [ int ], int ]]) -> bytes :
364375 """Finish computing the SHA Digest."""
365376 lo_bit_count = sha_info ["count_lo" ]
366377 hi_bit_count = sha_info ["count_hi" ]
@@ -401,13 +412,13 @@ class sha256:
401412 block_size = SHA_BLOCKSIZE
402413 name = "sha256"
403414
404- def __init__ (self , s = None ):
415+ def __init__ (self , s : Optional [ Union [ str , bytes ]] = None ):
405416 """Constructs a SHA256 hash object."""
406417 self ._sha = sha_init ()
407418 if s :
408419 sha_update (self ._sha , getbuf (s ))
409420
410- def update (self , s ):
421+ def update (self , s : Union [ str , bytes ] ):
411422 """Updates the hash object with a bytes-like object, s."""
412423 sha_update (self ._sha , getbuf (s ))
413424
@@ -434,7 +445,7 @@ class sha224(sha256):
434445 digest_size = digestsize = 28
435446 name = "sha224"
436447
437- def __init__ (self , s = None ):
448+ def __init__ (self , s : Optional [ Union [ str , bytes ]] = None ):
438449 """Constructs a SHA224 hash object."""
439450 self ._sha = sha224_init ()
440451 if s :
0 commit comments