def update(self, input_msg): # self is like context in rfc1321 # Though we would not be using the input directly here, we need its length. # Repeated calls are equivalent to a single call with concatenation of the arguments input_len = long(len(input_msg)) index = ((self.count[0] >> 3) & 0x3FL) # number of bytes mod 64 #update the number of bits self.count[0] += (input_len << 3) if self.count[0] < (input_len << 3): self.count[1] += 1 self.count[1] += (input_len >> 29) part_len = 64 - index # Transform as many times as possible if input_len >= part_len: temp = list(input_msg) self.buffer[index:] = temp[:part_len] self.transform(define.decode(self.buffer)) i = part_len while i + 63 < input_len: self.transform(define.decode(temp[i:i+64])) i += 64 else: index = 0 #self.input = list(input_msg[i:input_len]) else: i= 0 #temp_im = list(input_msg) self.buffer += list(input_msg)
def final_digest (self): # MD5 finalization: ends an MD5 message-digest operation returning the 16 byte digest # Performs the function of encode func of rfc1321 A = self.A B = self.B C = self.C D = self.D input = [ ] + self.buffer count = [ ] + self.count # Padding out to 56 mod 64 index = (self.count[0] >> 3) & 0x3fL if index < 56: padding_len = 56 - index else: padding_len = 120 - index padding = ['\200'] + ['\000'] * 63 self.update(padding[:padding_len]) # Call to update here does padding. bits = define.decode(self.buffer[:56]) + count self.transform(bits) # Store state in digest. digest = struct.pack("<IIII", self.A, self.B, self.C, self.D) self.A = A self.B = B self.C = C self.D = D self.buffer = input self.count = count return hexlify(digest)