/// <summary> /// Asserts the computed hash matches the response content hash. /// </summary> /// <param name="computedHash">SDK computed hash.</param> /// <param name="algorithm">Hash algorithm identifier.</param> /// <param name="response">Response containing a response hash.</param> /// <exception cref="ArgumentException"> /// Throws if <paramref name="algorithm"/> is invalid. /// </exception> /// <exception cref="InvalidDataException"> /// Throws if the hashes do not match. /// </exception> private static void AssertResponseHashMatch(GetHashResult computedHash, TransactionalHashAlgorithm algorithm, Response response) { if (computedHash == default) { throw Errors.ArgumentNull(nameof(computedHash)); } if (response == default) { throw Errors.ArgumentNull(nameof(response)); } switch (algorithm) { case TransactionalHashAlgorithm.MD5: if (!Enumerable.SequenceEqual( computedHash.MD5, response.Headers.TryGetValue("Content-MD5", out byte[] md5) ? md5 : default))
/// <summary> /// Asserts the content of the given array match the response content hash. /// </summary> /// <param name="content">Content to hash.</param> /// <param name="offset">Offset to start reading content at.</param> /// <param name="count">Number of bytes to read starting from the offset.</param> /// <param name="algorithm">Hash algorithm identifier.</param> /// <param name="response">Response containing a response hash.</param> /// <exception cref="ArgumentException"> /// Throws if <paramref name="algorithm"/> is invalid. /// </exception> /// <exception cref="InvalidDataException"> /// Throws if the hashes do not match. /// </exception> public static void AssertResponseHashMatch(byte[] content, int offset, int count, TransactionalHashAlgorithm algorithm, Response response) { GetHashResult computedHash = GetHash(content, offset, count, algorithm); AssertResponseHashMatch(computedHash, algorithm, response); }
/// <summary> /// Asserts the content of the given stream match the response content hash. /// </summary> /// <param name="content">Content to hash.</param> /// <param name="algorithm">Hash algorithm identifier.</param> /// <param name="response">Response containing a response hash.</param> /// <exception cref="ArgumentException"> /// Throws if <paramref name="algorithm"/> is invalid. /// </exception> /// <exception cref="InvalidDataException"> /// Throws if the hashes do not match. /// </exception> public static void AssertResponseHashMatch(Stream content, TransactionalHashAlgorithm algorithm, Response response) { GetHashResult computedHash = GetHash(content, algorithm); AssertResponseHashMatch(computedHash, algorithm, response); }