Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public partial class HelperExtensions
- {
- public static bool IsMd5(this hash o)
- {
- return o != null && o.Bytes != null && o.Length == 16;
- }
- public static bool IsValidMd5(this hash o)
- {
- return IsMd5(o) && !o.Equals(hash.ZeroesMd5) && !o.Equals(hash.ZeroLengthMd5);
- }
- }
- public sealed class hash
- {
- // :: data ::
- public readonly byte[] Bytes;
- // :: ctors ::
- public hash(byte[] value)
- {
- //if (value.Length == 0) throw new Exception("Zero byte-sequence for hash");
- Bytes = value;
- }
- public hash(string value)
- {
- Bytes = ToBytes(value);
- }
- public hash(hash o)
- {
- Bytes = o.Bytes;
- }
- // ::access::
- /*public byte[] GetBytesCopy()
- {
- return CopyBytes(Bytes);
- }*/
- public int Length { get { return Bytes.Length; } }
- // ::operators::
- public static implicit operator hash(byte[] val)
- {
- return new hash(val);
- }
- /* много с ним гемора. Скорее следует воспринимать hash как string, чем string как hash
- * public static implicit operator hash(string val)
- {
- return new hash(val);
- }*/
- public static explicit operator hash(string val)
- {
- return new hash(val);
- }
- public static bool operator ==(hash a, hash b)
- {
- if (ReferenceEquals(a, b))
- return true;
- if (ReferenceEquals(a, null) || ReferenceEquals(b, null))
- return false;
- if (ReferenceEquals(a.Bytes, b.Bytes))
- return true;
- if (ReferenceEquals(a.Bytes, null) || ReferenceEquals(b.Bytes, null))
- return false;
- return a.Bytes.Length == b.Bytes.Length && a.Bytes.SequenceEqual(b.Bytes);
- }
- public static bool operator !=(hash a, hash b)
- {
- return !(a == b);
- }
- // ::overrides::
- public override string ToString()
- {
- return ToString(Bytes);
- }
- public override bool Equals(object obj)
- {
- return this == obj as hash;
- }
- public override int GetHashCode()
- {
- return Bytes.Length == 0 ? 235 : (Bytes[0] | Bytes[Bytes.Length - 1] << 8 | Bytes.Length << 24);
- }
- //------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- // ::static::
- /*public static byte[] CopyBytes(byte[] bytes)
- {
- var res = new byte[bytes.Length];
- Buffer.BlockCopy(bytes, 0, res, 0, bytes.Length);
- return res;
- }*/
- public static string ToString(byte[] bytes)
- {
- return BitConverter.ToString(bytes).Replace("-", String.Empty);
- }
- private static bool Has0x(string str)
- {
- return str.StartsWith("0x", StringComparison.OrdinalIgnoreCase);
- }
- public static byte[] ToBytes(string str)
- {
- var i_str = Has0x(str) ? 2 : 0;
- //if (str.Length < 32) throw new Exception("Too short char-sequence for md5, need 32 at least.");
- var bytes = new byte[(str.Length - i_str) / 2];
- for (var i_bytes = 0; i_bytes < bytes.Length; ++i_bytes, i_str += 2)
- bytes[i_bytes] = byte.Parse(str.Substring(i_str, 2), NumberStyles.HexNumber);
- return bytes;
- }
- private static readonly Regex HexValidator = new Regex(@"^(0x)?([a-fA-F0-9])+$");
- private static readonly Regex Base64Validator = new Regex(@"^[a-zA-Z0-9\+/]*={0,3}$");
- //
- public static bool IsValidHex(string str)
- {
- return !string.IsNullOrEmpty(str) && (str.Length % 2 == 0) && HexValidator.IsMatch(str);
- }
- public static bool IsValidBase64(string str)
- {
- return !string.IsNullOrEmpty(str) && (str.Length % 4 == 0) && Base64Validator.IsMatch(str);
- }
- public static bool IsValidMd5(string str)
- {
- return IsValidHex(str) && str.Length == (Has0x(str) ? 34 : 32);
- }
- public static hash TryParse(string str)
- {
- if (IsValidHex(str)) return new hash(str);
- return IsValidBase64(str) ? new hash(Convert.FromBase64String(str)) : null;
- }
- public static hash TryParseMd5(string str)
- {
- if (IsValidMd5(str)) return new hash(str);
- if (!IsValidBase64(str)) return null;
- var bytes = Convert.FromBase64String(str);
- return bytes.Length != 16 ? null : new hash(bytes);
- }
- public static readonly hash ZeroesMd5 = new hash("0x00000000000000000000000000000000");
- public static readonly hash ZeroLengthMd5 = new hash("0xD41D8CD98F00B204E9800998ECF8427E");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement