Esempio n. 1
0
    def sign_der(pri_key_string, raw_data, password=None):
        """

        signature data with private key string

        Args:
            pri_key_string:base64 private key string
            raw_data: string data
            password:

        Returns:base64 encoded signature string

        """
        try:
            password = examine_password(password)
            pri_key_string_bytes = Encoder.str_to_base64_byte(pri_key_string)
            loaded_private_key = serialization.load_der_private_key(
                pri_key_string_bytes,
                password=password,
                backend=default_backend())
            signature_string = loaded_private_key.sign(
                raw_data.encode(encoding="utf-8"), ec.ECDSA(hashes.SHA256()))
            return Encoder.bytes_to_base64_str(signature_string)
        except Exception:
            logger.exception("signature error")
            return None
Esempio n. 2
0
 def verify_signature(pub_key_string, signature, raw_data):
     try:
         loaded_public_key = serialization.load_pem_public_key(
             pub_key_string, backend=default_backend())
         loaded_public_key.verify(Encoder.str_to_base64_byte(signature),
                                  raw_data, ec.ECDSA(hashes.SHA256()))
         return True
     except Exception:
         logger.exception("verify signature error")
         return False
Esempio n. 3
0
def test_server_chain():
    os.chdir(root_dir)
    server_web3 = chain.default_web3
    # chain.utils.deploy_contract(config.chain.core_contract)
    buyertrans = BuyerAgent(server_web3, config.chain.core_contract)
    print(server_web3.eth.defaultAccount)
    # desc_hash_base64 = 'AQkKqDxtNIRJ+1V82J5lP2/fRj/zbJ+2n0GzUF52Wsc='
    # desc_hash = Encoder.str_to_base64_byte(desc_hash_base64)
    # public_key = RSACipher.load_public_key()
    # print('pubkey ' + str(len(public_key)))
    # order_info = OrderInfo(
    #     desc_hash=desc_hash,
    #     buyer_rsa_pubkey=public_key,
    #     seller=buyertrans.web3.eth.defaultAccount,
    #     proxy=buyertrans.web3.eth.defaultAccount,
    #     secondary_proxy=buyertrans.web3.eth.defaultAccount,
    #     proxy_value=10,
    #     value=20,
    #     time_allowed=100
    # )
    # test_server_id = buyertrans.place_order(order_info)
    # print(test_server_id)
    # buyertrans.withdraw_order(test_server_id)
    # print(buyertrans.query_order(test_server_id))
    order_num = buyertrans.get_order_num()
    print(order_num)
    latest_order_info = buyertrans.query_order(order_num - 1)
    private_key_file_path = join_with_root(config.wallet.private_key_file)
    password_path = join_with_root(config.wallet.private_key_password_file)
    with open(password_path) as f:
        password = f.read()
    priv_key, pub_key = ECCipher.load_key_pair_from_private_key(
        private_key_file_path, password)
    pub_key_bytes = Encoder.str_to_base64_byte(pub_key)
    pub_key_loaded = load_der_public_key(pub_key_bytes,
                                         backend=default_backend())
    # print(Encoder.str_to_base64_byte(pub_key))
    # print(len(Encoder.str_to_base64_byte(pub_key)))
    # print(pub_key_bytes_to_addr(Encoder.str_to_base64_byte(pub_key)))
    # print(len(pub_key_bytes_to_addr(Encoder.str_to_base64_byte(pub_key))))
    print(get_addr_from_public_key(pub_key_loaded))
    print(to_bytes(hexstr=latest_order_info[2]))
    print(len(to_bytes(hexstr=latest_order_info[2])))
Esempio n. 4
0
    def get_public_key_from_private_key(pri_key_string, password=None):
        """
        get public key from private key string

        Args:
            pri_key_string: private key string
            password: read default value from config file

        Returns:
            base64(public key)
        """

        password = examine_password(password)
        pri_key_string_bytes = Encoder.str_to_base64_byte(pri_key_string)

        loaded_private_key = serialization.load_der_private_key(
            pri_key_string_bytes, password=password, backend=default_backend())
        puk = loaded_private_key.public_key()
        serialized_public = puk.public_bytes(
            encoding=serialization.Encoding.DER,
            format=serialization.PublicFormat.SubjectPublicKeyInfo)
        return Encoder.bytes_to_base64_str(serialized_public)
Esempio n. 5
0
 def test_str_to_base64_byte(self):
     bs = Encoder.str_to_base64_byte(self.string)
     self.assertEqual(self.b, bs)