示例#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>
        /// <returns>The generated hash code.</returns>
        public static string GetHashcode(string value, Nequeo.Php.Cryptography.HashcodeType hashcodeType)
        {
            int i = 0;

            // Generate the hash code
            HashAlgorithm alg = HashAlgorithm.Create(hashcodeType.ToString());

            byte[] byteValue = Encoding.ASCII.GetBytes(value);
            byte[] hashValue = alg.ComputeHash(byteValue);

            // Get the string value of hashcode.
            string[] octetArrayByte = new string[hashValue.Count()];
            foreach (Byte item in hashValue)
            {
                octetArrayByte[i++] = item.ToString("X2");
            }

            // Create the octet string of bytes.
            string octetValue = String.Join("", octetArrayByte);

            return(octetValue);
        }
示例#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 string GetHashcodeFile(string filename, Nequeo.Php.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;
                    }

                    // 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();
                }
            }
        }