(#7) Move StreamExtensions to child namespace

This commit is contained in:
Oliver Booth 2021-03-03 14:41:15 +00:00
parent 8e70823241
commit ef9dc29e42
2 changed files with 53 additions and 32 deletions

View File

@ -1,32 +0,0 @@
using System;
using System.IO;
using System.Security.Cryptography;
namespace X10D
{
/// <summary>
/// Extension methods for <see cref="Stream" />.
/// </summary>
public static class StreamExtensions
{
/// <summary>
/// Returns the hash of a stream using the specified hashing algorithm.
/// </summary>
/// <typeparam name="T">A <see cref="HashAlgorithm" /> derived type.</typeparam>
/// <param name="stream">The stream whose hash is to be computed.</param>
/// <returns>Returns a <see cref="byte" /> array representing the hash of the stream.</returns>
/// <exception cref="ArgumentNullException"><paramref name="stream" /> is <see langword="null" />.</exception>
public static byte[] GetHash<T>(this Stream stream)
where T : HashAlgorithm
{
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
var create = typeof(T).GetMethod("Create", Array.Empty<Type>());
using var crypt = (T)create?.Invoke(null, null);
return crypt?.ComputeHash(stream);
}
}
}

View File

@ -0,0 +1,53 @@
using System;
using System.IO;
using System.Security.Cryptography;
namespace X10D.StreamExtensions
{
/// <summary>
/// Extension methods for <see cref="Stream" />.
/// </summary>
public static class StreamExtensions
{
/// <summary>
/// Returns the hash of a stream using the specified hash algorithm.
/// </summary>
/// <typeparam name="T">A <see cref="HashAlgorithm" /> derived type.</typeparam>
/// <param name="stream">The stream whose hash is to be computed.</param>
/// <returns>A <see cref="byte" /> array representing the hash of the stream.</returns>
/// <exception cref="ArgumentNullException"><paramref name="stream" /> is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">
/// <typeparamref name="T" /> does not offer a static <c>Create</c> method.
/// -or-
/// An invocation to the static <c>Create</c> method defined in <typeparamref name="T" /> returned
/// <see langword="null" />.
/// </exception>
/// <exception cref="ObjectDisposedException">The stream has already been disposed.</exception>
/// <remarks>This method consumes the stream from its current position!.</remarks>
public static byte[] GetHash<T>(this Stream stream)
where T : HashAlgorithm
{
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
var type = typeof(T);
var create = type.GetMethod("Create", Array.Empty<Type>());
if (create is null)
{
throw new TypeInitializationException(type.FullName,
new ArgumentException(ExceptionMessages.HashAlgorithmNoCreateMethod));
}
using var crypt = create.Invoke(null, null) as T;
if (crypt is null)
{
throw new TypeInitializationException(type.FullName,
new ArgumentException(ExceptionMessages.HashAlgorithmCreateReturnedNull));
}
return crypt.ComputeHash(stream);
}
}
}