'Calculate a v4 signature.
:param bytes string_to_sign: String that contains request params and
is used for calculate signature of request
:param text secret_key: Second auth key of EC2 account that is used to
sign requests'

def _calculate_signature_v4(self, string_to_sign, secret_key):

    parts = string_to_sign.split('\n')
    if ((len(parts) != 4) or (parts[0] != 'AWS4-HMAC-SHA256')):
        raise exception.Unauthorized(message=_('Invalid    EC2    signature.'))
    scope = parts[2].split('/')
    if ((len(scope) != 4) or (scope[2] != 's3') or (scope[3] != 'aws4_request')):
        raise exception.Unauthorized(message=_('Invalid    EC2    signature.'))
    def _sign(key, msg):
        return hmac.new(key, msg, hashlib.sha256).digest()
    signed = _sign(('AWS4' + secret_key).encode('utf-8'), scope[0])
    signed = _sign(signed, scope[1])
    signed = _sign(signed, scope[2])
    signed = _sign(signed, 'aws4_request')
    signature = hmac.new(signed, string_to_sign, hashlib.sha256)
    return signature.hexdigest()
