def _parse_error_info_from_response_body(logger, response_body): error_code_to_return = SDK_UNKNOWN_SERVER_ERROR # TODO handle if response_body is too big error_message_to_return = "ServerResponseBody: " + str(response_body) try: body_obj = json.loads(ensure_string(response_body)) if 'Code' in body_obj: error_code_to_return = body_obj['Code'] if 'Message' in body_obj: error_message_to_return = body_obj['Message'] except ValueError: # failed to parse body as json format logger.warning('Failed to parse response as json format. Response:%s', str(response_body)) pass return error_code_to_return, error_message_to_return
def sign_string(self, source, access_secret): if platform.system() != "Windows": from Crypto.Signature import PKCS1_v1_5 from Crypto.Hash import SHA256 from Crypto.PublicKey import RSA key = RSA.importKey(b64_decode_bytes(ensure_bytes(access_secret))) h = SHA256.new(ensure_bytes(source)) signer = PKCS1_v1_5.new(key) signed_bytes = signer.sign(h) signed_base64 = b64_encode_bytes(signed_bytes) signature = ensure_string(signed_base64).replace('\n', '') return signature else: message = "auth type [publicKeyId] is disabled in Windows " \ "because 'pycrypto' is not supported, we will resolve " \ "this soon" raise ClientException(msg=message)
def sign_string(self, source, secret): source = ensure_bytes(source) secret = ensure_bytes(secret) h = hmac.new(secret, source, hashlib.sha1) signature = ensure_string(b64_encode_bytes(h.digest()).strip()) return signature
def md5_sum(content): content_bytes = ensure_bytes(content) md5_bytes = hashlib.md5(content_bytes).digest() return ensure_string(base64.standard_b64encode(md5_bytes))