Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Security.Cryptography;
- using System.Text;
- using MongoDB.Bson;
- namespace Yambr.Core.Helpers
- {
- public static class MD5Helper
- {
- public static string GetHashByJson(this object sourse)
- {
- return GetHash(sourse.ToJson());
- }
- public static string GetHash(this string source)
- {
- using (var md5Hash = MD5.Create())
- {
- return GetMd5Hash(md5Hash, source);
- }
- }
- public static string GetHash(this byte[] source)
- {
- using (var md5Hash = MD5.Create())
- {
- return GetMd5Hash(md5Hash, source);
- }
- }
- /*не протестировано
- public static string GetHash(this Stream source)
- {
- using (var md5Hash = MD5.Create())
- {
- return GetMd5Hash(md5Hash, source);
- }
- }*/
- public static bool VerifyHash(string source, string hash)
- {
- using (var md5Hash = MD5.Create())
- {
- return VerifyMd5Hash(md5Hash, source, hash);
- }
- }
- private static string GetMd5Hash(HashAlgorithm md5Hash, string input)
- {
- var bytes = Encoding.UTF8.GetBytes(input);
- return GetMd5Hash(md5Hash, bytes);
- }
- private static string GetMd5Hash(HashAlgorithm md5Hash, byte[] bytes)
- {
- // Convert the input string to a byte array and compute the hash.
- var data = md5Hash.ComputeHash(bytes);
- return Md5ToString(data);
- }
- /*private static string GetMd5Hash(HashAlgorithm md5Hash, Stream stream)
- {
- // Convert the input string to a byte array and compute the hash.
- var data = md5Hash.ComputeHash(stream);
- return Md5ToString(data);
- }*/
- private static string Md5ToString(IEnumerable<byte> data)
- {
- // Create a new Stringbuilder to collect the bytes
- // and create a string.
- var sBuilder = new StringBuilder();
- // Loop through each byte of the hashed data
- // and format each one as a hexadecimal string.
- foreach (var t in data)
- {
- sBuilder.Append(t.ToString("x2"));
- }
- // Return the hexadecimal string.
- return sBuilder.ToString();
- }
- // Verify a hash against a string.
- private static bool VerifyMd5Hash(HashAlgorithm md5Hash, string input, string hash)
- {
- // Hash the input.
- var hashOfInput = GetMd5Hash(md5Hash, input);
- // Create a StringComparer an compare the hashes.
- var comparer = StringComparer.OrdinalIgnoreCase;
- return 0 == comparer.Compare(hashOfInput, hash);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment