def test_random_value(self): """ Verify that hashlib.sha2 returns the same digest for a few quasi-random values. """ rng = SimpleRNG() for _ in range(4): count = 16 + rng.next_int16(48) data = rng.some_bytes(count) my_hex = XLSHA3(data).hexdigest() expected = hashlib.sha3_256(data).hexdigest() self.assertEqual(my_hex, expected)
def test_random_value(self): """ Verify that pyblake2.blake2b and hashlib.blake2b return the same digest for a few quasi-random values. This test only makes sense for more recent versions of hashlib which support blake2. """ if sys.version_info >= (3, 6): rng = SimpleRNG() for _ in range(4): count = 16 + rng.next_int16(48) data = rng.some_bytes(count) my_hex = XLBLAKE2B_256(data).hexdigest() expected = pyblake2.blake2b(data, digest_size=32).hexdigest() self.assertEqual(my_hex, expected)
def rand_test(self, count=1000): """ Repeath a suite of tests N times. """ rng = SimpleRNG(time.time()) self.do_test(count, rng.random, ()) self.do_test(count, rng.normalvariate, (0.0, 1.0)) self.do_test(count, rng.lognormvariate, (0.0, 1.0)) self.do_test(count, rng.vonmisesvariate, (0.0, 1.0)) self.do_test(count, rng.gammavariate, (0.01, 1.0)) self.do_test(count, rng.gammavariate, (0.1, 1.0)) self.do_test(count, rng.gammavariate, (0.1, 2.0)) self.do_test(count, rng.gammavariate, (0.5, 1.0)) self.do_test(count, rng.gammavariate, (0.9, 1.0)) self.do_test(count, rng.gammavariate, (1.0, 1.0)) self.do_test(count, rng.gammavariate, (2.0, 1.0)) self.do_test(count, rng.gammavariate, (20.0, 1.0)) self.do_test(count, rng.gammavariate, (200.0, 1.0)) self.do_test(count, rng.gauss, (0.0, 1.0)) self.do_test(count, rng.betavariate, (3.0, 3.0)) self.do_test(count, rng.triangular, (0.0, 1.0, 1.0 / 3.0))
def setUp(self): self.rng = SimpleRNG()
#!/usr/bin/env python3 # xlcrypto_py/test_nibble_counters.py """ Test nibble counters used in CountingBlooms. """ import time import unittest # from hashlib import sha1, sha256 as sha2 from rnglib import SimpleRNG from xlcrypto import XLFilterError from xlcrypto.filters import BloomSHA, NibbleCounters RNG = SimpleRNG(time.time()) class TestNibbleCounters(unittest.TestCase): """ Tests the counters associated with Bloom filters for sets whose members are 20- or 32-byte SHA digests. """ def do_nibble_test_bit(self, counters, filter_bit): """ Count up through all possible values and beyond. """ value = 0 # DEBUG # print("do_nibble_test_bit: filter_bit %6d" % filter_bit) # END for i in range(18): # DEBUG # print(" up %2d" % i)
def setUp(self): self.rng = SimpleRNG(time.time())