예제 #1
0
    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)
예제 #2
0
 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()
예제 #3
0
파일: check.py 프로젝트: matt-hayden/cutils
 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()
예제 #4
0
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()))
예제 #5
0
파일: hash.py 프로젝트: 4g/pictorg
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()
예제 #6
0
    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)
예제 #7
0
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()))
예제 #8
0
 def test_no_unicode(self):
     h = Hashxx()
     self.assertRaises(TypeError, h.update, 'hello')
예제 #9
0
 def test_no_args(self):
     h = Hashxx()
     self.assertRaises(TypeError, h.update)
예제 #10
0
 def test_bad_arg(self):
     h = Hashxx()
     self.assertRaises(TypeError, h.update, [1,2,3])
예제 #11
0
    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'))
예제 #12
0
 def test_empty_string(self):
     h = Hashxx()
     h.update(b'')
     self.assertEqual(h.digest(), 46947589)
예제 #13
0
 def hash_value(self, val, seed=0):
     h = Hashxx(seed=seed)
     h.update(val)
     return h.digest()
예제 #14
0
    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)
예제 #15
0
 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)
예제 #16
0
 def test_multiple_strings(self):
     h = Hashxx()
     h.update(b'hello')
     h.update(b'goodbye')
     self.assertEqual(h.digest(), 4110974955)