Exemple #1
0
class KnotHashV1:
    def __init__(self,
                 lengths,
                 list_size=256,
                 initial_position=0,
                 initial_skip_size=0):
        self.position = initial_position
        self.skip_size = initial_skip_size
        self.list = CircularList(list_size)

        for length in lengths:
            self.list.reverse(self.position, length)
            self.position = self.list.get_actual_position(self.position +
                                                          length +
                                                          self.skip_size)
            self.skip_size += 1

    def get_current_position(self):
        return self.position

    def get_list(self):
        return self.list.get_list()

    def get_skip_size(self):
        return self.skip_size

    def compute_checksum(self):
        current_list = self.get_list()
        return current_list[0] * current_list[1]
Exemple #2
0
    def __init__(self, message):
        self.message = message

        lengths = AsciiConverter.to_ascii_codes(message)

        position = 0
        skip_size = 0
        nums = CircularList(256)
        for _ in range(64):
            for length in lengths:
                nums.reverse(position, length)
                position = nums.get_actual_position(position + length +
                                                    skip_size)
                skip_size += 1

        sparse_hash = nums.get_list()
        dense_hash = DenseHashProcessor(sparse_hash).get_hash()
        self.string_hash = HashFormatter.format(dense_hash)
 def testGetActualPosition_whenPositionGTEListLength_shouldReturnGivenPositionModuloListLength(
         self):
     sut = CircularList(5)
     self.assertEqual(0, sut.get_actual_position(position=5))
 def testGetActualPosition_whenPositionLTListLength_shouldReturnGivenPosition(
         self):
     sut = CircularList(5)
     self.assertEqual(4, sut.get_actual_position(position=4))