Beispiel #1
0
        /// <summary>
        /// Get the hashcode from the file.
        /// </summary>
        /// <param name="filename">The path and file name to generate the hash code for.</param>
        /// <param name="hashcodeType">The hash name.</param>
        /// <returns>The generated hash code.</returns>
        public static string GetHashcodeFile(string filename, Nequeo.Cryptography.HashcodeType hashcodeType)
        {
            FileStream file = null;

            byte[]        hashValue = null;
            StringBuilder sb        = null;

            try
            {
                // Open the file to generate the hash code for.
                using (file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    // Select the hash code type.
                    switch (hashcodeType)
                    {
                    case HashcodeType.MD5:
                        // MD5 hashcode.
                        MD5 md5 = new MD5CryptoServiceProvider();
                        hashValue = md5.ComputeHash(file);
                        break;

                    case HashcodeType.SHA1:
                        // SHA1 hashcode.
                        SHA1 sha1 = new SHA1CryptoServiceProvider();
                        hashValue = sha1.ComputeHash(file);
                        break;

                    case HashcodeType.SHA256:
                        // SHA256 hashcode.
                        SHA256 sha256 = new SHA256CryptoServiceProvider();
                        hashValue = sha256.ComputeHash(file);
                        break;

                    case HashcodeType.SHA384:
                        // SHA384 hashcode.
                        SHA384 sha384 = new SHA384CryptoServiceProvider();
                        hashValue = sha384.ComputeHash(file);
                        break;

                    case HashcodeType.SHA512:
                        // SHA512 hashcode.
                        SHA512 sha512 = new SHA512CryptoServiceProvider();
                        hashValue = sha512.ComputeHash(file);
                        break;

                    case HashcodeType.SHA3:
                        // SHA3 hashcode.
                        Sha3.SHA3Managed sha3 = new Sha3.SHA3Managed();
                        hashValue = sha3.ComputeHash(file);
                        break;

                    case HashcodeType.PBKDF2:
                        throw new Exception("PBKDF2 Hashing is not supported.");
                    }

                    // Close the file.
                    file.Close();

                    // Get the hashcode bytes as hex-string.
                    sb = new StringBuilder();
                    for (int i = 0; i < hashValue.Length; i++)
                    {
                        sb.Append(hashValue[i].ToString("X2"));
                    }

                    // Return the hash code.
                    return(sb.ToString());
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                // Clean-up
                if (file != null)
                {
                    file.Close();
                }
            }
        }