usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Security.Cryptography;usingSystem.Text;usingSystem.Threading.Tasks;namespacehashtest{classProgram {Static voidMain (string[] args) { stringplaintext =". Net5 Frame"; //because the input type of all hash functions is byte[], you must first convert the source data to a byte array before you calculate the hash value. byte[] Plainbyte =ASCIIEncoding.ASCII.GetBytes (plaintext); Console.WriteLine ("PlainText string:"+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 Hash Calculation/// </summary> Static voidMd5hash (byte[] plainbyte) { //MD5 itself is an abstract classMD5 MD5 = MD5. Create ();//default Implementation class: Create ("System.Security.Cryptography.MD5"); byte[] Hashbyte =Md5.computehash (Plainbyte); Console.WriteLine ("1.0:MD5 The default implementation class hashes the plaintext string:"); Console.WriteLine (ByteArrayToString (Hashbyte)); Console.WriteLine ("======================================================"); //two derived classes of MD5: System.Security.Cryptography.MD5Cng and System.Security.Cryptography.MD5CryptoServiceProviderMD5 md5cng = MD5. Create ("System.Security.Cryptography.MD5Cng"); byte[] Cnghashbyte =Md5cng.computehash (Plainbyte); Console.WriteLine ("1.1: Set MD5 static method The parameter of Create is System.Security.Cryptography.MD5Cng and the hash result is:"); Console.WriteLine (ByteArrayToString (Cnghashbyte)); Console.WriteLine ("======================================================"); MD5 MD5CryptoServiceProvider= MD5. Create ("System.Security.Cryptography.MD5CryptoServiceProvider"); byte[] Providerhashbyte =Md5cryptoserviceprovider.computehash (Plainbyte); Console.WriteLine ("1.2: Set MD5 static method The parameter of Create is System.Security.Cryptography.MD5CryptoServiceProvider and the hash result is:"); Console.WriteLine (ByteArrayToString (Providerhashbyte)); Console.WriteLine ("======================================================"); //using derived classes directly for hashingMd5cng md5cng2 =Newmd5cng (); byte[] CngHashByte2 =Md5cng.computehash (Plainbyte); Console.WriteLine ("2.0: Hash the derived class md5cng directly using MD5, with the hash result:"); Console.WriteLine (ByteArrayToString (CngHashByte2)); Console.WriteLine ("======================================================"); MD5CryptoServiceProvider Md5cryptoserviceprovider2=NewMD5CryptoServiceProvider (); byte[] ProviderHashByte2 =Md5cng.computehash (Plainbyte); Console.WriteLine ("2.1: Hash The derived class MD5CryptoServiceProvider directly using MD5, with the hash result:"); Console.WriteLine (ByteArrayToString (ProviderHashByte2)); Console.WriteLine ("======================================================"); } /// <summary> ///SHA1 Hash/// </summary> /// <param name= "Plainbyte" ></param> Static voidSha1hash (byte[] plainbyte) { //SHA1 itself is an abstract classSHA1 SHA1 = SHA1. Create ();//default Implementation class: Create ("System.Security.Cryptography.SHA1"); byte[] Hashbyte =Sha1.computehash (Plainbyte); Console.WriteLine ("1.0:SHA1 The default implementation class hashes the plaintext string:"); Console.WriteLine (ByteArrayToString (Hashbyte)); Console.WriteLine ("======================================================"); //two derived classes of SHA1: System.Security.Cryptography.SHA1Cng and System.Security.Cryptography.SHA1CryptoServiceProvider SHA1 sha1cng = SHA1. Create ("System.Security.Cryptography.SHA1Cng"); byte[] Cnghashbyte =Sha1cng.computehash (Plainbyte); Console.WriteLine ("1.1: Set SHA1 static method The parameter of Create is System.Security.Cryptography.SHA1Cng and the hash result is:"); Console.WriteLine (ByteArrayToString (Cnghashbyte)); Console.WriteLine ("======================================================"); SHA1 SHA1CryptoServiceProvider= SHA1. Create ("System.Security.Cryptography.SHA1CryptoServiceProvider"); byte[] Providerhashbyte =Sha1cryptoserviceprovider.computehash (Plainbyte); Console.WriteLine ("1.2: Set SHA1 static method The parameter of Create is System.Security.Cryptography.SHA1CryptoServiceProvider and the hash result is:"); Console.WriteLine (ByteArrayToString (Providerhashbyte)); Console.WriteLine ("======================================================"); //using derived classes directly for hashingSha1cng sha1cng2 =Newsha1cng (); byte[] CngHashByte2 =Sha1cng2.computehash (Plainbyte); Console.WriteLine ("2.0: Hash the derived class sha1cng directly using SHA1, with the hash result:"); Console.WriteLine (ByteArrayToString (CngHashByte2)); Console.WriteLine ("======================================================"); SHA1CryptoServiceProvider Sha1cryptoserviceprovider2=NewSHA1CryptoServiceProvider (); byte[] ProviderHashByte2 =Sha1cryptoserviceprovider2.computehash (Plainbyte); Console.WriteLine ("2.1: Hash The derived class SHA1CryptoServiceProvider directly using SHA1, with the hash result:"); Console.WriteLine (ByteArrayToString (ProviderHashByte2)); Console.WriteLine ("======================================================"); } Static voidSha256hash (byte[] plainbyte) { } Static voidSha384hash (byte[] plainbyte) { } Static voidSha512hash (byte[] plainbyte) { //using derived classes directly for hashingSha512cng sha512cng2 =Newsha512cng (); byte[] CngHashByte2 =Sha512cng2.computehash (Plainbyte); Console.WriteLine ("2.0: Hash the derived class sha512cng directly using SHA512, with the hash result:"); Console.WriteLine (ByteArrayToString (CngHashByte2)); Console.WriteLine ("======================================================"); Sha512cryptoserviceprovider Sha512cryptoserviceprovider2=NewSha512cryptoserviceprovider (); byte[] ProviderHashByte2 =Sha512cryptoserviceprovider2.computehash (Plainbyte); Console.WriteLine ("2.1: Hash The derived class Sha512cryptoserviceprovider directly using SHA512, with the hash result:"); Console.WriteLine (ByteArrayToString (ProviderHashByte2)); Console.WriteLine ("======================================================"); Sha512managed sha512managed=Newsha512managed (); byte[] Sha512managedhashbyte =Sha512managed.computehash (Plainbyte); Console.WriteLine ("2.2: Hash The derived class sha512managed directly using SHA512, with the hash result:"); Console.WriteLine (ByteArrayToString (Sha512managedhashbyte)); Console.WriteLine ("======================================================"); } /// <summary> ///byte array into 16 binary string/// </summary> /// <param name= "arrinput" ></param> /// <returns></returns> Static stringByteArrayToString (byte[] arrinput) { inti; StringBuilder soutput=NewStringBuilder (arrinput.length); for(i =0; I < arrinput.length-1; i++) {soutput.append (Arrinput[i]. ToString ("X2")); } returnsoutput.tostring (); } }}
. NET Hash