Skip to content

Commit 87ba91d

Browse files
author
brentru
committed
add docstrings, nicer comments
1 parent f7d0640 commit 87ba91d

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

adafruit_hashlib/_sha1.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,21 @@
3636
from io import BytesIO
3737
from micropython import const
3838

39-
39+
# SHA Block size and message digest sizes, in bytes.
4040
SHA_BLOCKSIZE = 64
4141
SHA_DIGESTSIZE = 20
4242

43-
# initial hash value [5.3.1]
43+
# initial hash value [FIPS 5.3.1]
4444
K0 = const(0x5A827999)
4545
K1 = const(0x6ED9EBA1)
4646
K2 = const(0x8F1BBCDC)
4747
K3 = const(0xCA62C1D6)
4848

4949
def _getbuf(data):
5050
"""Converts data into ascii,
51-
returns bytes of data
51+
returns bytes of data.
52+
:param str bytes bytearray data: Data to convert.
53+
5254
"""
5355
if isinstance(data, str):
5456
return data.encode('ascii')
@@ -59,6 +61,7 @@ def _left_rotate(n, b):
5961
"""Left rotate a 32-bit integer, n, by b bits.
6062
:param int n: 32-bit integer
6163
:param int b: Desired rotation amount, in bits.
64+
6265
"""
6366
return ((n << b) | (n >> (32 - b))) & 0xffffffff
6467

@@ -68,8 +71,9 @@ def _hash_computation(chunk, h0, h1, h2, h3, h4):
6871
Per FIPS [6.1.2]
6972
:param bytes bytearray chunk: 64-bit bytearray
7073
:param list h_tuple: List of hash values for the chunk
74+
7175
"""
72-
assert len(chunk) == 64, "Chunk should be 64-bits"
76+
assert len(chunk) == 64, "Chunk size should be 64-bits"
7377

7478
w = [0] * 80
7579

@@ -126,7 +130,7 @@ class sha1():
126130
name = "sha1"
127131
def __init__(self):
128132
"""Construct a SHA-1 hash object.
129-
133+
:param bytes data: data to process
130134
"""
131135
# Initial Digest Variables
132136
self._h = (0x67452301,
@@ -135,7 +139,11 @@ def __init__(self):
135139
0x10325476,
136140
0xC3D2E1F0)
137141

142+
# bytes object with 0 <= len < 64 used to store the end of the message
143+
# if the message length is not congruent to 64
138144
self._unprocessed = b''
145+
146+
# Length in bytes of all data that has been processed so far
139147
self._msg_byte_len = 0
140148

141149
def _create_digest(self):
@@ -146,20 +154,18 @@ def _create_digest(self):
146154
message = self._unprocessed
147155
message_len = self._msg_byte_len + len(message)
148156

149-
# add trailing '1' bit (+ 0's padding) to string [§5.1.1]
157+
# add trailing '1' bit (+ 0's padding) to string [FIPS 5.1.1]
150158
message += b'\x80'
151159

152160
# append 0 <= k < 512 bits '0', so that the resulting message length (in bytes)
153161
# is congruent to 56 (mod 64)
154162
message += b'\x00' * ((56 - (message_len + 1) % 64) % 64)
155163

156-
157164
# append ml, the original message length, as a 64-bit big-endian integer.
158165
message_bit_length = message_len * 8
159166
message += struct.pack(b'>Q', message_bit_length)
160167

161168
# Process the final chunk
162-
# At this point, the length of the message is either 64 or 128 bytes.
163169
h = _hash_computation(message[:64], *self._h)
164170
if len(message) == 64:
165171
return h
@@ -173,7 +179,7 @@ def update(self, data):
173179
# if we get a string, convert to a bytearray objects
174180
data = _getbuf(data)
175181

176-
# switch input to bytesio api for easier reading
182+
# Use BytesIO for stream-like reading
177183
if isinstance(data, (bytes, bytearray)):
178184
data = BytesIO(data)
179185

@@ -182,6 +188,7 @@ def update(self, data):
182188

183189
while len(chunk) == 64:
184190
self._h = _hash_computation(chunk, *self._h)
191+
# increase the length of the message by 64 bytes
185192
self._msg_byte_len += 64
186193
# read the next 64 bytes
187194
chunk = data.read(64)

0 commit comments

Comments
 (0)