Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class HashHelper
- {
- public static string ToMd5(string value)
- {
- using (var crypto = MD5.Create())
- {
- string result = Hashing(value, crypto);
- return result;
- }
- }
- public static string ToSha1(string value)
- {
- using (var crypto = SHA1.Create())
- {
- string result = Hashing(value, crypto);
- return result;
- }
- }
- public static string ToSha256(string value)
- {
- using (var crypto = SHA256.Create())
- {
- string result = Hashing(value, crypto);
- return result;
- }
- }
- public static string ToSha384(string value)
- {
- using (var crypto = SHA384.Create())
- {
- string result = Hashing(value, crypto);
- return result;
- }
- }
- public static string ToSha512(string value)
- {
- using (var crypto = SHA512.Create())
- {
- string result = Hashing(value, crypto);
- return result;
- }
- }
- private static string Hashing(string value, HashAlgorithm crypto)
- {
- //將字串編碼成 UTF8 位元組陣列
- var bytes = Encoding.UTF8.GetBytes(value);
- //取得雜湊值位元組陣列
- var hash = crypto.ComputeHash(bytes);
- StringBuilder result = new StringBuilder();
- foreach (var item in hash)
- {
- result.Append(item.ToString("X2"));
- }
- return result.ToString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement