/// <summary> /// Creates the hash for an object. /// </summary> /// <param name="obj">The object.</param> /// <returns>Returns the hash of the object.</returns> public static RawHash Calculate(object obj) { RawHash rawHash; using (var stream = new HashStream(GetHashAlgorithm())) { GetRawBytes(stream, obj); rawHash = stream.GetHash(); } return(rawHash); }
/// <summary> /// Creates the hash for an object. /// </summary> /// <typeparam name="T">The hash algorithm type.</typeparam> /// <param name="obj">The object.</param> /// <returns>Returns the hash of the object.</returns> public static RawHash Calculate <T>(object obj) where T : HashAlgorithm { RawHash rawHash; using (var stream = new HashStream(GetHashAlgorithm(typeof(T)))) { GetRawBytes(stream, obj); rawHash = stream.GetHash(); } return(rawHash); }
/// <summary> /// Creates the hash for a set of objects. /// </summary> /// <typeparam name="T">The hash algorithm type.</typeparam> /// <param name="objects">The objects.</param> /// <returns>Returns the hash of the set of objects.</returns> public static RawHash Calculate <T>(params object[] objects) where T : HashAlgorithm { if (objects == null) { return(RawHash.Zero()); } RawHash rawHash; using (var stream = new HashStream(GetHashAlgorithm(typeof(T)))) { GetRawBytes(stream, objects); rawHash = stream.GetHash(); } return(rawHash); }
/// <summary> /// Creates the hash for a pair of Hash128 objects. Optimized specialization of the generic Calculate() methods that has been shown to be ~3x faster /// The generic function uses reflection to obtain the four 32bit fields in the Hash128 which is slow, this function uses more direct byte access /// </summary> /// <param name="hash1">The first hash to combine</param> /// <param name="hash2">The second hash to combine</param> /// <returns>Returns the combined hash of the two hashes.</returns> public static RawHash Calculate(Hash128 hash1, Hash128 hash2) { byte[] hashBytes = new byte[32]; IntPtr ptr = Marshal.AllocHGlobal(16); Marshal.StructureToPtr(hash1, ptr, false); Marshal.Copy(ptr, hashBytes, 0, 16); Marshal.StructureToPtr(hash2, ptr, true); Marshal.Copy(ptr, hashBytes, 16, 16); Marshal.FreeHGlobal(ptr); HashStream hashStream = new HashStream(GetHashAlgorithm()); PassBytesToStreamInBlocks(hashStream, hashBytes, 4); return(hashStream.GetHash()); }