You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
124 lines
3.3 KiB
124 lines
3.3 KiB
//-----------------------------------------------------------------------
|
|
// <copyright file="HashMgr.cs" company="Origtek">
|
|
// HashMgr belongs to Copyright (c) Origtek. All rights reserved.
|
|
// </copyright>
|
|
//-----------------------------------------------------------------------
|
|
|
|
namespace OT.COM.Encryption
|
|
{
|
|
using System;
|
|
using System.Security.Cryptography;
|
|
|
|
/// <summary>
|
|
/// Manager of Hash function
|
|
/// </summary>
|
|
public partial class HashMgr
|
|
{
|
|
/// <summary>
|
|
/// For choose hash type
|
|
/// </summary>
|
|
public enum HashType
|
|
{
|
|
/// <summary>
|
|
/// MD5 algorithm
|
|
/// </summary>
|
|
HT_MD5 = 0,
|
|
|
|
/// <summary>
|
|
/// SHA1 algorithm
|
|
/// </summary>
|
|
HT_SHA1 = 1,
|
|
|
|
/// <summary>
|
|
/// SHA256 algorithm
|
|
/// </summary>
|
|
HT_SHA256 = 2,
|
|
|
|
/// <summary>
|
|
/// SHA384 algorithm
|
|
/// </summary>
|
|
HT_SHA384 = 3,
|
|
|
|
/// <summary>
|
|
/// SHA512 algorithm
|
|
/// </summary>
|
|
HT_SHA512 = 4,
|
|
}
|
|
|
|
/// <summary>
|
|
/// Generate hash function
|
|
/// </summary>
|
|
/// <param name="i_htType">
|
|
/// Hash type
|
|
/// </param>
|
|
/// <param name="i_sData">
|
|
/// string data
|
|
/// </param>
|
|
/// <returns>
|
|
/// Result string
|
|
/// </returns>
|
|
public string Generate(HashType i_htType, string i_sData)
|
|
{
|
|
return System.Text.Encoding.Unicode.GetString(
|
|
this.Gernerate(
|
|
i_htType,
|
|
System.Text.Encoding.Unicode.GetBytes(i_sData)));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Base function of Generate
|
|
/// </summary>
|
|
/// <param name="i_htType">
|
|
/// Hash type
|
|
/// </param>
|
|
/// <param name="i_byData">
|
|
/// Byte array data
|
|
/// </param>
|
|
/// <returns>
|
|
/// Result byte array
|
|
/// </returns>
|
|
public byte[] Gernerate(HashType i_htType, byte[] i_byData)
|
|
{
|
|
// This is one implementation of the abstract class MD5.
|
|
string sName = Enum.GetName(typeof(HashType), i_htType);
|
|
byte[] result = null;
|
|
object oRes = null;
|
|
|
|
string s = string.Format("System.Security.Cryptography.{0}CryptoServiceProvider", sName.Substring(3));
|
|
|
|
switch (i_htType)
|
|
{
|
|
case HashType.HT_SHA256:
|
|
{
|
|
oRes = new SHA256CryptoServiceProvider();
|
|
}
|
|
|
|
break;
|
|
case HashType.HT_SHA384:
|
|
{
|
|
oRes = new SHA384CryptoServiceProvider();
|
|
}
|
|
|
|
break;
|
|
case HashType.HT_SHA512:
|
|
{
|
|
oRes = new SHA512CryptoServiceProvider();
|
|
}
|
|
|
|
break;
|
|
default:
|
|
{
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
if (oRes != null && oRes is HashAlgorithm)
|
|
{
|
|
result = (oRes as HashAlgorithm).ComputeHash(i_byData);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
}
|