Advertisement
vilgelmbb

hash helper

Dec 14th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.18 KB | None | 0 0
  1. public partial class HelperExtensions
  2.     {
  3.         public static bool IsMd5(this hash o)
  4.         {
  5.             return o != null && o.Bytes != null && o.Length == 16;
  6.         }
  7.  
  8.         public static bool IsValidMd5(this hash o)
  9.         {
  10.             return IsMd5(o) && !o.Equals(hash.ZeroesMd5) && !o.Equals(hash.ZeroLengthMd5);
  11.         }
  12.     }
  13.  
  14.     public sealed class hash
  15.     {
  16.         // :: data ::
  17.         public readonly byte[] Bytes;
  18.  
  19.         // :: ctors ::
  20.         public hash(byte[] value)
  21.         {
  22.             //if (value.Length == 0) throw new Exception("Zero byte-sequence for hash");
  23.             Bytes = value;
  24.         }
  25.  
  26.         public hash(string value)
  27.         {
  28.             Bytes = ToBytes(value);
  29.         }
  30.  
  31.         public hash(hash o)
  32.         {
  33.             Bytes = o.Bytes;
  34.         }
  35.  
  36.         // ::access::
  37.         /*public byte[] GetBytesCopy()
  38.         {
  39.             return CopyBytes(Bytes);
  40.         }*/
  41.  
  42.         public int Length { get { return Bytes.Length; } }
  43.  
  44.         // ::operators::
  45.         public static implicit operator hash(byte[] val)
  46.         {
  47.             return new hash(val);
  48.         }
  49.  
  50.         /* много с ним гемора. Скорее следует воспринимать hash как string, чем string как hash
  51.          * public static implicit operator hash(string val)
  52.         {
  53.             return new hash(val);
  54.         }*/
  55.  
  56.         public static explicit operator hash(string val)
  57.         {
  58.             return new hash(val);
  59.         }
  60.  
  61.         public static bool operator ==(hash a, hash b)
  62.         {
  63.             if (ReferenceEquals(a, b))
  64.                 return true;
  65.             if (ReferenceEquals(a, null) || ReferenceEquals(b, null))
  66.                 return false;
  67.             if (ReferenceEquals(a.Bytes, b.Bytes))
  68.                 return true;
  69.             if (ReferenceEquals(a.Bytes, null) || ReferenceEquals(b.Bytes, null))
  70.                 return false;
  71.             return a.Bytes.Length == b.Bytes.Length && a.Bytes.SequenceEqual(b.Bytes);
  72.         }
  73.  
  74.         public static bool operator !=(hash a, hash b)
  75.         {
  76.             return !(a == b);
  77.         }
  78.  
  79.         // ::overrides::
  80.         public override string ToString()
  81.         {
  82.             return ToString(Bytes);
  83.         }
  84.  
  85.         public override bool Equals(object obj)
  86.         {
  87.             return this == obj as hash;
  88.         }
  89.  
  90.         public override int GetHashCode()
  91.         {
  92.             return Bytes.Length == 0 ? 235 : (Bytes[0] | Bytes[Bytes.Length - 1] << 8 | Bytes.Length << 24);
  93.         }
  94.        
  95.         //------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  96.         // ::static::
  97.         /*public static byte[] CopyBytes(byte[] bytes)
  98.         {
  99.             var res = new byte[bytes.Length];
  100.             Buffer.BlockCopy(bytes, 0, res, 0, bytes.Length);
  101.             return res;
  102.         }*/
  103.  
  104.         public static string ToString(byte[] bytes)
  105.         {
  106.             return BitConverter.ToString(bytes).Replace("-", String.Empty);
  107.         }
  108.  
  109.         private static bool Has0x(string str)
  110.         {
  111.             return str.StartsWith("0x", StringComparison.OrdinalIgnoreCase);
  112.         }
  113.  
  114.         public static byte[] ToBytes(string str)
  115.         {
  116.             var i_str = Has0x(str) ? 2 : 0;
  117.             //if (str.Length < 32) throw new Exception("Too short char-sequence for md5, need 32 at least.");
  118.             var bytes = new byte[(str.Length - i_str) / 2];
  119.             for (var i_bytes = 0; i_bytes < bytes.Length; ++i_bytes, i_str += 2)
  120.                 bytes[i_bytes] = byte.Parse(str.Substring(i_str, 2), NumberStyles.HexNumber);
  121.             return bytes;
  122.         }
  123.  
  124.         private static readonly Regex HexValidator = new Regex(@"^(0x)?([a-fA-F0-9])+$");
  125.         private static readonly Regex Base64Validator = new Regex(@"^[a-zA-Z0-9\+/]*={0,3}$");
  126.         //
  127.         public static bool IsValidHex(string str)
  128.         {
  129.             return !string.IsNullOrEmpty(str) && (str.Length % 2 == 0) && HexValidator.IsMatch(str);
  130.         }
  131.  
  132.         public static bool IsValidBase64(string str)
  133.         {
  134.             return !string.IsNullOrEmpty(str) && (str.Length % 4 == 0) && Base64Validator.IsMatch(str);
  135.         }
  136.  
  137.         public static bool IsValidMd5(string str)
  138.         {
  139.             return IsValidHex(str) && str.Length == (Has0x(str) ? 34 : 32);
  140.         }
  141.  
  142.         public static hash TryParse(string str)
  143.         {
  144.             if (IsValidHex(str)) return new hash(str);
  145.             return IsValidBase64(str) ? new hash(Convert.FromBase64String(str)) : null;
  146.         }
  147.  
  148.         public static hash TryParseMd5(string str)
  149.         {
  150.             if (IsValidMd5(str)) return new hash(str);
  151.             if (!IsValidBase64(str)) return null;
  152.             var bytes = Convert.FromBase64String(str);
  153.             return bytes.Length != 16 ? null : new hash(bytes);
  154.         }
  155.        
  156.         public static readonly hash ZeroesMd5 = new hash("0x00000000000000000000000000000000");
  157.         public static readonly hash ZeroLengthMd5 = new hash("0xD41D8CD98F00B204E9800998ECF8427E");
  158.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement