16
16
17
17
# pylint: disable=C0103, W0108, R0915, C0116, C0115
18
18
19
+ from __future__ import annotations
19
20
20
- def __translate (key , translation ):
21
+ try :
22
+ from typing import Union
23
+ except ImportError :
24
+ pass
25
+
26
+
27
+ def __translate (key : Union [bytes , bytearray ], translation : bytes ) -> bytes :
21
28
return bytes (translation [x ] for x in key )
22
29
23
30
@@ -28,7 +35,7 @@ def __translate(key, translation):
28
35
SHA_DIGESTSIZE = 32
29
36
30
37
31
- def new_shaobject ():
38
+ def new_shaobject () -> dict :
32
39
"""Struct. for storing SHA information."""
33
40
return {
34
41
"digest" : [0 ] * 8 ,
@@ -40,7 +47,7 @@ def new_shaobject():
40
47
}
41
48
42
49
43
- def sha_init ():
50
+ def sha_init () -> dict :
44
51
"""Initialize the SHA digest."""
45
52
sha_info = new_shaobject ()
46
53
sha_info ["digest" ] = [
@@ -73,7 +80,7 @@ def sha_init():
73
80
Gamma1 = lambda x : (S (x , 17 ) ^ S (x , 19 ) ^ R (x , 10 ))
74
81
75
82
76
- def sha_transform (sha_info ) :
83
+ def sha_transform (sha_info : dict ) -> None :
77
84
W = []
78
85
79
86
d = sha_info ["data" ]
@@ -90,7 +97,7 @@ def sha_transform(sha_info):
90
97
ss = sha_info ["digest" ][:]
91
98
92
99
# pylint: disable=too-many-arguments, line-too-long
93
- def RND (a , b , c , d , e , f , g , h , i , ki ):
100
+ def RND (a , b , c , d , e , f , g , h , i , ki ): # type: ignore[no-untyped-def]
94
101
"""Compress"""
95
102
t0 = h + Sigma1 (e ) + Ch (e , f , g ) + ki + W [i ]
96
103
t1 = Sigma0 (a ) + Maj (a , b , c )
@@ -298,7 +305,7 @@ def RND(a, b, c, d, e, f, g, h, i, ki):
298
305
sha_info ["digest" ] = dig
299
306
300
307
301
- def sha_update (sha_info , buffer ) :
308
+ def sha_update (sha_info : dict , buffer : Union [ bytes , bytearray ]) -> None :
302
309
"""Update the SHA digest.
303
310
:param dict sha_info: SHA Digest.
304
311
:param str buffer: SHA buffer size.
@@ -346,13 +353,13 @@ def sha_update(sha_info, buffer):
346
353
sha_info ["local" ] = count
347
354
348
355
349
- def getbuf (s ) :
356
+ def getbuf (s : Union [ str , bytes , bytearray ]) -> Union [ bytes , bytearray ] :
350
357
if isinstance (s , str ):
351
358
return s .encode ("ascii" )
352
359
return bytes (s )
353
360
354
361
355
- def sha_final (sha_info ) :
362
+ def sha_final (sha_info : dict ) -> bytes :
356
363
"""Finish computing the SHA Digest."""
357
364
lo_bit_count = sha_info ["count_lo" ]
358
365
hi_bit_count = sha_info ["count_hi" ]
@@ -393,28 +400,28 @@ class sha256:
393
400
block_size = SHA_BLOCKSIZE
394
401
name = "sha256"
395
402
396
- def __init__ (self , s = None ):
403
+ def __init__ (self , s : Union [ str , bytes , bytearray ] = None ):
397
404
"""Constructs a SHA256 hash object."""
398
405
self ._sha = sha_init ()
399
406
if s :
400
407
sha_update (self ._sha , getbuf (s ))
401
408
402
- def update (self , s ) :
409
+ def update (self , s : Union [ str , bytes , bytearray ]) -> None :
403
410
"""Updates the hash object with a bytes-like object, s."""
404
411
sha_update (self ._sha , getbuf (s ))
405
412
406
- def digest (self ):
413
+ def digest (self ) -> bytes :
407
414
"""Returns the digest of the data passed to the update()
408
415
method so far."""
409
416
return sha_final (self ._sha .copy ())[: self ._sha ["digestsize" ]]
410
417
411
- def hexdigest (self ):
418
+ def hexdigest (self ) -> str :
412
419
"""Like digest() except the digest is returned as a string object of
413
420
double length, containing only hexadecimal digits.
414
421
"""
415
422
return "" .join (["%.2x" % i for i in self .digest ()])
416
423
417
- def copy (self ):
424
+ def copy (self ) -> sha256 :
418
425
"""Return a copy (“clone”) of the hash object."""
419
426
new = sha256 ()
420
427
new ._sha = self ._sha .copy ()
@@ -429,7 +436,9 @@ class HMAC:
429
436
430
437
blocksize = 64 # 512-bit HMAC; can be changed in subclasses.
431
438
432
- def __init__ (self , key , msg = None ):
439
+ def __init__ (
440
+ self , key : Union [bytes , bytearray ], msg : Union [bytes , bytearray ] = None
441
+ ):
433
442
"""Create a new HMAC object.
434
443
435
444
key: key for the keyed hash object.
@@ -478,15 +487,15 @@ def __init__(self, key, msg=None):
478
487
self .update (msg )
479
488
480
489
@property
481
- def name (self ):
490
+ def name (self ) -> str :
482
491
"""Return the name of this object"""
483
492
return "hmac-" + self .inner .name
484
493
485
- def update (self , msg ) :
494
+ def update (self , msg : Union [ bytes , bytearray ]) -> None :
486
495
"""Update this hashing object with the string msg."""
487
496
self .inner .update (msg )
488
497
489
- def copy (self ):
498
+ def copy (self ) -> HMAC :
490
499
"""Return a separate copy of this hashing object.
491
500
492
501
An update to this copy won't affect the original object.
@@ -499,7 +508,7 @@ def copy(self):
499
508
other .outer = self .outer .copy ()
500
509
return other
501
510
502
- def _current (self ):
511
+ def _current (self ) -> sha256 :
503
512
"""Return a hash object for the current state.
504
513
505
514
To be used only internally with digest() and hexdigest().
@@ -508,7 +517,7 @@ def _current(self):
508
517
hmac .update (self .inner .digest ())
509
518
return hmac
510
519
511
- def digest (self ):
520
+ def digest (self ) -> bytes :
512
521
"""Return the hash value of this hashing object.
513
522
514
523
This returns a string containing 8-bit data. The object is
@@ -518,13 +527,13 @@ def digest(self):
518
527
hmac = self ._current ()
519
528
return hmac .digest ()
520
529
521
- def hexdigest (self ):
530
+ def hexdigest (self ) -> str :
522
531
"""Like digest(), but returns a string of hexadecimal digits instead."""
523
532
hmac = self ._current ()
524
533
return hmac .hexdigest ()
525
534
526
535
527
- def new_hmac (key , msg = None ):
536
+ def new_hmac (key : Union [ bytes , bytearray ], msg : Union [ bytes , bytearray ] = None ) -> HMAC :
528
537
"""Create a new hashing object and return it.
529
538
530
539
key: The starting key for the hash.
0 commit comments