internal string GetExpectedCanonicalRequestForChunkedTrailersSigningTest() { var trailingHeaders = new Dictionary <string, string> { { "x-amz-foo", "bar" } }; return(string.Join('\n', "PUT", "/examplebucket/chunkObject.txt", "", "content-encoding:aws-chunked", "content-length:" + ChunkedUploadWrapperStream.ComputeChunkedContentLength(Chunk1Size + Chunk2Size, ChunkedUploadWrapperStream.V4A_SIGNATURE_LENGTH, trailingHeaders, CoreChecksumAlgorithm.NONE).ToString(), "host:s3.amazonaws.com", "x-amz-content-sha256:STREAMING-AWS4-ECDSA-P256-SHA256-PAYLOAD-TRAILER", "x-amz-date:20150830T123600Z", "x-amz-decoded-content-length:" + (Chunk1Size + Chunk2Size).ToString(), "x-amz-region-set:us-east-1", "x-amz-storage-class:REDUCED_REDUNDANCY", "x-amz-trailer:x-amz-foo", "", "content-encoding;content-length;host;x-amz-content-sha256;x-amz-date;x-amz-decoded-content-length;x-amz-region-set;x-amz-storage-class;x-amz-trailer", "STREAMING-AWS4-ECDSA-P256-SHA256-PAYLOAD-TRAILER")); }
public static string SetRequestBodyHash(IRequest request) { string value = null; bool flag = request.Headers.TryGetValue("X-Amz-Content-SHA256", out value); if (flag && !request.UseChunkEncoding) { return(value); } if (request.UseChunkEncoding) { value = "STREAMING-AWS4-HMAC-SHA256-PAYLOAD"; if (request.Headers.ContainsKey("Content-Length")) { request.Headers["X-Amz-Decoded-Content-Length"] = request.Headers["Content-Length"]; long originalLength = long.Parse(request.Headers["Content-Length"], CultureInfo.InvariantCulture); request.Headers["Content-Length"] = ChunkedUploadWrapperStream.ComputeChunkedContentLength(originalLength).ToString(CultureInfo.InvariantCulture); } if (request.Headers.ContainsKey("Content-Encoding")) { string text = request.Headers["Content-Encoding"]; if (!text.Contains("aws-chunked")) { request.Headers["Content-Encoding"] = text + ", " + "aws-chunked"; } } else { request.Headers["Content-Encoding"] = "aws-chunked"; } } else if (request.ContentStream != null) { value = request.ComputeContentStreamHash(); } else { byte[] requestPayloadBytes = GetRequestPayloadBytes(request); value = AWSSDKUtils.ToHex(CryptoUtilFactory.CryptoInstance.ComputeSHA256Hash(requestPayloadBytes), lowercase: true); } if (value != null) { if (flag) { request.Headers["X-Amz-Content-SHA256"] = value; } else { request.Headers.Add("X-Amz-Content-SHA256", value); } } return(value); }
/// <summary> /// If the caller has already set the x-amz-content-sha256 header with a pre-computed /// content hash, or it is present as ContentStreamHash on the request instance, return /// the value to be used in request canonicalization. /// If not set as a header or in the request, attempt to compute a hash based on /// inspection of the style of the request content. /// </summary> /// <param name="request"></param> /// <returns> /// The computed hash, whether already set in headers or computed here. Null /// if we were not able to compute a hash. /// </returns> public static string SetRequestBodyHash(IRequest request) { string computedContentHash = null; if (request.Headers.TryGetValue(XAmzContentSha256, out computedContentHash)) { return(computedContentHash); } if (request.UseChunkEncoding) { computedContentHash = StreamingBodySha256; if (request.Headers.ContainsKey("Content-Length")) { // substitute the originally declared content length with the true size of // the data we'll upload, which is inflated with chunk metadata request.Headers[XAmzDecodedContentLength] = request.Headers["Content-Length"]; var originalContentLength = long.Parse(request.Headers["Content-Length"], CultureInfo.InvariantCulture); request.Headers["Content-Length"] = ChunkedUploadWrapperStream.ComputeChunkedContentLength(originalContentLength).ToString(CultureInfo.InvariantCulture); } request.Headers["Content-Encoding"] = "aws-chunked"; } else { if (request.ContentStream != null) { computedContentHash = request.ComputeContentStreamHash(); } else { byte[] payloadHashBytes; if (request.Content != null) { payloadHashBytes = CryptoUtilFactory.CryptoInstance.ComputeSHA256Hash(request.Content); } else { var payload = request.UseQueryString ? "" : GetRequestPayload(request); payloadHashBytes = CryptoUtilFactory.CryptoInstance.ComputeSHA256Hash(Encoding.UTF8.GetBytes(payload)); } computedContentHash = AWSSDKUtils.ToHex(payloadHashBytes, true); } } if (computedContentHash != null) { request.Headers.Add(XAmzContentSha256, computedContentHash); } return(computedContentHash); }
internal string GetExpectedCanonicalRequestForChunkedSigningTest() { return(String.Join('\n', "PUT", "/examplebucket/chunkObject.txt", "", "content-encoding:aws-chunked", "content-length:" + ChunkedUploadWrapperStream.ComputeChunkedContentLength(Chunk1Size + Chunk2Size, ChunkedUploadWrapperStream.V4A_SIGNATURE_LENGTH).ToString(), "host:s3.amazonaws.com", "x-amz-content-sha256:STREAMING-AWS4-ECDSA-P256-SHA256-PAYLOAD", "x-amz-date:20150830T123600Z", "x-amz-decoded-content-length:" + (Chunk1Size + Chunk2Size).ToString(), "x-amz-region-set:us-east-1", "x-amz-storage-class:REDUCED_REDUNDANCY", "", "content-encoding;content-length;host;x-amz-content-sha256;x-amz-date;x-amz-decoded-content-length;x-amz-region-set;x-amz-storage-class", "STREAMING-AWS4-ECDSA-P256-SHA256-PAYLOAD")); }