Created
December 2, 2019 23:30
-
-
Save fredyfx/02f481cc9cb0e7d92840f6e9ca2ad2fa to your computer and use it in GitHub Desktop.
http://www.voidcn.com/article/p-dspqubwz-yn.html and it is also at: https://blog.csdn.net/jiangfeng0213/article/details/84127742
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Security.Cryptography; | |
using System.Text; | |
namespace Hash | |
{ | |
public class Hash | |
{ | |
public Hash() { } | |
public enum HashType : int | |
{ | |
MD5, | |
SHA1, | |
SHA256, | |
SHA512 | |
} | |
public static string GetHash(string text, HashType hashType) | |
{ | |
string hashString; | |
switch (hashType) | |
{ | |
case HashType.MD5: | |
hashString = GetMD5(text); | |
break; | |
case HashType.SHA1: | |
hashString = GetSHA1(text); | |
break; | |
case HashType.SHA256: | |
hashString = GetSHA256(text); | |
break; | |
case HashType.SHA512: | |
hashString = GetSHA512(text); | |
break; | |
default: | |
hashString = "Invalid Hash Type"; | |
break; | |
} | |
return hashString; | |
} | |
public static bool CheckHash(string original, string hashString, HashType hashType) | |
{ | |
string originalHash = GetHash(original, hashType); | |
return (originalHash == hashString); | |
} | |
private static string GetMD5(string text) | |
{ | |
UnicodeEncoding UE = new UnicodeEncoding(); | |
byte[] hashValue; | |
byte[] message = UE.GetBytes(text); | |
MD5 hashString = new MD5CryptoServiceProvider(); | |
string hex = ""; | |
hashValue = hashString.ComputeHash(message); | |
foreach (byte x in hashValue) | |
{ | |
hex += String.Format("{0:x2}", x); | |
} | |
return hex; | |
} | |
private static string GetSHA1(string text) | |
{ | |
UnicodeEncoding UE = new UnicodeEncoding(); | |
byte[] hashValue; | |
byte[] message = UE.GetBytes(text); | |
SHA1Managed hashString = new SHA1Managed(); | |
string hex = ""; | |
hashValue = hashString.ComputeHash(message); | |
foreach (byte x in hashValue) | |
{ | |
hex += String.Format("{0:x2}", x); | |
} | |
return hex; | |
} | |
private static string GetSHA256(string text) | |
{ | |
UnicodeEncoding UE = new UnicodeEncoding(); | |
byte[] hashValue; | |
byte[] message = UE.GetBytes(text); | |
SHA256Managed hashString = new SHA256Managed(); | |
string hex = ""; | |
hashValue = hashString.ComputeHash(message); | |
foreach (byte x in hashValue) | |
{ | |
hex += String.Format("{0:x2}", x); | |
} | |
return hex; | |
} | |
private static string GetSHA512(string text) | |
{ | |
UnicodeEncoding UE = new UnicodeEncoding(); | |
byte[] hashValue; | |
byte[] message = UE.GetBytes(text); | |
SHA512Managed hashString = new SHA512Managed(); | |
string hex = ""; | |
hashValue = hashString.ComputeHash(message); | |
foreach (byte x in hashValue) | |
{ | |
hex += String.Format("{0:x2}", x); | |
} | |
return hex; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment