HMAC-SHA1 算法
//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 算法
//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算法
/// <summary>
/// 图片数据的md5
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
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编码/解码
//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);
}
Guid
/// <summary>
/// 由连字符分隔的32位数字xxxx-xxxx-xxxx-xxxx
/// </summary>
/// <returns></returns>
public static string GetGuid()
{
Guid guid = new Guid();
guid = Guid.NewGuid();
return guid.ToString();
}
/// <summary>
/// 根据GUID获取16位的唯一字符串
/// </summary>
/// <param name=\"guid\"></param>
/// <returns></returns>
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);
}
/// <summary>
/// 根据GUID获取19位的唯一数字序列
/// </summary>
/// <returns></returns>
public static long GuidToLongID()
{
byte[] buffer = Guid.NewGuid().ToByteArray();
return BitConverter.ToInt64(buffer, 0);
}
UrlEncode 编码
using System.Text;
using System.Collections;
public static class WebUtility
{
// Fields
private static char[] _htmlEntityEndingChars = new char[] { ';', '&' };
private const char HIGH_SURROGATE_START = '\ud800';
private const char LOW_SURROGATE_END = '\udfff';
private const char LOW_SURROGATE_START = '\udc00';
private const int UNICODE_PLANE00_END = 0xffff;
private const int UNICODE_PLANE01_START = 0x10000;
private const int UNICODE_PLANE16_END = 0x10ffff;
private const int UnicodeReplacementChar = 0xfffd;
private static void ConvertSmpToUtf16(uint smpChar, out char leadingSurrogate, out char trailingSurrogate)
{
int num = ((int)smpChar) - 0x10000;
leadingSurrogate = (char)((num / 0x400) + 0xd800);
trailingSurrogate = (char)((num % 0x400) + 0xdc00);
}
private static int HexToInt(char h)
{
if ((h >= '0') && (h <= '9'))
{
return (h - '0');
}
if ((h >= 'a') && (h <= 'f'))
{
return ((h - 'a') + 10);
}
if ((h >= 'A') && (h <= 'F'))
{
return ((h - 'A') + 10);
}
return -1;
}
private static char IntToHex(int n)
{
if (n <= 9)
{
return (char)(n + 0x30);
}
return (char)((n - 10) + 0x41);
}
private static bool IsUrlSafeChar(char ch)
{
if ((((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z'))) || ((ch >= '0') && (ch <= '9')))
{
return true;
}
switch (ch)
{
case '(':
case ')':
case '*':
case '-':
case '.':
case '_':
case '!':
return true;
}
return false;
}
public static string UrlEncode(string value)
{
if (value == null)
{
return null;
}
byte[] bytes = Encoding.UTF8.GetBytes(value);
return Encoding.UTF8.GetString(UrlEncode(bytes, 0, bytes.Length, false));
}
private static bool ValidateUrlEncodingParameters(byte[] bytes, int offset, int count)
{
if ((bytes == null) && (count == 0))
{
return false;
}
if (bytes == null)
{
//throw new ArgumentNullException("bytes");
}
if ((offset < 0) || (offset > bytes.Length))
{
//throw new ArgumentOutOfRangeException("offset");
}
if ((count < 0) || ((offset + count) > bytes.Length))
{
//throw new ArgumentOutOfRangeException("count");
}
return true;
}
private static byte[] UrlEncode(byte[] bytes, int offset, int count)
{
if (!ValidateUrlEncodingParameters(bytes, offset, count))
{
return null;
}
int num = 0;
int num2 = 0;
for (int i = 0; i < count; i++)
{
char ch = (char)bytes[offset + i];
if (ch == ' ')
{
num++;
}
else if (!IsUrlSafeChar(ch))
{
num2++;
}
}
if ((num == 0) && (num2 == 0))
{
return bytes;
}
byte[] buffer = new byte[count + (num2 * 2)];
int num4 = 0;
for (int j = 0; j < count; j++)
{
byte num6 = bytes[offset + j];
char ch2 = (char)num6;
if (IsUrlSafeChar(ch2))
{
buffer[num4++] = num6;
}
else if (ch2 == ' ')
{
buffer[num4++] = 0x2b;
}
else
{
buffer[num4++] = 0x25;
buffer[num4++] = (byte)IntToHex((num6 >> 4) & 15);
buffer[num4++] = (byte)IntToHex(num6 & 15);
}
}
return buffer;
}
private static byte[] UrlEncode(byte[] bytes, int offset, int count, bool alwaysCreateNewReturnValue)
{
byte[] buffer = UrlEncode(bytes, offset, count);
if ((alwaysCreateNewReturnValue && (buffer != null)) && (buffer == bytes))
{
return (byte[])buffer.Clone();
}
return buffer;
}
public static byte[] UrlEncodeToBytes(byte[] value, int offset, int count)
{
return UrlEncode(value, offset, count, true);
}
}
持续更新...