/// <summary> /// Compares the hash of the given streams using the specified hashing algo /// </summary> /// <param name="stream1">First stream</param> /// <param name="stream2">Second stream</param> /// <param name="algo">Hashing algo to use</param> /// <returns>True if the hashes are equal</returns> static bool CompareHash(Stream stream1, Stream stream2, HashingAlgos algo = HashingAlgos.MD5) { var hash1 = ComputeHash(stream1, algo); var hash2 = ComputeHash(stream2, algo); return(IsEqual(hash1, hash2)); }
/// <summary> /// Computes the hash from the given byte array /// </summary> /// <param name="bytes">Array whose hash is to be computed</param> /// <param name="algo">Hashing algo to use</param> /// <returns></returns> static byte[] ComputeHash(byte[] bytes, HashingAlgos algo = HashingAlgos.MD5) { switch (algo) { case HashingAlgos.MD5: return(MD5.Create().ComputeHash(bytes)); case HashingAlgos.SHA256: return(SHA256.Create().ComputeHash(bytes)); case HashingAlgos.SHA512: return(SHA512.Create().ComputeHash(bytes)); default: throw null; } }
/// <summary> /// Compares the given hash and the hash computed from the given stream using the specified hashing algo /// </summary> /// <param name="hash">Hash against which the stream's computed hash is to be compared</param> /// <param name="stream">Stream whose computed hash is to be compared</param> /// <param name="algo">Hashing algo to use</param> /// <returns>True if the hashes are equal</returns> static bool CompareHash(byte[] hash, Stream stream, HashingAlgos algo = HashingAlgos.MD5) { var computedHash = ComputeHash(stream, algo); return(IsEqual(hash, computedHash)); }
/// <summary> /// Computes the hash of the given text /// </summary> /// <param name="text">Text whose hash is to be computed</param> /// <param name="algo">Hashing algo to use</param> /// <returns>The computed hash</returns> static byte[] ComputeHash(string text, HashingAlgos algo = HashingAlgos.MD5) { var bytes = Encoding.UTF8.GetBytes(text); return(ComputeHash(bytes, algo)); }