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;
}
}
}
分享到:
相关推荐
此类提供MD5,SHA1,SHA256,SHA512等四种算法,加密字串的长度依次增大。 加密算法实现
c#_实现MD5_SHA1_SHA256_SHA512等常用加密算法
## 说明 此程序可以进行sha1,sha256,md5的加密 在input输入框内输入所需加密的数据,然后点击"encryption"按扭即可实现加密; 默认input输入框内的数据为123456
Vue-常用加密(MD5,DES,SHA256,BASE64,SHA1,RSA)
JAVA 加密算法
对于asp这种古董级的技术,这年头想找一些有用的资料已经不容易了 ASP实现MD5,SHA1,SHA256加密算法的实现。 附DEMO演示。
加密算法的 js 实现 md4 md5 sha-1 加密算法的 js 实现 md4 md5 sha-1 加密算法的 js 实现 md4 md5 sha-1 加密算法的 js 实现 md4 md5 sha-1
用VC++写的加密算法SHA256,简单引用即可,简单方便,采用的是VS2013IDE
C语言 3DES、AES、RC6、TEA、RSA、MD5、SHA1、SHA256加密源码
C#实现字符串SHA-256加密算法,非常好用,供大家学习参考。
java实现的sha256加密算法
C++实现了SHA1加密算法,MD5加密算法和base64加解密算法类。 程序中做了测试, 绝对可用。
Java&Android; SHA1、SHA-512、SHA-384等加密算法工具类
使用C++实现的sha256和sha512加密算法,开发平台为VS2013,不能运行的自行更改编译环境,在linux下运行良好,在windows下可能出现内存泄漏,请忽略。也能得到正确结果。
md5介绍:md5deep工具就是监控linux系统下的文件,给文件生成md5值,这个包是他的源码
SHA256.zip加密算法(同Arduino,STM32适用)
C#实现字符串SHA-256加密算法工程下载,VS2013工程,使用系统带的加密库, using System.Security.Cryptography;
SHA1非对称加密算法(c语言实现).
CRC DES MD5 Sha等,各种加密算法源码
C++实现对数据的SHA-256加密算法