def __calculateAdjacent(bits: bitarray, type): positive = bits.copy() negative = bits.copy() length = bits.length() index = -1 count = bits.count() carrySubtract = True carryAdd = True if count == length and type == 'lat': carryAdd = False elif count == 0 and type == 'lat': carrySubtract = False while (carrySubtract or carryAdd) and abs(index) <= length: value = bits[index] if carrySubtract: if value: negative[index] = False carrySubtract = False else: negative[index] = True if carryAdd: if value: positive[index] = False else: positive[index] = True carryAdd = False index += -1 return negative, positive
def shr(data: bitarray.bitarray, dist: int): # shift right data = data.copy() for a in range(dist): data.insert(0, 0) data.pop() return data
def pad(message: bitarray.bitarray): # This assumes all fits into only 1 message of 512 base = message.copy() ks = 448 - 1 - len(message) while ks < 0: ks += block_size base.append(1) for _ in range(ks): base.append(0) extra = "{0:b}".format(len(message)) extra = "0" * (64 - len(extra)) + extra return base + bitarray.bitarray(extra)
def SIGMA_1(data: bitarray.bitarray): # sha operation prep = (rotr(data.copy(), 6), rotr(data.copy(), 11), rotr(data.copy(), 25)) return xor(*prep)
def SIGMA_0(data: bitarray.bitarray): # sha operation prep = (rotr(data.copy(), 2), rotr(data.copy(), 13), rotr(data.copy(), 22)) return xor(*prep)
def sigma_1(data: bitarray.bitarray): # sha operation prep = (rotr(data.copy(), 17), rotr(data.copy(), 19), shr(data.copy(), 10)) return xor(*prep)
def sigma_0(data: bitarray.bitarray): # sha operation prep = (rotr(data.copy(), 7), rotr(data.copy(), 18), shr(data.copy(), 3)) return xor(*prep)
def rotr(data: bitarray.bitarray, dist: int): # rotate right data = data.copy() for a in range(dist): data.insert(0, data.pop()) return data