Esempio n. 1
0
        private String CalculateFromText(HashType ht, String str)
        {
            byte[]        hashBytes   = null;
            StringBuilder hashBuilder = new StringBuilder();

            switch (ht)
            {
            case HashType.MD5:
                //Compute hash based on source data.
                hashBytes = new MD5CryptoServiceProvider().ComputeHash(UTF8Encoding.UTF8.GetBytes(str));
                break;

            case HashType.MD4:
                //Compute hash based on source data.
                hashBytes = new Md4().ComputeHash(UTF8Encoding.UTF8.GetBytes(str));
                break;

            case HashType.ED2K:
                //Compute hash based on source data.
                hashBytes = new Ed2k().ComputeHash(UTF8Encoding.UTF8.GetBytes(str));
                break;

            case HashType.CRC32:
                hashBytes = new Crc32().ComputeHash(UTF8Encoding.UTF8.GetBytes(str));
                break;

            case HashType.SHA1:
                //Compute hash based on source data.
                hashBytes = new SHA1CryptoServiceProvider().ComputeHash(UTF8Encoding.UTF8.GetBytes(str));
                break;

            case HashType.SHA256:
                //Compute hash based on source data.
                hashBytes = SHA256.Create().ComputeHash(UTF8Encoding.UTF8.GetBytes(str));
                break;

            case HashType.SHA384:
                //Compute hash based on source data.
                hashBytes = SHA384.Create().ComputeHash(UTF8Encoding.UTF8.GetBytes(str));
                break;

            case HashType.SHA512:
                //Compute hash based on source data.
                hashBytes = SHA512.Create().ComputeHash(UTF8Encoding.UTF8.GetBytes(str));
                break;

            case HashType.TTH:
                TTH tth = new TTH();
                tth.Initialize();
                hashBytes = tth.ComputeHash(UTF8Encoding.UTF8.GetBytes(str));
                break;
            }

            foreach (Byte hashByte in hashBytes)
            {
                hashBuilder.Append(hashByte.ToString("X2").ToLower());
            }
            return(hashBuilder.ToString());
        }
Esempio n. 2
0
        private String CalculateFromFile(HashType ht, String filename)
        {
            byte[] hashBytes = null;
            using (FileStream file = new FileStream(filename, FileMode.Open))
            {
                switch (ht)
                {
                case HashType.MD5:
                    hashBytes = new MD5CryptoServiceProvider().ComputeHash(file);
                    break;

                case HashType.MD4:
                    hashBytes = new Md4().ComputeHash(file);
                    break;

                case HashType.ED2K:
                    hashBytes = new Ed2k().ComputeHash(file);
                    break;

                case HashType.CRC32:
                    hashBytes = new Crc32().ComputeHash(file);
                    break;

                case HashType.SHA1:
                    hashBytes = new SHA1CryptoServiceProvider().ComputeHash(file);
                    break;

                case HashType.SHA256:
                    hashBytes = SHA256.Create().ComputeHash(file);
                    break;

                case HashType.SHA384:
                    hashBytes = SHA384.Create().ComputeHash(file);
                    break;

                case HashType.SHA512:
                    hashBytes = SHA512.Create().ComputeHash(file);
                    break;

                case HashType.TTH:
                    TTH tth = new TTH();
                    tth.Initialize();
                    hashBytes = tth.ComputeHash(file);
                    break;
                }
            }
            StringBuilder hashBuilder = new StringBuilder();

            foreach (byte hashByte in hashBytes)
            {
                hashBuilder.Append(hashByte.ToString("x2").ToLower());
            }
            return(hashBuilder.ToString());
        }