using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; namespace ServiceStack { public static class ByteArrayExtensions { public static bool AreEqual(this byte[] b1, byte[] b2) { if (b1 == b2) return true; if (b1 == null || b2 == null) return false; if (b1.Length != b2.Length) return false; for (var i = 0; i < b1.Length; i++) { if (b1[i] != b2[i]) return false; } return true; } public static byte[] ToSha1Hash(this byte[] bytes) { using (var sha1 = SHA1.Create()) { return sha1.ComputeHash(bytes); } } } public class ByteArrayComparer : IEqualityComparer { public static ByteArrayComparer Instance = new ByteArrayComparer(); public bool Equals(byte[] left, byte[] right) { if (left == null || right == null) { return left == right; } return left.SequenceEqual(right); } public int GetHashCode(byte[] key) { if (key == null) throw new ArgumentNullException(nameof(key)); return key.Sum(b => b); } } }