def generate_der_keys(password=None): """ generate private ,public key string tuple Args: password: Returns: private ,public key string tuple """ password = examine_password(password) private_key = ec.generate_private_key(ec.SECP256K1(), default_backend()) serialized_private = private_key.private_bytes( encoding=serialization.Encoding.DER, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.BestAvailableEncryption( password)) pri_key_string = Encoder.bytes_to_base64_str(serialized_private) logger.debug("pri_key_string:%s" % pri_key_string) puk = private_key.public_key() serialized_public = puk.public_bytes( encoding=serialization.Encoding.DER, format=serialization.PublicFormat.SubjectPublicKeyInfo) pub_key_string = Encoder.bytes_to_base64_str(serialized_public) logger.debug("pub_key_string:%s" % pub_key_string) return pri_key_string, pub_key_string
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
def sign(pri_key_string, raw_data, password=None): try: password = examine_password(password) loaded_private_key = serialization.load_pem_private_key( pri_key_string, password=password, backend=default_backend()) signature_string = loaded_private_key.sign( raw_data, ec.ECDSA(hashes.SHA256())) return Encoder.bytes_to_base64_str(signature_string) except Exception: logger.exception("pem sign error") return None
def get_all_orders(self): num = self.contract.call().numOrders() data = dict() for i in range(num): order = self.contract.call().orderRecords(i + 1) market_hash = Encoder.bytes_to_base64_str(order[0]) rsa = Encoder.bytes_to_base64_str(order[1])[:10] + "..." status = order[10] item = data.get(market_hash, []) seller_addr = order[3] buyer_addr = order[2] item.append({ 'public_key': rsa, 'status': status, 'order_id': i + 1, 'seller_addr': seller_addr, 'buyer_addr': buyer_addr }) data[market_hash] = item return data
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)
def test_bytes_to_base64_str(self): self.assertEqual(Encoder.bytes_to_base64_str(self.b), self.string)