//HMAC-SHA1 算法1
public static object HMAC_SHA1(string content, string secretKey, bool raw_outut = false)
{
Encoding enc = Encoding.UTF8;
HMACSHA1 hmacsha1 = new HMACSHA1(enc.GetBytes(secretKey));
byte[] bytes = enc.GetBytes(content);
if (raw_outut)
{
return hmacsha1.ComputeHash(bytes);
}
else
{
return BitConverter.ToString(hmacsha1.ComputeHash(bytes)).Replace("-", string.Empty).ToLower();
}
}
//HMAC-SHA1 算法2
public static string ComputeHMAC_SHA1Base64(string data, string key)
{
System.Security.Cryptography.HMACSHA1 hmacsha1 = new System.Security.Cryptography.HMACSHA1(Encoding.UTF8.GetBytes(key), true);
byte[] dataBuffer = Encoding.UTF8.GetBytes(data);
byte[] hashBytes = hmacsha1.ComputeHash(dataBuffer);
return Convert.ToBase64String(hashBytes);
}
//md5
public static string Md5Sum(string strToEncrypt)
{
byte[] bs = Encoding.UTF8.GetBytes(strToEncrypt);
MD5 md5 = MD5CryptoServiceProvider.Create();
byte[] hashBytes = md5.ComputeHash(bs);
string hashString = "";
for (int i = 0; i < hashBytes.Length; i++)
{
hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');
}
return hashString.PadLeft(32, '0');
}
//16 md5
public static string GetMd5_16byte(string ConvertString)
{
string md5Pwd = string.Empty;
//使用加密服务提供程序
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
//将指定的字节子数组的每个元素的数值转换为它的等效十六进制字符串表示形式。
md5Pwd = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8);
md5Pwd = md5Pwd.Replace("-", "");
return md5Pwd;
}
//32 md5
public static string GetMd5_32byte(string str)
{
string pwd = string.Empty;
//实例化一个md5对像
MD5 md5 = MD5.Create();
// 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择
byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
// 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
for (int i = 0; i < s.Length; i++)
{
// 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符
pwd = pwd + s[i].ToString("X");
}
return pwd;
}
///
/// 图片数据的md5
///
///
///
public static string ComputeMd5Hex(byte[] data)
{
MD5CryptoServiceProvider md5CSP = new MD5CryptoServiceProvider();
byte[] dataEncrypt = data;
byte[] resultEncrypt = md5CSP.ComputeHash(dataEncrypt);
string result = string.Empty;
char pad = '0';
for (int i = 0; i < resultEncrypt.Length; i++)
{
string hex = System.Convert.ToString(resultEncrypt[i], 16);
hex = hex.PadLeft(2, pad);
result += hex;
}
return result;
}
//string Base64编码
public static string ToBase64String(string value)
{
if (value == null || value == "")
{
return "";
}
byte[] bytes = Encoding.UTF8.GetBytes(value);
return Convert.ToBase64String(bytes);
}
//string Base64 解码
public static string UnBase64String(string value)
{
if (value == null || value == "")
{
return "";
}
byte[] bytes = Convert.FromBase64String(value);
return Encoding.UTF8.GetString(bytes);
}
///
/// 由连字符分隔的32位数字xxxx-xxxx-xxxx-xxxx
///
///
public static string GetGuid()
{
Guid guid = new Guid();
guid = Guid.NewGuid();
return guid.ToString();
}
///
/// 根据GUID获取16位的唯一字符串
///
///
///
public static string GuidTo16String()
{
long i = 1;
foreach (byte b in Guid.NewGuid().ToByteArray())
i *= ((int)b + 1);
return string.Format("{0:x}", i - DateTime.Now.Ticks);
}
///
/// 根据GUID获取19位的唯一数字序列
///
///
public static long GuidToLongID()
{
byte[] buffer = Guid.NewGuid().ToByteArray();
return BitConverter.ToInt64(buffer, 0);
}
持续更新...