/// <summary> /// Performs a measurement for the number of hashes pr second for the given algorithm and implementation /// </summary> /// <returns>The number of hashes pr second.</returns> /// <param name="algorithm">The algorithm to test with.</param> /// <param name="implementation">The implementation to test.</param> /// <param name="blocksize">The size of the blocks being hashed.</param> /// <param name="hashesprround">The number of hashes between each time check.</param> /// <param name="measureseconds">The number of seconds to measure.</param> /// <param name="bufferoffset">The number of bytes to offset the buffer for measuring non-aligned performance</param> public static long TestHashesPrSecond(string algorithm = "SHA256", HashImplementation implementation = HashImplementation.Any, int blocksize = 102400, int hashesprround = 1000, float measureseconds = 2f, int bufferoffset = 0) { using (var alg = Create(algorithm, false, implementation)) { if (alg == null) { return(0); } var st = DateTime.Now; var target = st.Ticks + TimeSpan.FromSeconds(measureseconds).Ticks; var buffer = new byte[blocksize + bufferoffset]; var performed = 0L; alg.Initialize(); while (DateTime.Now.Ticks < target) { for (var i = 0; i < hashesprround; i++) { alg.TransformBlock(buffer, bufferoffset, blocksize, buffer, bufferoffset); } performed++; } alg.TransformFinalBlock(buffer, 0, 0); var elapsed = DateTime.Now - st; return((long)((performed * hashesprround * (blocksize / 64)) / elapsed.TotalSeconds)); } }
/// <summary> /// Returns a value indicating if the specific implementation is supported on this system /// </summary> /// <returns><c>true</c>, if implementation was supportsed, <c>false</c> otherwise.</returns> /// <param name="implementation">The implementation to test for.</param> public static bool SupportsImplementation(HashImplementation implementation) { switch (implementation) { case HashImplementation.OpenSSL10: return(OpenSSL10Version != null); case HashImplementation.OpenSSL11: return(OpenSSL11Version != null); case HashImplementation.AppleCommonCrypto: return(AppleCommonCryptoHashAlgorithm.IsSupported); case HashImplementation.CNG: case HashImplementation.Managed: case HashImplementation.Any: return(true); } return(false); }
public Hasher(HashImplementation hashImplementation) { _hashImplementation = hashImplementation; }
/// <summary> /// Create the specified hashing algorithm. /// </summary> /// <returns>The created algorithm.</returns> /// <param name="algorithm">The name of the hash algorithm to create.</param> /// <param name="allowfallback">If set to <c>true</c>, the `<seealso cref="System.Security.Cryptography.HashAlgorithm.Create()"/> method is called if not implementation could be loaded</param> /// <param name="implementation">The hash implementation toy use</param> public static HashAlgorithm Create(string algorithm, bool allowfallback = true, HashImplementation implementation = HashImplementation.Any) { HashAlgorithm result = null; // If we are not asked for a particular version, pick the best we found if (implementation == HashImplementation.Any) { implementation = PreferedImplementation; } switch (implementation) { case HashImplementation.OpenSSL10: result = OpenSSL10HashAlgorithm.Create(algorithm); break; case HashImplementation.OpenSSL11: result = OpenSSL11HashAlgorithm.Create(algorithm); break; case HashImplementation.CNG: result = CNGHashAlgorithm.Create(algorithm, false); break; case HashImplementation.AppleCommonCrypto: result = AppleCommonCryptoHashAlgorithm.Create(algorithm); break; case HashImplementation.Managed: result = HashAlgorithm.Create(algorithm); break; } if (allowfallback) { result = result ?? HashAlgorithm.Create(algorithm); } return(result); }