Example #1
0
 def test_get_hash_nested_structure(self):
     """
     Ensure a tree like object is recursively hashed to produce a root-hash
     value.
     """
     child_list = ['bar', 1, 1.234, ]
     child_dict = {
         'b': False,
         'a': None,
         'c': True
     }
     to_hash = {
         'foo': child_list,
         'baz': child_dict
     }
     seed_hashes = []
     # REMEMBER - in this algorithm the keys to a dict object are ordered.
     seed_hashes.append(sha512(b'baz').hexdigest())
     seed_hashes.append(_get_hash(child_dict).hexdigest())
     seed_hashes.append(sha512(b'foo').hexdigest())
     seed_hashes.append(_get_hash(child_list).hexdigest())
     seed = ''.join(seed_hashes)
     expected = sha512(seed.encode('utf-8'))
     actual = _get_hash(to_hash)
     self.assertEqual(expected.hexdigest(), actual.hexdigest())
Example #2
0
 def test_get_hash_float(self):
     """
     Ensure float values are hashed correctly.
     """
     expected = sha512(b'12345.6789')
     actual = _get_hash(12345.6789)
     self.assertEqual(expected.hexdigest(), actual.hexdigest())
Example #3
0
 def test_get_hash_str(self):
     """
     Strings are hashed correctly
     """
     expected = sha512(b"foo")
     actual = _get_hash("foo")
     self.assertEqual(expected.hexdigest(), actual.hexdigest())
Example #4
0
 def test_get_hash_int(self):
     """
     Ensure integer values are hashed correctly.
     """
     expected = sha512(b"1234567890987654321234567890987654321")
     actual = _get_hash(1234567890987654321234567890987654321)
     self.assertEqual(expected.hexdigest(), actual.hexdigest())
Example #5
0
 def test_get_hash_float(self):
     """
     Ensure float values are hashed correctly.
     """
     expected = sha512(b"12345.6789")
     actual = _get_hash(12345.6789)
     self.assertEqual(expected.hexdigest(), actual.hexdigest())
Example #6
0
 def test_get_hash_int(self):
     """
     Ensure integer values are hashed correctly.
     """
     expected = sha512(b'1234567890987654321234567890987654321')
     actual = _get_hash(1234567890987654321234567890987654321)
     self.assertEqual(expected.hexdigest(), actual.hexdigest())
Example #7
0
 def test_get_hash_str(self):
     """
     Strings are hashed correctly
     """
     expected = sha512(b'foo')
     actual = _get_hash('foo')
     self.assertEqual(expected.hexdigest(), actual.hexdigest())
Example #8
0
 def test_get_hash_boolean_true(self):
     """
     Ensure hash of Python's True boolean value is a hash of 'true' (since
     this is the true value in JSON).
     """
     expected = sha512(b'true')
     actual = _get_hash(True)
     self.assertEqual(expected.hexdigest(), actual.hexdigest())
Example #9
0
 def test_get_hash_boolean_false(self):
     """
     Ensure hash of Python's False boolean value is a hash of 'false'
     (since this is the false value in JSON).
     """
     expected = sha512(b'false')
     actual = _get_hash(False)
     self.assertEqual(expected.hexdigest(), actual.hexdigest())
Example #10
0
 def test_get_hash_boolean_false(self):
     """
     Ensure hash of Python's False boolean value is a hash of 'false'
     (since this is the false value in JSON).
     """
     expected = sha512(b"false")
     actual = _get_hash(False)
     self.assertEqual(expected.hexdigest(), actual.hexdigest())
Example #11
0
 def test_get_hash_boolean_true(self):
     """
     Ensure hash of Python's True boolean value is a hash of 'true' (since
     this is the true value in JSON).
     """
     expected = sha512(b"true")
     actual = _get_hash(True)
     self.assertEqual(expected.hexdigest(), actual.hexdigest())
Example #12
0
 def test_get_hash_none(self):
     """
     Ensure the hash of Python's None is actually a hash of 'null' (since
     this is the null value for JSON).
     """
     expected = sha512(b"null")
     actual = _get_hash(None)
     self.assertEqual(expected.hexdigest(), actual.hexdigest())
Example #13
0
 def test_get_hash_none(self):
     """
     Ensure the hash of Python's None is actually a hash of 'null' (since
     this is the null value for JSON).
     """
     expected = sha512(b'null')
     actual = _get_hash(None)
     self.assertEqual(expected.hexdigest(), actual.hexdigest())
Example #14
0
 def test_get_hash_nested_structure(self):
     """
     Ensure a tree like object is recursively hashed to produce a root-hash
     value.
     """
     child_list = ["bar", 1, 1.234]
     child_dict = {"b": False, "a": None, "c": True}
     to_hash = {"foo": child_list, "baz": child_dict}
     seed_hashes = []
     # REMEMBER - in this algorithm the keys to a dict object are ordered.
     seed_hashes.append(sha512(b"baz").hexdigest())
     seed_hashes.append(_get_hash(child_dict).hexdigest())
     seed_hashes.append(sha512(b"foo").hexdigest())
     seed_hashes.append(_get_hash(child_list).hexdigest())
     seed = "".join(seed_hashes)
     expected = sha512(seed.encode("utf-8"))
     actual = _get_hash(to_hash)
     self.assertEqual(expected.hexdigest(), actual.hexdigest())
Example #15
0
 def test_get_hash_nested_structure(self):
     """
     Ensure a tree like object is recursively hashed to produce a root-hash
     value.
     """
     child_list = [
         'bar',
         1,
         1.234,
     ]
     child_dict = {'b': False, 'a': None, 'c': True}
     to_hash = {'foo': child_list, 'baz': child_dict}
     seed_hashes = []
     # REMEMBER - in this algorithm the keys to a dict object are ordered.
     seed_hashes.append(sha512(b'baz').hexdigest())
     seed_hashes.append(_get_hash(child_dict).hexdigest())
     seed_hashes.append(sha512(b'foo').hexdigest())
     seed_hashes.append(_get_hash(child_list).hexdigest())
     seed = ''.join(seed_hashes)
     expected = sha512(seed.encode('utf-8'))
     actual = _get_hash(to_hash)
     self.assertEqual(expected.hexdigest(), actual.hexdigest())
Example #16
0
    def test_get_hash_list(self):
        """
        Ensure all the items in a list are hashed in the correct order.
        """
        to_hash = []
        for i in range(5):
            to_hash.append(str(uuid.uuid4()))

        seed_hashes = []
        for item in to_hash:
            seed_hashes.append(sha512(item.encode("utf-8")).hexdigest())
        seed = "".join(seed_hashes)
        expected = sha512(seed.encode("utf-8"))
        actual = _get_hash(to_hash)
        self.assertEqual(expected.hexdigest(), actual.hexdigest())
Example #17
0
    def test_get_hash_list(self):
        """
        Ensure all the items in a list are hashed in the correct order.
        """
        to_hash = []
        for i in range(5):
            to_hash.append(str(uuid.uuid4()))

        seed_hashes = []
        for item in to_hash:
            seed_hashes.append(sha512(item.encode('utf-8')).hexdigest())
        seed = ''.join(seed_hashes)
        expected = sha512(seed.encode('utf-8'))
        actual = _get_hash(to_hash)
        self.assertEqual(expected.hexdigest(), actual.hexdigest())
Example #18
0
 def test_get_seal(self):
     """
     Ensure a good "seal" is created for the passed in dict of values
     given the supplied private key and appropriate shared public key to
     be used to validate the seal.
     """
     values = {"foo": "bar", "baz": {"a": 1, "b": True, "c": 3.141, "d": [1, 2, 3]}}
     seal = get_seal(values, PRIVATE_KEY)
     # Check it's a string and valid hexdecimal value
     self.assertIsInstance(seal, str)
     self.assertIsInstance(int(seal, 16), int)
     # Check it's a seal that can be validated with the correct public key.
     sig = binascii.unhexlify(seal.encode("ascii"))
     key = rsa.PublicKey.load_pkcs1(PUBLIC_KEY.encode("ascii"))
     root_hash = _get_hash(values).hexdigest()
     self.assertTrue(rsa.verify(root_hash.encode("ascii"), sig, key))
Example #19
0
    def test_get_hash_dict(self):
        """
        Ensures that the dict is hashed in such a way that the keys are
        sorted so the resulting leaf hashes are used in the correct order.
        """
        to_hash = {}
        for i in range(5):
            k = str(uuid.uuid4())
            v = str(uuid.uuid4())
            to_hash[k] = v

        seed_hashes = []
        for k in sorted(to_hash):
            v = to_hash[k]
            seed_hashes.append(sha512(k.encode("utf-8")).hexdigest())
            seed_hashes.append(sha512(v.encode("utf-8")).hexdigest())
        seed = "".join(seed_hashes)
        expected = sha512(seed.encode("utf-8"))
        actual = _get_hash(to_hash)
        self.assertEqual(expected.hexdigest(), actual.hexdigest())
Example #20
0
    def test_get_hash_dict(self):
        """
        Ensures that the dict is hashed in such a way that the keys are
        sorted so the resulting leaf hashes are used in the correct order.
        """
        to_hash = {}
        for i in range(5):
            k = str(uuid.uuid4())
            v = str(uuid.uuid4())
            to_hash[k] = v

        seed_hashes = []
        for k in sorted(to_hash):
            v = to_hash[k]
            seed_hashes.append(sha512(k.encode('utf-8')).hexdigest())
            seed_hashes.append(sha512(v.encode('utf-8')).hexdigest())
        seed = ''.join(seed_hashes)
        expected = sha512(seed.encode('utf-8'))
        actual = _get_hash(to_hash)
        self.assertEqual(expected.hexdigest(), actual.hexdigest())
Example #21
0
 def test_get_seal(self):
     """
     Ensure a good "seal" is created for the passed in dict of values
     given the supplied private key and appropriate shared public key to
     be used to validate the seal.
     """
     values = {
         'foo': 'bar',
         'baz': {
             'a': 1,
             'b': True,
             'c': 3.141,
             'd': [1, 2, 3]
         },
     }
     seal = get_seal(values, PRIVATE_KEY)
     # Check it's a string and valid hexdecimal value
     self.assertIsInstance(seal, str)
     self.assertIsInstance(int(seal, 16), int)
     # Check it's a seal that can be validated with the correct public key.
     sig = binascii.unhexlify(seal.encode('ascii'))
     key = rsa.PublicKey.load_pkcs1(PUBLIC_KEY.encode('ascii'))
     root_hash = _get_hash(values).hexdigest()
     self.assertTrue(rsa.verify(root_hash.encode('ascii'), sig, key))