/// <summary> /// Calculates the length in bytes of a TrailingChecksumWrapperStream initialized /// with the given trailing headers and optional checksum /// </summary> /// <param name="trailingHeaders">Dictionary of trailing headers</param> /// <param name="checksumAlgorithm">Trailing checksum</param> /// <param name="baseStreamLength">Length of the base stream in bytes</param> /// <returns>Length of a TrailingChecksumWrapperStream with given parameters, in bytes</returns> public static long CalculateLength(IDictionary <string, string> trailingHeaders, CoreChecksumAlgorithm checksumAlgorithm, long baseStreamLength) { var prefixLength = baseStreamLength.ToString("X", CultureInfo.InvariantCulture).Length; var trailingHeaderLength = 0; if (trailingHeaders != null) { foreach (var key in trailingHeaders.Keys) { if (checksumAlgorithm != CoreChecksumAlgorithm.NONE && ChecksumUtils.GetChecksumHeaderKey(checksumAlgorithm) == key) { trailingHeaderLength += key.Length + CryptoUtilFactory.GetChecksumBase64Length(checksumAlgorithm) + HEADER_ROW_PADDING_LENGTH; } else { trailingHeaderLength += key.Length + trailingHeaders[key].Length + HEADER_ROW_PADDING_LENGTH; } } } return(prefixLength + NEWLINE_LENGTH + baseStreamLength + NEWLINE_LENGTH + EMPTY_CHUNK_LENGTH + trailingHeaderLength + NEWLINE_LENGTH); }
public void CalculateChecksumTest(CoreChecksumAlgorithm algorithm, string content, string expectedBase64Checksum) { var contentBytes = Encoding.Default.GetBytes(content); var computedChecksum = Convert.ToBase64String(CryptoUtilFactory.GetChecksumInstance(algorithm).ComputeHash(contentBytes)); Assert.AreEqual(expectedBase64Checksum, computedChecksum); }
public static string GenerateChecksumForContent(string content, bool fBase64Encode) { byte[] array = CryptoUtilFactory.get_CryptoInstance().ComputeMD5Hash(Encoding.UTF8.GetBytes(content)); if (fBase64Encode) { return(Convert.ToBase64String(array)); } return(BitConverter.ToString(array).Replace("-", string.Empty)); }
/// <summary> /// Initiates a stream wrapper to append trailing headers to an unsigned payload, /// with a trailing checksum /// </summary> /// <param name="baseStream">Stream to wrap</param> /// <param name="trailingHeaders">Header keys and values to append after the stream's conent</param> /// <param name="checksumAlgorithm">Algorithm to use to calculate the stream's checksum</param> public TrailingHeadersWrapperStream( Stream baseStream, IDictionary <string, string> trailingHeaders, CoreChecksumAlgorithm checksumAlgorithm) : this(baseStream, trailingHeaders) { if (checksumAlgorithm != CoreChecksumAlgorithm.NONE) { _checksumAlgorithm = checksumAlgorithm; _hashAlgorithm = CryptoUtilFactory.GetChecksumInstance(checksumAlgorithm); } }
internal static void SignRequest(IRequest request, RequestMetrics metrics, string awsAccessKeyId, string awsSecretAccessKey) { request.get_Headers()["X-Amz-Date"] = AWSSDKUtils.get_FormattedCurrentTimestampRFC822(); string text = BuildStringToSign(request); metrics.AddProperty(17, (object)text); string str = CryptoUtilFactory.get_CryptoInstance().HMACSign(text, awsSecretAccessKey, 0); string value = "AWS " + awsAccessKeyId + ":" + str; request.get_Headers()["Authorization"] = value; }
public static S3PostUploadSignedPolicy GetSignedPolicy(string policy, AWSCredentials credentials) { ImmutableCredentials credentials2 = credentials.GetCredentials(); string text = Convert.ToBase64String(credentials2.get_UseToken() ? addTokenToPolicy(policy, credentials2.get_Token()) : Encoding.UTF8.GetBytes(policy.Trim())); string signature = CryptoUtilFactory.get_CryptoInstance().HMACSign(Encoding.UTF8.GetBytes(text), credentials2.get_SecretKey(), 0); return(new S3PostUploadSignedPolicy { Policy = text, Signature = signature, AccessKeyId = credentials2.get_AccessKey(), SecurityToken = credentials2.get_Token(), SignatureVersion = "2" }); }
/// <summary> /// Initializes a chunked upload stream with one or more trailing headers, /// which may include a trailing checksum header /// </summary> /// <param name="stream">Stream to wrap</param> /// <param name="wrappedStreamBufferSize">Size of buffer used for reading from stream</param> /// <param name="headerSigningResult">SigV4 or SigV4a signing result for the request's headers</param> /// <param name="trailingChecksum">Algorithm to use to calculate the stream's checksum</param> /// <param name="trailingHeaders">Trailing headers to append after the wrapped stream</param> public ChunkedUploadWrapperStream(Stream stream, int wrappedStreamBufferSize, AWSSigningResultBase headerSigningResult, CoreChecksumAlgorithm trailingChecksum, IDictionary <string, string> trailingHeaders) : this(stream, wrappedStreamBufferSize, headerSigningResult) { if (trailingChecksum != CoreChecksumAlgorithm.NONE) { _trailingChecksum = trailingChecksum; _hashAlgorithm = CryptoUtilFactory.GetChecksumInstance(trailingChecksum); } _trailingHeadersConsumed = false; _trailingHeaders = trailingHeaders; }
/// <summary> /// Computes the total size of the data payload, including the chunk metadata /// and optional trailing headers. Called externally so as to be able to set /// the correct Content-Length header value. /// </summary> /// <param name="originalLength">Length of the wrapped stream</param> /// <param name="signatureLength">Length of the signature for each chunk, in bytes</param> /// <param name="trailingHeaders">Optional trailing headers</param> /// <param name="trailingChecksum">Optional checksum algorithm for a trailing header</param> /// <returns>Total size of the wrapped payload, in bytes</returns> public static long ComputeChunkedContentLength(long originalLength, int signatureLength, IDictionary <string, string> trailingHeaders, CoreChecksumAlgorithm trailingChecksum) { if (originalLength < 0) { throw new ArgumentOutOfRangeException("originalLength", "Expected 0 or greater value for originalLength."); } int trailingHeaderLength = 0; long chunkedContentLength; // Calculate the size of the chunked content, before trailing headers/checksum if (originalLength == 0) { chunkedContentLength = CalculateChunkHeaderLength(0, signatureLength); } else { var maxSizeChunks = originalLength / DefaultChunkSize; var remainingBytes = originalLength % DefaultChunkSize; chunkedContentLength = maxSizeChunks * CalculateChunkHeaderLength(DefaultChunkSize, signatureLength) + (remainingBytes > 0 ? CalculateChunkHeaderLength(remainingBytes, signatureLength) : 0) + CalculateChunkHeaderLength(0, signatureLength); } if (trailingHeaders?.Count > 0) { foreach (var key in trailingHeaders.Keys) { // If the trailing checksum key is already in dictionary, use the // expected length since the checksum value may not be set yet. if (trailingChecksum != CoreChecksumAlgorithm.NONE && ChecksumUtils.GetChecksumHeaderKey(trailingChecksum) == key) { trailingHeaderLength += key.Length + CryptoUtilFactory.GetChecksumBase64Length(trailingChecksum) + HEADER_ROW_PADDING_LENGTH; } else { trailingHeaderLength += key.Length + trailingHeaders[key].Length + HEADER_ROW_PADDING_LENGTH; } } trailingHeaderLength += TRAILING_HEADER_SIGNATURE_KEY.Length + signatureLength + HEADER_ROW_PADDING_LENGTH; } return(chunkedContentLength + trailingHeaderLength); }
/// <summary> /// Attempts to select and then calculate the checksum for a request /// </summary> /// <param name="request">Request to calculate the checksum for</param> /// <param name="checksumAlgorithm">Checksum algorithm to use, specified on the request using a service-specific enum</param> /// <param name="fallbackToMD5">If checksumAlgorithm is <see cref="CoreChecksumAlgorithm.NONE"/>, /// this flag controls whether or not to fallback to using a MD5 to generate a checksum. /// </param> public static void SetRequestChecksum(IRequest request, string checksumAlgorithm, bool fallbackToMD5 = true) { var coreChecksumAlgoritm = ChecksumUtils.ConvertToCoreChecksumAlgorithm(checksumAlgorithm); if (coreChecksumAlgoritm == CoreChecksumAlgorithm.NONE) { if (fallbackToMD5) { SetRequestChecksumMD5(request); } return; } var checksumHeaderKey = GetChecksumHeaderKey(coreChecksumAlgoritm); // If the user provided a precalculated header for this checksum, don't recalculate it if (request.Headers.TryGetValue(checksumHeaderKey, out var checksumHeaderValue)) { if (!string.IsNullOrEmpty(checksumHeaderValue)) { return; } } if (request.UseChunkEncoding || (request.DisablePayloadSigning ?? false)) { // Add the checksum key to the trailing headers, but not the value // because we won't know it until the wrapper stream is fully read, // and we only want to read the wrapper stream once. // // The header key is required upfront for calculating the total length of // the wrapper stream, which we need to send as the Content-Length header // before the wrapper stream is transmitted. request.TrailingHeaders.Add(checksumHeaderKey, string.Empty); request.SelectedChecksum = coreChecksumAlgoritm; } else // calculate and set the checksum in the request headers { request.Headers[checksumHeaderKey] = CalculateChecksumForRequest(CryptoUtilFactory.GetChecksumInstance(coreChecksumAlgoritm), request); } }
internal static string ComputeEncodedMD5FromEncodedString(string base64EncodedString) { byte[] array = Convert.FromBase64String(base64EncodedString); return(Convert.ToBase64String(CryptoUtilFactory.get_CryptoInstance().ComputeMD5Hash(array))); }
public HashingWrapperSHA1() : base(CryptoUtilFactory.GetChecksumInstance(CoreChecksumAlgorithm.SHA1)) { }
public HashingWrapperCRC32() : base(CryptoUtilFactory.GetChecksumInstance(CoreChecksumAlgorithm.CRC32)) { }