Пример #1
0
 def check_gen_key_pair(self):
     private_key_bytes = ECIES.generate_private_key()
     self.assertEqual(32, len(private_key_bytes))
     public_key_bytes = ECIES.get_public_key_by_bytes_private_key(
         private_key_bytes)
     self.assertEqual(33, len(public_key_bytes))
     return private_key_bytes, public_key_bytes
Пример #2
0
 def test_encrypt_with_gcm_mode(self):
     for _ in range(10):
         private_key_bytes = ECIES.generate_private_key()
         self.assertEqual(32, len(private_key_bytes))
         public_key_bytes = ECIES.get_public_key_by_bytes_private_key(
             private_key_bytes)
         self.assertEqual(33, len(public_key_bytes))
         msg = b'Attack!'
         nonce, mac_tag, encode_g_tilde, cipher_text = ECIES.encrypt_with_gcm_mode(
             msg, b'', public_key_bytes)
         self.assertRaises(SDKException, ECIES.encrypt_with_gcm_mode, msg,
                           b'', '')
         self.assertRaises(SDKException, ECIES.encrypt_with_gcm_mode, msg,
                           b'', b'')
         decrypt_msg = ECIES.decrypt_with_gcm_mode(nonce, mac_tag,
                                                   cipher_text,
                                                   private_key_bytes, b'',
                                                   encode_g_tilde)
         self.assertEqual(msg, decrypt_msg)
         cipher_text = b'\x00' * len(cipher_text)
         decrypt_msg = ECIES.decrypt_with_gcm_mode(nonce, mac_tag,
                                                   cipher_text,
                                                   private_key_bytes, b'',
                                                   encode_g_tilde)
         self.assertRaises(SDKException, ECIES.decrypt_with_gcm_mode, nonce,
                           mac_tag, cipher_text, '', b'', encode_g_tilde)
         self.assertRaises(SDKException, ECIES.decrypt_with_gcm_mode, nonce,
                           mac_tag, cipher_text, b'', b'', encode_g_tilde)
         self.assertEqual(b'', decrypt_msg)
Пример #3
0
 def test_get_public_key_by_private_key(self):
     for _ in range(10):
         private_key_bytes = ECIES.generate_private_key()
         self.assertEqual(32, len(private_key_bytes))
         public_key_bytes = ECIES.get_public_key_by_bytes_private_key(private_key_bytes)
         self.assertEqual(33, len(public_key_bytes))
         self.assertRaises(SDKException, ECIES.get_public_key_by_bytes_private_key, '')
         self.assertRaises(SDKException, ECIES.get_public_key_by_bytes_private_key, b'')
Пример #4
0
 def test_encrypt_with_cbc_mode(self):
     for _ in range(10):
         private_key_bytes, public_key_bytes = self.check_gen_key_pair()
         msg = b'Attack!'
         aes_iv, encode_g_tilde, cipher_text = ECIES.encrypt_with_cbc_mode(msg, public_key_bytes)
         self.assertRaises(SDKException, ECIES.encrypt_with_cbc_mode, msg, '')
         self.assertRaises(SDKException, ECIES.encrypt_with_cbc_mode, msg, b'')
         decrypt_msg = ECIES.decrypt_with_cbc_mode(cipher_text, private_key_bytes, aes_iv, encode_g_tilde)
         self.assertRaises(SDKException, ECIES.decrypt_with_cbc_mode, cipher_text, b'', aes_iv, encode_g_tilde)
         self.assertRaises(SDKException, ECIES.decrypt_with_cbc_mode, cipher_text, '', aes_iv, encode_g_tilde)
         self.assertEqual(msg, decrypt_msg)
         cipher_text = b'\x00' * len(cipher_text)
         self.assertRaises(SDKException, ECIES.decrypt_with_cbc_mode, cipher_text, private_key_bytes, aes_iv,
                           encode_g_tilde)
Пример #5
0
def get_item_list_from_contract(identity_acct: Account) -> list:
    sdk = app.config['ONTOLOGY']
    if not isinstance(sdk, Ontology):
        return list()
    get_item_list_func = WasmInvokeFunction('get_item_list')
    get_item_list_func.set_params_value(identity_acct.get_address())
    tx = sdk.wasm_vm.make_invoke_transaction(
        app.config['CONTRACT_ADDRESS_HEX'], get_item_list_func,
        identity_acct.get_address(), app.config['GAS_PRICE'],
        app.config['GAS_LIMIT'])
    tx.sign_transaction(identity_acct)
    response = sdk.rpc.send_raw_transaction_pre_exec(tx)
    if not isinstance(response, dict):
        return list()
    result = response.get('Result')
    if result is None:
        return list()
    builder = WasmParamsBuilder(bytes.fromhex(result))
    struct_len = builder.read_var_uint()
    album_list = list()
    for _ in range(struct_len):
        try:
            encrypted_ipfs_address_bytes = bytes.fromhex(builder.pop_str())
            ext = builder.pop_str()
            aes_iv = bytes.fromhex(builder.pop_str())
            encode_g_tilde = bytes.fromhex(builder.pop_str())
            ipfs_address = ECIES.decrypt_with_cbc_mode(
                encrypted_ipfs_address_bytes,
                identity_acct.get_private_key_bytes(), aes_iv, encode_g_tilde)
            album_list.append([ipfs_address.decode('ascii'), ext])
        except Exception as e:
            print('Decrypt with cbc mode failed: ', e.args[0])
    return album_list
