MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / get_block_words

Function get_block_words

hashes/md5.py:134–187  ·  view source on GitHub ↗

Splits bit string into blocks of 512 chars and yields each block as a list of 32-bit words Example: Suppose the input is the following: bit_string = "000000000...0" + # 0x00 (32 bits, padded to the right) "000000010...0" + # 0x01 (32 bits, padded to th

(bit_string: bytes)

Source from the content-addressed store, hash-verified

132
133
134def get_block_words(bit_string: bytes) -> Generator[list[int]]:
135 """
136 Splits bit string into blocks of 512 chars and yields each block as a list
137 of 32-bit words
138
139 Example: Suppose the input is the following:
140 bit_string =
141 "000000000...0" + # 0x00 (32 bits, padded to the right)
142 "000000010...0" + # 0x01 (32 bits, padded to the right)
143 "000000100...0" + # 0x02 (32 bits, padded to the right)
144 "000000110...0" + # 0x03 (32 bits, padded to the right)
145 ...
146 "000011110...0" # 0x0a (32 bits, padded to the right)
147
148 Then len(bit_string) == 512, so there'll be 1 block. The block is split
149 into 32-bit words, and each word is converted to little endian. The
150 first word is interpreted as 0 in decimal, the second word is
151 interpreted as 1 in decimal, etc.
152
153 Thus, block_words == [[0, 1, 2, 3, ..., 15]].
154
155 Arguments:
156 bit_string {[string]} -- [bit string with multiple of 512 as length]
157
158 Raises:
159 ValueError -- [length of bit string isn't multiple of 512]
160
161 Yields:
162 a list of 16 32-bit words
163
164 >>> test_string = ("".join(format(n << 24, "032b") for n in range(16))
165 ... .encode("utf-8"))
166 >>> list(get_block_words(test_string))
167 [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]]
168 >>> list(get_block_words(test_string * 4)) == [list(range(16))] * 4
169 True
170 >>> list(get_block_words(b"1" * 512)) == [[4294967295] * 16]
171 True
172 >>> list(get_block_words(b""))
173 []
174 >>> list(get_block_words(b"1111"))
175 Traceback (most recent call last):
176 ...
177 ValueError: Input must have length that&#x27;s a multiple of 512
178 """
179 if len(bit_string) % 512 != 0:
180 raise ValueError("Input must have length that's a multiple of 512")
181
182 for pos in range(0, len(bit_string), 512):
183 block = bit_string[pos : pos + 512]
184 block_words = []
185 for i in range(0, 512, 32):
186 block_words.append(int(to_little_endian(block[i : i + 32]), 2))
187 yield block_words
188
189
190def not_32(i: int) -> int:

Callers 1

md5_meFunction · 0.85

Calls 2

to_little_endianFunction · 0.85
appendMethod · 0.45

Tested by

no test coverage detected