class CryptoTests(unittest.TestCase): def setUp(self): self.crypto = Crypto() def test_get_signature(self): """ get_signature should return signature for specified value and private key """ priv_key = 'b22c8ea30609663197550b010e7abf5a9726523e8ca7ffdfb6a102815d3c8e97' tgs_sign = 'd83c0713135d774afda7df23e8c45d4456f0e7cfbea92824b8980d2d6934b16f5e7b665e95cfd7d7ec2eddcd9c5ca7e2c0e257df01817033bc0f2aab2ce7bab2' value_1 = b'test value' signature_1 = self.crypto.get_signature(value_1, priv_key).hex() self.assertEqual(signature_1, tgs_sign) def test_check_signature(self): """ check_signature should validate signature """ public_key = '0224d2079e86e937224f08aa37a857ca6116546868edde549d0bd6b8536af9d554' tcs_sig = '749625f8d70efae75ffd4a62e22c6534b2cbaa49212c454e6cfb7c5215e39ef01d0388999b2d38a24ad379245e1b4c69b9259b1c8c86bb011712999b4565192d' value = digest('some_key').hex() + 'some_data' + str(None) + str(PersistMode.SECURED) self.assertTrue(self.crypto.check_signature(digest(value), tcs_sig, public_key))
def validate_authorization(dkey, value: Value): log.debug(f"Going to validate authorization for key {dkey.hex()}") sign = value.authorization.sign exp_time = value.authorization.pub_key.exp_time persist_mode = value.persist_mode data = value.data assert exp_time is None or exp_time > int(time.time()) d_record = digest(dkey.hex() + str(data) + str(exp_time) + persist_mode.value) if not Crypto.check_signature(d_record, sign, value.authorization.pub_key.key): raise InvalidSignException(sign)
def of_params(dkey: bytes, data: str, persist_mode: PersistMode, exp_time=None, priv_key=Config.NODE_PRIVATE_KEY, pub_key=Config.NODE_PUBLIC_KEY): log.debug(f'Going to sign {data} with key: [{dkey.hex()}]') dval = digest(dkey.hex() + str(data) + str(exp_time) + str(persist_mode)) signature = Crypto.get_signature(dval, priv_key).hex() log.debug(f'Successfully signed data with key: [{dkey.hex()}]') return Value(dkey, data, str(persist_mode), Authorization(PublicKey(pub_key, exp_time), signature))
def of_params(dkey: bytes, data, exp_time=None, priv_key=Config.NODE_PRIVATE_KEY, pub_key=Config.NODE_PUBLIC_KEY): log.debug(f'Going to sign {data} with key: [{dkey.hex()}]') if isinstance(data, Value) or isinstance(data, ControlledValue): data = str(data) dval = digest(dkey.hex() + str(data) + str(exp_time)) signature = Crypto.get_signature(dval, priv_key).hex() log.debug(f'Successfully signed data with key: [{dkey.hex()}]') return NodeMessage( dkey, data, Authorization(PublicKey(pub_key, exp_time), signature))
def is_valid(self): dval = digest(self.__dkey.hex() + self.data + str(self.authorization.pub_key.exp_time)) pub_key = self.authorization.pub_key.key return Crypto.check_signature(dval, self.authorization.sign, pub_key)
def setUp(self): self.crypto = Crypto()