ComputeHash() protected static method

Computes RFC 2104-compliant HMAC signature.
protected static ComputeHash ( byte data, string secretkey, SigningAlgorithm algorithm ) : string
data byte
secretkey string
algorithm SigningAlgorithm
return string
Beispiel #1
0
        private static void SignHttp(IRequest request, RequestMetrics metrics, string awsAccessKeyId, string awsSecretAccessKey)
        {
            SigningAlgorithm algorithm = SigningAlgorithm.HmacSHA256;
            string           text      = Guid.NewGuid().ToString();
            string           formattedCurrentTimestampRFC = AWSSDKUtils.FormattedCurrentTimestampRFC822;
            bool             flag = IsHttpsRequest(request);

            flag = false;
            request.Headers["Date"]       = formattedCurrentTimestampRFC;
            request.Headers["X-Amz-Date"] = formattedCurrentTimestampRFC;
            request.Headers.Remove("X-Amzn-Authorization");
            string text2 = request.Endpoint.Host;

            if (!request.Endpoint.IsDefaultPort)
            {
                text2 = text2 + ":" + request.Endpoint.Port;
            }
            request.Headers["host"] = text2;
            byte[] array = null;
            string text3;

            if (flag)
            {
                request.Headers["x-amz-nonce"] = text;
                text3 = formattedCurrentTimestampRFC + text;
                array = Encoding.UTF8.GetBytes(text3);
            }
            else
            {
                Uri endpoint = request.Endpoint;
                if (!string.IsNullOrEmpty(request.ResourcePath))
                {
                    endpoint = new Uri(request.Endpoint, request.ResourcePath);
                }
                text3 = request.HttpMethod + "\n" + GetCanonicalizedResourcePath(endpoint) + "\n" + GetCanonicalizedQueryString(request.Parameters) + "\n" + GetCanonicalizedHeadersForStringToSign(request) + "\n" + GetRequestPayload(request);
                array = CryptoUtilFactory.CryptoInstance.ComputeSHA256Hash(Encoding.UTF8.GetBytes(text3));
            }
            metrics.AddProperty(Metric.StringToSign, text3);
            string        str           = AbstractAWSSigner.ComputeHash(array, awsSecretAccessKey, algorithm);
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append(flag ? "AWS3-HTTPS" : "AWS3");
            stringBuilder.Append(" ");
            stringBuilder.Append("AWSAccessKeyId=" + awsAccessKeyId + ",");
            stringBuilder.Append("Algorithm=" + algorithm.ToString() + ",");
            if (!flag)
            {
                stringBuilder.Append(GetSignedHeadersComponent(request) + ",");
            }
            stringBuilder.Append("Signature=" + str);
            string value = stringBuilder.ToString();

            request.Headers["X-Amzn-Authorization"] = value;
        }
        public override void Sign(IRequest request, IClientConfig clientConfig, RequestMetrics metrics, string awsAccessKeyId, string awsSecretAccessKey)
        {
            if (string.IsNullOrEmpty(awsAccessKeyId))
            {
                throw new ArgumentOutOfRangeException("awsAccessKeyId", "The AWS Access Key ID cannot be NULL or a Zero length string");
            }
            string formattedTimestampRFC = AWSSDKUtils.GetFormattedTimestampRFC822(0);

            request.Headers.Add("X-Amz-Date", formattedTimestampRFC);
            string str = AbstractAWSSigner.ComputeHash(formattedTimestampRFC, awsSecretAccessKey, SigningAlgorithm.HmacSHA1);

            request.Headers.Add("Authorization", "AWS " + awsAccessKeyId + ":" + str);
        }
        public override void Sign(IRequest request, IClientConfig clientConfig, RequestMetrics metrics, string awsAccessKeyId, string awsSecretAccessKey)
        {
            if (string.IsNullOrEmpty(awsAccessKeyId))
            {
                throw new ArgumentOutOfRangeException("awsAccessKeyId", "The AWS Access Key ID cannot be NULL or a Zero length string");
            }
            request.Parameters["AWSAccessKeyId"]   = awsAccessKeyId;
            request.Parameters["SignatureVersion"] = clientConfig.SignatureVersion;
            request.Parameters["SignatureMethod"]  = clientConfig.SignatureMethod.ToString();
            request.Parameters["Timestamp"]        = AWSSDKUtils.FormattedCurrentTimestampISO8601;
            request.Parameters.Remove("Signature");
            string text = AWSSDKUtils.CalculateStringToSignV2(request.Parameters, request.Endpoint.AbsoluteUri);

            metrics.AddProperty(Metric.StringToSign, text);
            string value = AbstractAWSSigner.ComputeHash(text, awsSecretAccessKey, clientConfig.SignatureMethod);

            request.Parameters["Signature"] = value;
        }
Beispiel #4
0
        private static void SignHttps(IRequest request, IClientConfig clientConfig, RequestMetrics metrics, string awsAccessKeyId, string awsSecretAccessKey)
        {
            string text = Guid.NewGuid().ToString();
            string formattedCurrentTimestampRFC = AWSSDKUtils.FormattedCurrentTimestampRFC822;
            string text2 = formattedCurrentTimestampRFC + text;

            metrics.AddProperty(Metric.StringToSign, text2);
            string        str           = AbstractAWSSigner.ComputeHash(text2, awsSecretAccessKey, clientConfig.SignatureMethod);
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append("AWS3-HTTPS").Append(" ");
            stringBuilder.Append("AWSAccessKeyId=" + awsAccessKeyId + ",");
            stringBuilder.Append("Algorithm=" + clientConfig.SignatureMethod.ToString() + ",");
            stringBuilder.Append("SignedHeaders=x-amz-date;x-amz-nonce,");
            stringBuilder.Append("Signature=" + str);
            request.Headers["X-Amzn-Authorization"] = stringBuilder.ToString();
            request.Headers["x-amz-nonce"]          = text;
            request.Headers["X-Amz-Date"]           = formattedCurrentTimestampRFC;
        }