CryptoHelper.cs
上传用户:scs668
上传日期:2019-09-29
资源大小:43k
文件大小:3k
源码类别:

C#编程

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Security.Cryptography;
  5. public class CryptoHelper
  6. {
  7.     /// <summary>
  8.     /// 复合 Hash:string --> byte[] --> hashed byte[] --> base64 string
  9.     /// </summary>
  10.     /// <param name="s"></param>
  11.     /// <returns></returns>
  12.     public static string ComputeHashString(string s)
  13.     {
  14.         return ToBase64String(ComputeHash(ConvertStringToByteArray(s)));
  15.     }
  16.     public static byte[] ComputeHash(byte[] buf)
  17.     {
  18.         //return ((HashAlgorithm)CryptoConfig.CreateFromName("SHA1")).ComputeHash(buf);
  19.         return SHA1.Create().ComputeHash(buf);
  20.     }
  21.     /// <summary>
  22.     /// //System.Convert.ToBase64String
  23.     /// </summary>
  24.     /// <param name="buf"></param>
  25.     /// <returns></returns>
  26.     public static string ToBase64String(byte[] buf)
  27.     {
  28.         return System.Convert.ToBase64String(buf);
  29.     }
  30.     public static byte[] FromBase64String(string s)
  31.     {
  32.         return System.Convert.FromBase64String(s);
  33.     }
  34.     /// <summary>
  35.     /// //Encoding.UTF8.GetBytes(s)
  36.     /// </summary>
  37.     /// <param name="s"></param>
  38.     /// <returns></returns>
  39.     public static byte[] ConvertStringToByteArray(String s)
  40.     {
  41.         return Encoding.UTF8.GetBytes(s);//gb2312
  42.     }
  43.     public static string ConvertByteArrayToString(byte[] buf)
  44.     {
  45.         //return System.Text.Encoding.GetEncoding("utf-8").GetString(buf);
  46.         return Encoding.UTF8.GetString(buf);
  47.     }
  48.     /// <summary>
  49.     /// 字节数组转换为十六进制字符串
  50.     /// </summary>
  51.     /// <param name="buf"></param>
  52.     /// <returns></returns>
  53.     public static string ByteArrayToHexString(byte[] buf)
  54.     {
  55.         StringBuilder sb = new StringBuilder();
  56.         for (int i = 0; i < buf.Length; i++)
  57.         {
  58.             sb.Append(buf[i].ToString("X").Length == 2 ? buf[i].ToString("X") : "0" + buf[i].ToString("X"));
  59.         }
  60.         return sb.ToString();
  61.     }
  62.     /// <summary>
  63.     /// 十六进制字符串转换为字节数组
  64.     /// </summary>
  65.     /// <param name="s"></param>
  66.     /// <returns></returns>
  67.     public static byte[] HexStringToByteArray(string s)
  68.     {
  69.         Byte[] buf = new byte[s.Length / 2];
  70.         for (int i = 0; i < buf.Length; i++)
  71.         {
  72.             buf[i] = (byte)(Char2Hex(s.Substring(i * 2, 1)) * 0x10 + Char2Hex(s.Substring(i * 2 + 1, 1)));
  73.         }
  74.         return buf;
  75.     }
  76.     private static byte Char2Hex(string chr)
  77.     {
  78.         switch (chr)
  79.         {
  80.             case "0":
  81.                 return 0x00;
  82.             case "1":
  83.                 return 0x01;
  84.             case "2":
  85.                 return 0x02;
  86.             case "3":
  87.                 return 0x03;
  88.             case "4":
  89.                 return 0x04;
  90.             case "5":
  91.                 return 0x05;
  92.             case "6":
  93.                 return 0x06;
  94.             case "7":
  95.                 return 0x07;
  96.             case "8":
  97.                 return 0x08;
  98.             case "9":
  99.                 return 0x09;
  100.             case "A":
  101.                 return 0x0a;
  102.             case "B":
  103.                 return 0x0b;
  104.             case "C":
  105.                 return 0x0c;
  106.             case "D":
  107.                 return 0x0d;
  108.             case "E":
  109.                 return 0x0e;
  110.             case "F":
  111.                 return 0x0f;
  112.         }
  113.         return 0x00;
  114.     }
  115. }