using System;
using SystemIO;
using SystemSecurityCryptography;
namespace Common
{
/// <summary>
///Copyright (C) kwklover(邝伟科)
///File name:Hashercs
///Author:邝伟科 Version: Date:年月日
///Description:哈希(不可逆)加密通用类库函数
/// </summary>
public class Hasher
{
private byte[] _HashKey; //哈希密钥存储变量
private string _HashText; //待加密的字符串
public Hasher()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 哈希密钥
/// </summary>
public byte[] HashKey
{
set
{
_HashKey=value;
}
get
{
return _HashKey;
}
}
/// <summary>
/// 需要产生加密哈希的字符串
/// </summary>
public string HashText
{
set
{
_HashText=value;
}
get
{
return _HashText;
}
}
/// <summary>
/// 使用HMACSHA类产生长度为 字节的哈希序列需提供相应的密钥接受任何大小的密钥
/// </summary>
/// <returns></returns>
public string HMACSHAHasher()
{
byte[] HmacKey=HashKey;
byte[] HmacData=SystemTextEncodingUTFGetBytes(HashText);
HMACSHA Hmac = new HMACSHA(HmacKey);
CryptoStream cs = new CryptoStream(StreamNull Hmac CryptoStreamModeWrite);
csWrite(HmacData HmacDataLength);
csClose();
byte[] Result=HmacHash;
return ConvertToBaseString(Result); //返回长度为字节字符串
}
/// <summary>
/// 使用MACTripleDES类产生长度为 字节的哈希序列需提供相应的密钥密钥长度可为 或 字节的密钥
/// </summary>
/// <returns></returns>
public string MACTripleDESHasher()
{
byte[] MacKey=HashKey;
byte[] MacData=SystemTextEncodingUTFGetBytes(HashText);
MACTripleDES Mac=new MACTripleDES(MacKey);
byte[] Result=MacComputeHash(MacData);
return ConvertToBaseString(Result); //返回长度为字节字符串
}
/// <summary>
/// 使用MDCryptoServiceProvider类产生哈希值不需要提供密钥
/// </summary>
/// <returns></returns>
public string MDHasher()
{
byte[] MDData=SystemTextEncodingUTFGetBytes(HashText);
MD Md=new MDCryptoServiceProvider();
byte[] Result=MdComputeHash(MDData);
return ConvertToBaseString(Result); //返回长度为字节字符串
}
/// <summary>
/// 使用SHAManaged类产生长度为位哈希值不需要提供密钥
/// </summary>
/// <returns></returns>
public string SHAManagedHasher()
{
byte[] SHAData=SystemTextEncodingUTFGetBytes(HashText);
SHAManaged Sha=new SHAManaged();
byte[] Result=ShaComputeHash(SHAData);
return ConvertToBaseString(Result); //返回长度为字节的字符串
}
/// <summary>
/// 使用SHAManaged类产生长度为位哈希值不需要提供密钥
/// </summary>
/// <returns></returns>
public string SHAManagedHasher()
{
byte[] SHAData=SystemTextEncodingUTFGetBytes(HashText);
SHAManaged Sha=new SHAManaged();
byte[] Result=ShaComputeHash(SHAData);
return ConvertToBaseString(Result); //返回长度为字节的字符串
}
/// <summary>
/// 使用SHAManaged类产生长度为位哈希值不需要提供密钥
/// </summary>
/// <returns></returns>
public string SHAManagedHasher()
{
byte[] SHAData=SystemTextEncodingUTFGetBytes(HashText);
SHAManaged Sha=new SHAManaged();
byte[] Result=ShaComputeHash(SHAData);
return ConvertToBaseString(Result); //返回长度为字节的字符串
}
/// <summary>
/// 使用SHAManaged类产生长度为位哈希值不需要提供密钥
/// </summary>
/// <returns></returns>
public string SHAManagedHasher()
{
byte[] SHAData=SystemTextEncodingUTFGetBytes(HashText);
SHAManaged Sha=new SHAManaged();
byte[] Result=ShaComputeHash(SHAData);
return ConvertToBaseString(Result); //返回长度为字节的字符串
}
}
}