Beispiel #1
0
    def test_double_sha(self):
        # Example from bitcoin specification

        self.assertEqual(
            exercise.double_sha(b'hello'),
            binascii.unhexlify(
                '9595c9df90075148eb06860365df33584b75bff782a510c6cd4883a419833d50'
            ))
Beispiel #2
0
    def test_return_from_two_pools_second_reseed(self):
        p = exercise.EntropyPool()
        p.add_event(1, 0, b'abcdefghijklmnopqrstuvwxyz')
        p.add_event(2, 0, b'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
        p.add_event(3, 0, b'0123456789')

        p.add_event(1, 1, b'will get used')

        p.add_event(1, 2, b'will not get used')
        p.add_event(1, 3, b'will not get used 2')

        p._reseed_count = 1

        self.assertEquals(
            p.maybe_seed(),
            exercise.double_sha(
                b'\x01\x1aabcdefghijklmnopqrstuvwxyz\x02\x1aABCDEFGHIJKLMNOPQRSTUVWXYZ\x03\x0a0123456789'
            ) + exercise.double_sha(b'\x01\x0dwill get used'))
Beispiel #3
0
    def test_flush(self):
        p = exercise.EntropyPool()
        p.add_event(17, 9, b'hello')
        p.add_event(2, 9, b' world')

        seed = p._pools[9].flush()

        self.assertEquals(seed,
                          exercise.double_sha(b'\x11\x05hello\x02\x06 world'))
        self.assertEquals(p._pools[9].length, 0)
Beispiel #4
0
    def test_reseed(self):
        # `reseed` increments the counter to mark the generator as seeded, and computes the new key by
        # hashing the old key together with the seed.

        g = exercise.Generator()

        g.reseed(binascii.unhexlify('1234'))

        self.assertEqual(g._counter, 1)
        self.assertEqual(
            g._key,
            exercise.double_sha(
                binascii.unhexlify(
                    '00000000000000000000000000000000000000000000000000000000000000001234'
                )))
Beispiel #5
0
    def test_return_just_the_first_pool_on_first_reseed(self):
        p = exercise.EntropyPool()
        p.add_event(1, 0, b'abcdefghijklmnopqrstuvwxyz')
        p.add_event(2, 0, b'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
        p.add_event(3, 0, b'0123456789')

        p.add_event(1, 1, b'will not get used')
        p.add_event(1, 2, b'will not get used 2')
        p.add_event(1, 3, b'will not get used 3')

        self.assertEquals(
            p.maybe_seed(),
            exercise.double_sha(
                b'\x01\x1aabcdefghijklmnopqrstuvwxyz\x02\x1aABCDEFGHIJKLMNOPQRSTUVWXYZ\x03\x0a0123456789'
            ))