Skip to content

Commit 11bf70c

Browse files
Add type annotations to hmac.py
1 parent 4e4f3f5 commit 11bf70c

File tree

1 file changed

+30
-21
lines changed

1 file changed

+30
-21
lines changed

adafruit_azureiot/hmac.py

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,15 @@
1616

1717
# pylint: disable=C0103, W0108, R0915, C0116, C0115
1818

19+
from __future__ import annotations
1920

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:
2128
return bytes(translation[x] for x in key)
2229

2330

@@ -28,7 +35,7 @@ def __translate(key, translation):
2835
SHA_DIGESTSIZE = 32
2936

3037

31-
def new_shaobject():
38+
def new_shaobject() -> dict:
3239
"""Struct. for storing SHA information."""
3340
return {
3441
"digest": [0] * 8,
@@ -40,7 +47,7 @@ def new_shaobject():
4047
}
4148

4249

43-
def sha_init():
50+
def sha_init() -> dict:
4451
"""Initialize the SHA digest."""
4552
sha_info = new_shaobject()
4653
sha_info["digest"] = [
@@ -73,7 +80,7 @@ def sha_init():
7380
Gamma1 = lambda x: (S(x, 17) ^ S(x, 19) ^ R(x, 10))
7481

7582

76-
def sha_transform(sha_info):
83+
def sha_transform(sha_info: dict) -> None:
7784
W = []
7885

7986
d = sha_info["data"]
@@ -90,7 +97,7 @@ def sha_transform(sha_info):
9097
ss = sha_info["digest"][:]
9198

9299
# 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]
94101
"""Compress"""
95102
t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]
96103
t1 = Sigma0(a) + Maj(a, b, c)
@@ -298,7 +305,7 @@ def RND(a, b, c, d, e, f, g, h, i, ki):
298305
sha_info["digest"] = dig
299306

300307

301-
def sha_update(sha_info, buffer):
308+
def sha_update(sha_info: dict, buffer: Union[bytes, bytearray]) -> None:
302309
"""Update the SHA digest.
303310
:param dict sha_info: SHA Digest.
304311
:param str buffer: SHA buffer size.
@@ -346,13 +353,13 @@ def sha_update(sha_info, buffer):
346353
sha_info["local"] = count
347354

348355

349-
def getbuf(s):
356+
def getbuf(s: Union[str, bytes, bytearray]) -> Union[bytes, bytearray]:
350357
if isinstance(s, str):
351358
return s.encode("ascii")
352359
return bytes(s)
353360

354361

355-
def sha_final(sha_info):
362+
def sha_final(sha_info: dict) -> bytes:
356363
"""Finish computing the SHA Digest."""
357364
lo_bit_count = sha_info["count_lo"]
358365
hi_bit_count = sha_info["count_hi"]
@@ -393,28 +400,28 @@ class sha256:
393400
block_size = SHA_BLOCKSIZE
394401
name = "sha256"
395402

396-
def __init__(self, s=None):
403+
def __init__(self, s: Union[str, bytes, bytearray] = None):
397404
"""Constructs a SHA256 hash object."""
398405
self._sha = sha_init()
399406
if s:
400407
sha_update(self._sha, getbuf(s))
401408

402-
def update(self, s):
409+
def update(self, s: Union[str, bytes, bytearray]) -> None:
403410
"""Updates the hash object with a bytes-like object, s."""
404411
sha_update(self._sha, getbuf(s))
405412

406-
def digest(self):
413+
def digest(self) -> bytes:
407414
"""Returns the digest of the data passed to the update()
408415
method so far."""
409416
return sha_final(self._sha.copy())[: self._sha["digestsize"]]
410417

411-
def hexdigest(self):
418+
def hexdigest(self) -> str:
412419
"""Like digest() except the digest is returned as a string object of
413420
double length, containing only hexadecimal digits.
414421
"""
415422
return "".join(["%.2x" % i for i in self.digest()])
416423

417-
def copy(self):
424+
def copy(self) -> sha256:
418425
"""Return a copy (“clone”) of the hash object."""
419426
new = sha256()
420427
new._sha = self._sha.copy()
@@ -429,7 +436,9 @@ class HMAC:
429436

430437
blocksize = 64 # 512-bit HMAC; can be changed in subclasses.
431438

432-
def __init__(self, key, msg=None):
439+
def __init__(
440+
self, key: Union[bytes, bytearray], msg: Union[bytes, bytearray] = None
441+
):
433442
"""Create a new HMAC object.
434443
435444
key: key for the keyed hash object.
@@ -478,15 +487,15 @@ def __init__(self, key, msg=None):
478487
self.update(msg)
479488

480489
@property
481-
def name(self):
490+
def name(self) -> str:
482491
"""Return the name of this object"""
483492
return "hmac-" + self.inner.name
484493

485-
def update(self, msg):
494+
def update(self, msg: Union[bytes, bytearray]) -> None:
486495
"""Update this hashing object with the string msg."""
487496
self.inner.update(msg)
488497

489-
def copy(self):
498+
def copy(self) -> HMAC:
490499
"""Return a separate copy of this hashing object.
491500
492501
An update to this copy won't affect the original object.
@@ -499,7 +508,7 @@ def copy(self):
499508
other.outer = self.outer.copy()
500509
return other
501510

502-
def _current(self):
511+
def _current(self) -> sha256:
503512
"""Return a hash object for the current state.
504513
505514
To be used only internally with digest() and hexdigest().
@@ -508,7 +517,7 @@ def _current(self):
508517
hmac.update(self.inner.digest())
509518
return hmac
510519

511-
def digest(self):
520+
def digest(self) -> bytes:
512521
"""Return the hash value of this hashing object.
513522
514523
This returns a string containing 8-bit data. The object is
@@ -518,13 +527,13 @@ def digest(self):
518527
hmac = self._current()
519528
return hmac.digest()
520529

521-
def hexdigest(self):
530+
def hexdigest(self) -> str:
522531
"""Like digest(), but returns a string of hexadecimal digits instead."""
523532
hmac = self._current()
524533
return hmac.hexdigest()
525534

526535

527-
def new_hmac(key, msg=None):
536+
def new_hmac(key: Union[bytes, bytearray], msg: Union[bytes, bytearray] = None) -> HMAC:
528537
"""Create a new hashing object and return it.
529538
530539
key: The starting key for the hash.

0 commit comments

Comments
 (0)