def test_one_string(self): h = Hashxx() h.update(b'hello') self.assertEqual(h.digest(), 4211111929) h = Hashxx() h.update(b'goodbye') self.assertEqual(h.digest(), 2269043192)
def _hash(self, filepath, block_size): checksum = Hash() with open(filepath, 'rb') as file: buffer = file.read(block_size) while buffer: checksum.update(buffer) buffer = file.read(block_size) return checksum.digest()
def hash_multiple_values(vals): ''' Same as hash_one_value but iterates over a list and uses the Hashxx class so it can call update() multiple times, passing in additional data on each call. This could also be useful for streaming data, and also allows you to get the current (partial) digest and continue adding data. ''' hasher = Hashxx(seed=0) for idx, val in enumerate(vals): hasher.update(val) print(" Intermediate hash up to %s = %d" % (repr(vals[:idx+1]), hasher.digest())) print("Hash of %s = %d" % (repr(vals), hasher.digest()))
def hashf(filename): d = Hashxx(seed=0) # seed is optional d1 = Hashxx(seed=128) for buf in chunks(filename, 1024): d.update(buf) d1.update(buf) return d.digest() , d1.digest()
def test_incremental(self): # Make sure incrementally computed results match those # computed all at once hello_hash = self.hash_value(b'hello') hello_world_hash = self.hash_value(b'helloworld') h = Hashxx() h.update(b'hello') self.assertEqual(h.digest(), hello_hash) h.update(b'world') self.assertEqual(h.digest(), hello_world_hash)
def hash_multiple_values(vals): ''' Same as hash_one_value but iterates over a list and uses the Hashxx class so it can call update() multiple times, passing in additional data on each call. This could also be useful for streaming data, and also allows you to get the current (partial) digest and continue adding data. ''' hasher = Hashxx(seed=0) for idx, val in enumerate(vals): hasher.update(val) print(" Intermediate hash up to %s = %d" % (repr(vals[:idx + 1]), hasher.digest())) print("Hash of %s = %d" % (repr(vals), hasher.digest()))
def test_no_unicode(self): h = Hashxx() self.assertRaises(TypeError, h.update, 'hello')
def test_no_args(self): h = Hashxx() self.assertRaises(TypeError, h.update)
def test_bad_arg(self): h = Hashxx() self.assertRaises(TypeError, h.update, [1,2,3])
def test_simultaneous(self): # Ensure that interleaved updates still give same results as # independent h1 = Hashxx() h2 = Hashxx() h1.update(b'he') h2.update(b'goo') h1.update(b'll') h2.update(b'db') h1.update(b'o') h2.update(b'ye') self.assertEqual(h1.digest(), self.hash_value(b'hello')) self.assertEqual(h2.digest(), self.hash_value(b'goodbye'))
def test_empty_string(self): h = Hashxx() h.update(b'') self.assertEqual(h.digest(), 46947589)
def hash_value(self, val, seed=0): h = Hashxx(seed=seed) h.update(val) return h.digest()
def test_seeds(self): h = Hashxx(seed=0) h.update(b'hello') self.assertEqual(h.digest(), 4211111929) h = Hashxx(seed=1) h.update(b'hello') self.assertEqual(h.digest(), 4244634537) h = Hashxx(seed=2) h.update(b'hello') self.assertEqual(h.digest(), 4191738725)
def test_tuple(self): # Tuples shouldn't affect the hash, they should be equivalent to hashing # each part in a separate update h = Hashxx() h.update((b'hello',b'goodbye')) self.assertEqual(h.digest(), 4110974955)
def test_multiple_strings(self): h = Hashxx() h.update(b'hello') h.update(b'goodbye') self.assertEqual(h.digest(), 4110974955)