Пример #6
0
 def test_java_sdk_case(self):
     hex_private_key = '9a31d585431ce0aa0aab1f0a432142e98a92afccb7bcbcaff53f758df82acdb3'
     private_key_bytes = bytes.fromhex(hex_private_key)
     msg = b'1234567890'
     iv = bytes.fromhex('f88305e04df4bdc491ca6ff65d222386')
     encode_g_tilde = bytes.fromhex('0480a0e5157874371c32cdb01e7e0938d155eaed8f50eecbc6d39b71685d5c69c'
                                    'bb3f5b5c497d2e34ab47f105f85fc39edf0588d32b7e87403d65ff1e181800590')
     cipher_text_bytes = bytes.fromhex('8e7f8f37c16a712f7360c5eebcb0b01a')
     decrypt_msg = ECIES.decrypt_with_cbc_mode(cipher_text_bytes, private_key_bytes, iv, encode_g_tilde)
     self.assertEqual(msg, decrypt_msg)
Пример #7
0
 def test_compatibility(self):
     hex_private_key = '9a31d585431ce0aa0aab1f0a432142e98a92afccb7bcbcaff53f758df82acdb3'
     hex_public_key = ECIES.get_public_key_by_hex_private_key(
         hex_private_key)
     self.assertEqual(
         '021401156f187ec23ce631a489c3fa17f292171009c6c3162ef642406d3d09c74d',
         hex_public_key)
     bytes_private_key = binascii.a2b_hex(hex_private_key)
     public_key_bytes = ECIES.get_public_key_by_bytes_private_key(
         bytes_private_key)
     msg = b'Attack!'
     aes_iv, encode_g_tilde, cipher_text = ECIES.encrypt_with_cbc_mode(
         msg, public_key_bytes)
     decrypt_msg = ECIES.decrypt_with_cbc_mode(cipher_text,
                                               bytes_private_key, aes_iv,
                                               encode_g_tilde)
     self.assertEqual(msg, decrypt_msg)
     nonce, mac_tag, encode_g_tilde, cipher_text = ECIES.encrypt_with_gcm_mode(
         msg, b'', public_key_bytes)
     self.assertRaises(SDKException, ECIES.encrypt_with_gcm_mode, msg, b'',
                       '')
     self.assertRaises(SDKException, ECIES.encrypt_with_gcm_mode, msg, b'',
                       b'')
     decrypt_msg = ECIES.decrypt_with_gcm_mode(nonce, mac_tag, cipher_text,
                                               bytes_private_key, b'',
                                               encode_g_tilde)
     self.assertEqual(msg, decrypt_msg)
 def test_put_one_item(self):
     ipfs_address = 'QmVwRs3tMPwi8vHqZXfxdgbcJXdmrgViGiy77o9ohef6ss'
     ext = '.jpg'
     aes_iv, encode_g_tilde, encrypted_ipfs_address = ECIES.encrypt_with_cbc_mode(ipfs_address.encode('ascii'),
                                                                                  self.ont_id_ctrl_acct.get_public_key_bytes())
     put_one_item_func = WasmInvokeFunction('put_one_item')
     put_one_item_func.set_params_value(self.ont_id_ctrl_acct.get_address(), ipfs_address, ext, aes_iv.hex(),
                                        encode_g_tilde.hex())
     tx = self.sdk.wasm_vm.make_invoke_transaction(self.contract_address, put_one_item_func,
                                                   self.ont_id_ctrl_acct.get_address(), self.gas_price,
                                                   self.gas_limit)
     tx.sign_transaction(self.ont_id_ctrl_acct)
     tx_hash = self.sdk.rpc.send_raw_transaction(tx)
     self.assertEqual(64, len(tx_hash))
     time.sleep(12)
     event = self.sdk.rpc.get_contract_event_by_tx_hash(tx_hash)
     states = Event.get_event_from_event_list_by_contract_address(event['Notify'], self.contract_address)
     self.assertEqual(1, len(states))
Пример #9
0
def put_one_item_to_contract(ont_id_acct: Account, ipfs_address: str, ext: str,
                             payer_acct: Account) -> str:
    sdk = app.config['ONTOLOGY']
    if not isinstance(sdk, Ontology):
        return ''
    put_one_item_func = WasmInvokeFunction('put_one_item')
    aes_iv, encode_g_tilde, encrypted_ipfs_address = ECIES.encrypt_with_cbc_mode(
        ipfs_address.encode('ascii'), ont_id_acct.get_public_key_bytes())
    put_one_item_func.set_params_value(ont_id_acct.get_address(),
                                       encrypted_ipfs_address.hex(), ext,
                                       aes_iv.hex(), encode_g_tilde.hex())

    tx = sdk.wasm_vm.make_invoke_transaction(
        app.config['CONTRACT_ADDRESS_HEX'], put_one_item_func,
        payer_acct.get_address(), app.config['GAS_PRICE'],
        app.config['GAS_LIMIT'])
    tx.sign_transaction(ont_id_acct)
    tx.add_sign_transaction(payer_acct)
    try:
        return sdk.rpc.send_raw_transaction(tx)
    except Exception as e:
        print('put_one_item_to_contract: ', e)
        return ''