.net 哈希

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace HashTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string plainText = ".Net5 框架";
            // 因为所有哈希函数的输入类型都是 Byte[],所以必须先将源数据转换为字节数组后再计算哈希值。
            byte[] plainByte = ASCIIEncoding.ASCII.GetBytes(plainText);
            Console.WriteLine("明文字符串:" + plainText);
            Console.WriteLine("======================================================");

            Console.ForegroundColor = ConsoleColor.Red;
            MD5Hash(plainByte);
            Console.ForegroundColor = ConsoleColor.Green;
            SHA1Hash(plainByte);
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            SHA512Hash(plainByte);
            Console.ReadKey();
        }

        /// <summary>
        /// MD5哈希计算
        /// </summary>
        static void MD5Hash(byte[] plainByte)
        {            
            // MD5本身是一个抽象类
            MD5 md5 = MD5.Create();   // 默认实现类:Create("System.Security.Cryptography.MD5");         
            byte[] hashByte = md5.ComputeHash(plainByte);
            Console.WriteLine("1.0:MD5默认实现类对明文字符串进行哈希计算后的结果:");
            Console.WriteLine(ByteArrayToString(hashByte));
            Console.WriteLine("======================================================");

            // MD5的两个派生类:System.Security.Cryptography.MD5Cng 和 System.Security.Cryptography.MD5CryptoServiceProvider
            MD5 md5Cng = MD5.Create("System.Security.Cryptography.MD5Cng");
            byte[] cngHashByte = md5Cng.ComputeHash(plainByte);
            Console.WriteLine("1.1:设置MD5的静态方法Create的参数为System.Security.Cryptography.MD5Cng,哈希结果为:");
            Console.WriteLine(ByteArrayToString(cngHashByte));
            Console.WriteLine("======================================================");

            MD5 md5CryptoServiceProvider = MD5.Create("System.Security.Cryptography.MD5CryptoServiceProvider");
            byte[] providerHashByte = md5CryptoServiceProvider.ComputeHash(plainByte);
            Console.WriteLine("1.2:设置MD5的静态方法Create的参数为System.Security.Cryptography.MD5CryptoServiceProvider,哈希结果为:");
            Console.WriteLine(ByteArrayToString(providerHashByte));
            Console.WriteLine("======================================================");

            // 直接使用派生类进行哈希
            MD5Cng md5Cng2 = new MD5Cng();
            byte[] cngHashByte2 = md5Cng.ComputeHash(plainByte);
            Console.WriteLine("2.0:直接使用MD5的派生类MD5Cng进行哈希,哈希结果为:");
            Console.WriteLine(ByteArrayToString(cngHashByte2));
            Console.WriteLine("======================================================");

            MD5CryptoServiceProvider md5CryptoServiceProvider2 = new MD5CryptoServiceProvider();
            byte[] providerHashByte2 = md5Cng.ComputeHash(plainByte);
            Console.WriteLine("2.1:直接使用MD5的派生类MD5CryptoServiceProvider进行哈希,哈希结果为:");
            Console.WriteLine(ByteArrayToString(providerHashByte2));
            Console.WriteLine("======================================================");
        }

        /// <summary>
        /// SHA1哈希
        /// </summary>
        /// <param name="plainByte"></param>
        static void SHA1Hash(byte[] plainByte)
        {
            // SHA1本身是一个抽象类
            SHA1 sha1 = SHA1.Create();   // 默认实现类:Create("System.Security.Cryptography.SHA1");         
            byte[] hashByte = sha1.ComputeHash(plainByte);
            Console.WriteLine("1.0:SHA1默认实现类对明文字符串进行哈希计算后的结果:");
            Console.WriteLine(ByteArrayToString(hashByte));
            Console.WriteLine("======================================================");

            // SHA1的两个派生类:System.Security.Cryptography.SHA1Cng 和 System.Security.Cryptography.SHA1CryptoServiceProvider
            SHA1 sha1Cng = SHA1.Create("System.Security.Cryptography.SHA1Cng");
            byte[] cngHashByte = sha1Cng.ComputeHash(plainByte);
            Console.WriteLine("1.1:设置SHA1的静态方法Create的参数为System.Security.Cryptography.SHA1Cng,哈希结果为:");
            Console.WriteLine(ByteArrayToString(cngHashByte));
            Console.WriteLine("======================================================");

            SHA1 sha1CryptoServiceProvider = SHA1.Create("System.Security.Cryptography.SHA1CryptoServiceProvider");
            byte[] providerHashByte = sha1CryptoServiceProvider.ComputeHash(plainByte);
            Console.WriteLine("1.2:设置SHA1的静态方法Create的参数为System.Security.Cryptography.SHA1CryptoServiceProvider,哈希结果为:");
            Console.WriteLine(ByteArrayToString(providerHashByte));
            Console.WriteLine("======================================================");

            // 直接使用派生类进行哈希
            SHA1Cng sha1Cng2 = new SHA1Cng();
            byte[] cngHashByte2 = sha1Cng2.ComputeHash(plainByte);
            Console.WriteLine("2.0:直接使用SHA1的派生类SHA1Cng进行哈希,哈希结果为:");
            Console.WriteLine(ByteArrayToString(cngHashByte2));
            Console.WriteLine("======================================================");

            SHA1CryptoServiceProvider sha1CryptoServiceProvider2 = new SHA1CryptoServiceProvider();
            byte[] providerHashByte2 = sha1CryptoServiceProvider2.ComputeHash(plainByte);
            Console.WriteLine("2.1:直接使用SHA1的派生类SHA1CryptoServiceProvider进行哈希,哈希结果为:");
            Console.WriteLine(ByteArrayToString(providerHashByte2));
            Console.WriteLine("======================================================"); 
        }

        static void SHA256Hash(byte[] plainByte)
        {

        }

        static void SHA384Hash(byte[] plainByte)
        {

        }

        static void SHA512Hash(byte[] plainByte)
        {
            // 直接使用派生类进行哈希
            SHA512Cng sha512Cng2 = new SHA512Cng();
            byte[] cngHashByte2 = sha512Cng2.ComputeHash(plainByte);
            Console.WriteLine("2.0:直接使用SHA512的派生类SHA512Cng进行哈希,哈希结果为:");
            Console.WriteLine(ByteArrayToString(cngHashByte2));
            Console.WriteLine("======================================================");

            SHA512CryptoServiceProvider sha512CryptoServiceProvider2 = new SHA512CryptoServiceProvider();
            byte[] providerHashByte2 = sha512CryptoServiceProvider2.ComputeHash(plainByte);
            Console.WriteLine("2.1:直接使用SHA512的派生类SHA512CryptoServiceProvider进行哈希,哈希结果为:");
            Console.WriteLine(ByteArrayToString(providerHashByte2));
            Console.WriteLine("======================================================");

            SHA512Managed sha512Managed = new SHA512Managed();
            byte[] sha512ManagedHashByte = sha512Managed.ComputeHash(plainByte);
            Console.WriteLine("2.2:直接使用SHA512的派生类SHA512Managed进行哈希,哈希结果为:");
            Console.WriteLine(ByteArrayToString(sha512ManagedHashByte));
            Console.WriteLine("======================================================");
        }

        /// <summary>
        /// 字节数组转化成16进制字符串
        /// </summary>
        /// <param name="arrInput"></param>
        /// <returns></returns>
        static string ByteArrayToString(byte[] arrInput)
        {
            int i;
            StringBuilder sOutput = new StringBuilder(arrInput.Length);
            for (i = 0; i < arrInput.Length - 1; i++)
            {
                sOutput.Append(arrInput[i].ToString("X2"));
            }
            return sOutput.ToString();
        }
    }
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。