Esempio n. 1
0
        /// <summary>
        /// Get the hashcode from the value.
        /// </summary>
        /// <param name="value">The value to generate the hash code for.</param>
        /// <param name="hashcodeType">The hash name.</param>
        /// <param name="keySizePBKDF2">The PBKDF2 key size to generate.</param>
        /// <returns>The generated hash code.</returns>
        public static byte[] GetHashcodeRaw(string value, Nequeo.Cryptography.HashcodeType hashcodeType, int keySizePBKDF2 = 100)
        {
            // Generate the hash code
            HashAlgorithm alg = null;

            // Select the hash code type.
            switch (hashcodeType)
            {
            case HashcodeType.SHA3:
                // SHA3 hashcode.
                alg = new Sha3.SHA3Managed();
                break;

            case HashcodeType.PBKDF2:
                // PBKDF2 hashcode.
                byte[] hashValueEx = RandomDerivedKey.GenerateEx(value, keySizePBKDF2, 5000);
                return(hashValueEx);

            default:
                alg = HashAlgorithm.Create(hashcodeType.ToString());
                break;
            }

            byte[] byteValue = Encoding.ASCII.GetBytes(value);
            byte[] hashValue = alg.ComputeHash(byteValue);
            return(hashValue);
        }
Esempio n. 2
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 byte[] GetHashcodeFileRaw(string filename, Nequeo.Cryptography.HashcodeType hashcodeType)
        {
            FileStream file = null;

            byte[] hashValue = 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();

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