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

Function preprocess

hashes/md5.py:90–131  ·  view source on GitHub ↗

Preprocesses the message string: - Convert message to bit string - Pad bit string to a multiple of 512 chars: - Append a 1 - Append 0's until length = 448 (mod 512) - Append length of original message (64 chars) Example: Suppose the input is the following:

(message: bytes)

Source from the content-addressed store, hash-verified

88
89
90def preprocess(message: bytes) -> bytes:
91 """
92 Preprocesses the message string:
93 - Convert message to bit string
94 - Pad bit string to a multiple of 512 chars:
95 - Append a 1
96 - Append 0's until length = 448 (mod 512)
97 - Append length of original message (64 chars)
98
99 Example: Suppose the input is the following:
100 message = "a"
101
102 The message bit string is "01100001", which is 8 bits long. Thus, the
103 bit string needs 439 bits of padding so that
104 (bit_string + "1" + padding) = 448 (mod 512).
105 The message length is "000010000...0" in 64-bit little-endian binary.
106 The combined bit string is then 512 bits long.
107
108 Arguments:
109 message {[string]} -- [message string]
110
111 Returns:
112 processed bit string padded to a multiple of 512 chars
113
114 >>> preprocess(b"a") == (b"01100001" + b"1" +
115 ... (b"0" * 439) + b"00001000" + (b"0" * 56))
116 True
117 >>> preprocess(b"") == b"1" + (b"0" * 447) + (b"0" * 64)
118 True
119 """
120 bit_string = b""
121 for char in message:
122 bit_string += format(char, "08b").encode("utf-8")
123 start_len = format(len(bit_string), "064b").encode("utf-8")
124
125 # Pad bit_string to a multiple of 512 chars
126 bit_string += b"1"
127 while len(bit_string) % 512 != 448:
128 bit_string += b"0"
129 bit_string += to_little_endian(start_len[32:]) + to_little_endian(start_len[:32])
130
131 return bit_string
132
133
134def get_block_words(bit_string: bytes) -> Generator[list[int]]:

Callers 1

md5_meFunction · 0.70

Calls 2

to_little_endianFunction · 0.85
encodeMethod · 0.45

Tested by

no test coverage detected