62,243
社区成员




[code=csharp]
private static byte[] MD5(String data) throws IOException {
byte[] bytes = null;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
bytes = md.digest(data.getBytes(Constants.CHARSET_UTF8));
} catch (GeneralSecurityException gse) {
String msg = getStringFromException(gse);
throw new IOException(msg);
}
return bytes;
}
private static String B(byte[] bytes) {
StringBuilder sign = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(bytes[i] & 0xFF);
if (hex.length() == 1) {
sign.append("0");
}
sign.append(hex.toUpperCase());
}
return sign.toString();
}
public class Digest
{
public static string MD5Of(string text)
{
return MD5Of(text, Encoding.UTF8);
}
public static string MD5Of(string text, Encoding enc)
{
return HashOf<MD5CryptoServiceProvider>(text, enc);
}
public static string SHA1Of(string text)
{
return SHA1Of(text, Encoding.UTF8);
}
public static string SHA1Of(string text, Encoding enc)
{
return HashOf<SHA1CryptoServiceProvider>(text, enc);
}
public static string SHA384Of(string text)
{
return SHA384Of(text, Encoding.UTF8);
}
public static string SHA384Of(string text, Encoding enc)
{
return HashOf<SHA384CryptoServiceProvider>(text, enc);
}
public static string SHA512Of(string text)
{
return SHA512Of(text, Encoding.UTF8);
}
public static string SHA512Of(string text, Encoding enc)
{
return HashOf<SHA512CryptoServiceProvider>(text, enc);
}
public static string SHA256Of(string text)
{
return SHA256Of(text, Encoding.UTF8);
}
public static string SHA256Of(string text, Encoding enc)
{
return HashOf<SHA256CryptoServiceProvider>(text, enc);
}
public static string HashOf<TP>(string text, Encoding enc)
where TP : HashAlgorithm, new()
{
return BitConverter.ToString(HashBytes<TP>(text, enc)).Replace("-", "");
}
public static byte[] HashBytes<TP>(string text, Encoding enc)
where TP : HashAlgorithm, new()
{
var buffer = enc.GetBytes(text);
var provider = new TP();
return provider.ComputeHash(buffer);
}
}