using System;
using System.IO;
using System.Security.Cryptography;
namespace com.network_junkies.data.cryptography
{
public enum HashType
{
Aes,
Des,
Dsa,
Md5,
Rc2,
Rng,
Rsa,
Sha1,
Sha256,
Sha384,
Sha512,
TripleDes
}
public static class HashGenerator
{
public static string ComputeHash(Stream inputStream, HashType hashType)
{
if (inputStream == null) return String.Empty;
dynamic provider;
switch (hashType)
{
case HashType.Aes:
provider = new AesCryptoServiceProvider();
break;
case HashType.Des:
provider = new DESCryptoServiceProvider();
break;
case HashType.Dsa:
provider = new DSACryptoServiceProvider();
break;
case HashType.Md5:
provider = new MD5CryptoServiceProvider();
break;
case HashType.Rc2:
provider = new RC2CryptoServiceProvider();
break;
case HashType.Rng:
provider = new RNGCryptoServiceProvider();
break;
case HashType.Rsa:
provider = new RSACryptoServiceProvider();
break;
case HashType.Sha1:
provider = new SHA1CryptoServiceProvider();
break;
case HashType.Sha256:
provider = new SHA256CryptoServiceProvider();
break;
case HashType.Sha384:
provider = new SHA384CryptoServiceProvider();
break;
case HashType.Sha512:
provider = new SHA512CryptoServiceProvider();
break;
case HashType.TripleDes:
provider = new TripleDESCryptoServiceProvider();
break;
default:
return string.Empty;
}
provider.ComputeHash(inputStream);
var result = FormatHash(provider.Hash);
provider.Dispose();
return result;
}
private static string FormatHash(byte[] hash)
{
return BitConverter.ToString(hash).Replace("-", "").ToLower();
}
}
}