I would like to know, if my design of this class is good OOP.
Should I implement for every hash algorithm a separate class?
I'm asking this, because the HashService can be used with a KeyedHashAlgorithm which wouldn't work correctly?!
public class HashService : IDisposable
{
public HashService(HashAlgorithm algorithm)
{
HashAlgorithm = algorithm;
Encoder = Encoding.UTF8;
}
protected HashAlgorithm HashAlgorithm { get; set; }
/// <summary>
/// The size, in bits, of the computed hash code.
/// </summary>
public int HashSize
{
get { return HashAlgorithm.HashSize; }
}
/// <summary>
/// Gets or sets the encoding for strings.
/// </summary>
public Encoding Encoder { get; set; }
public string ComputeHash(string input)
{
byte[] bytes = Encoder.GetBytes(input);
byte[] hash = ComputeHash(bytes);
return ToHex(hash); // method impl. omitted
}
/// <summary>
/// Computes the hash value for the specified byte array
/// </summary>
/// <param name="buffer">The input to compute the hash code for.</param>
/// <returns>The computed hash code.</returns>
public byte[] ComputeHash(byte[] buffer)
{
return HashAlgorithm.ComputeHash(buffer);
}
public byte[] ComputeHash(byte[] buffer, int offset, int count)
{
return HashAlgorithm.ComputeHash(buffer, offset, count);
}
public byte[] ComputeHash(Stream inputStream)
{
return HashAlgorithm.ComputeHash(inputStream);
}
#region Implementation of IDisposable
// omitted
#endregion
// ReSharper disable InconsistentNaming
public static HashService CreateMd5()
{
return new HashService(new MD5CryptoServiceProvider());
}
public static HashService CreateRIPEMD160()
{
return new HashService(new RIPEMD160Managed());
}
public static HashService CreateSHA256()
{
return new HashService(new SHA256Managed());
}
public static HashService CreateSHA384()
{
return new HashService(new SHA384Managed());
}
public static HashService CreateSHA512()
{
return new HashService(new SHA512Managed());
}
public static HashService CreateSHA1()
{
return new HashService(new SHA1Managed());
}
public static HashService CreateCrc32()
{
return new HashService(new Crc32Managed());
}
// ReSharper restore InconsistentNaming
}