Dispose() защищенный закрытый Метод

protected final Dispose ( bool disposing ) : void
disposing bool
Результат void
Пример #1
0
 public static string GetMd5String(byte[] buffer)
 {
     System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
     byte[] retVal = md5.ComputeHash(buffer);
     md5.Dispose();
     return(BitConverter.ToString(retVal).Replace("-", ""));
 }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public static byte[] GetMd5Bytes(byte[] buffer)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }

            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] retVal = md5.ComputeHash(buffer);
            md5.Dispose();
            return(retVal);
        }
Пример #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public static string GetMd5String(byte[] buffer)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }

            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] retVal = md5.ComputeHash(buffer);
            md5.Dispose();
            return(BitConverter.ToString(retVal).Replace("-", ""));
        }
Пример #4
0
 public static string GetHash(string text)
 {
     MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
     byte[] hash = md5.ComputeHash(Encoding.UTF8.GetBytes(text));
     StringBuilder Hash = new StringBuilder();
     foreach (byte b in hash)
     {
         Hash.AppendFormat("{0:X2}", b);
     }
     md5.Dispose();
     return Hash.ToString();
 }
Пример #5
0
        //-------------------------------------------------------

        /// <summary>
        /// バイト配列からMD5のハッシュコード文字列を取得する
        /// </summary>
        /// <param name="tData">ハッシュコードを取得する対象のバイト配列</param>
        /// <returns>ハッシュコード文字列</returns>
        public static string GetMD5Hash(byte[] data)
        {
            // MD5CryptoServiceProviderオブジェクトを作成
            System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();

            // ハッシュ値を計算する
            byte[] hash = md5.ComputeHash(data);

            // リソースを解放する
            md5.Clear();
            md5.Dispose();

            // byte型配列を16進数の文字列に変換
            System.Text.StringBuilder result = new System.Text.StringBuilder();
            foreach (byte code in hash)
            {
                result.Append(code.ToString("x2"));
            }

            return(result.ToString());
        }
        private string MD5Stream(System.IO.Stream stream)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider md5 = null;
            string resule = string.Empty;

            try
            {
                md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                byte[] hash = md5.ComputeHash(stream);
                resule = System.BitConverter.ToString(hash);
                resule = resule.Replace("-", "");
            }
            finally
            {
                if (md5 != null)
                {
                    md5.Dispose();
                }
            }
            return(resule);
        }
Пример #7
0
        /// <summary>
        /// MD5CryptoServiceProvider类的哈希大小为 128 位
        /// 此类型的任何公共static成员都是线程安全的。但不保证所有实例成员都是线程安全的
        /// </summary>
        private static string Encrypt_static(string clearText, Encoding encode)
        {
            MD5 md5 = null;
            try
            {
                byte[] originalBytes = encode.GetBytes(clearText); //Encoding.UTF8.GetBytes(clearText);

                md5 = new MD5CryptoServiceProvider();
                byte[] data = md5.ComputeHash(originalBytes);

                StringBuilder builder = new StringBuilder();
                foreach (var item in data)
                {
                    builder.Append(item.ToString("X2"));
                }

                return builder.ToString();
            }
            catch (ArgumentNullException) { return clearText; }
            catch (EncoderFallbackException) { return clearText; }
            //catch (TargetInvocationException) { return clearText; }
            catch (ObjectDisposedException) { return clearText; }
            catch (InvalidOperationException) { return clearText; }
            catch { return clearText; }
            finally
            {
                if (md5 != null)
                {
                    md5.Clear();
                    md5.Dispose();
                }

                md5 = null;
            }

            //return "";
        }
Пример #8
0
        /// <summary>
        /// Generates a hash for the given plain text value and returns a
        /// base64-encoded result. Before the hash is computed, a random salt
        /// is generated and appended to the plain text. This salt is stored at
        /// the end of the hash value, so it can be used later for hash
        /// verification.
        /// </summary>
        /// <param name="byteData">
        /// Plaintext value to be hashed. 
        /// </param>
        /// <param name="hashAlgorithm">
        /// Name of the hash algorithm. Allowed values are: "MD5", "SHA1",
        /// "SHA256", "SHA384", "SHA512", "HMACMD5", "HMACSHA1", "HMACSHA256",
        ///  "HMACSHA512" (if any other value is specified  MD5 will be used). 
        /// 
        /// HMAC algorithms uses Hash-based Message Authentication Code.
        /// The HMAC process mixes a secret key with the message data, hashes 
        /// the result with the hash function, mixes that hash value with 
        /// the secret key again, and then applies the hash function
        /// a second time. HMAC hashes are fixed lenght and generally
        /// much longer than non-HMAC hashes of the same type.
        /// 
        /// https://msdn.microsoft.com/en-us/library/system.security.cryptography.hmacsha256(v=vs.110).aspx      
        /// 
        /// This value is case-insensitive.
        /// </param>
        /// <param name="saltBytes">
        /// Optional but recommended salt bytes to apply to the hash. If not passed the
        /// raw encoding is used. If salt is nullthe raw algorithm is used (useful for 
        /// file hashes etc.) HMAC versions REQUIRE that salt is passed.
        /// </param>
        /// <param name="useBinHex">if true returns the data as BinHex byte pair string. Otherwise Base64 is returned.</param>
        /// <returns>
        /// Hash value formatted as a base64-encoded or BinHex stringstring.
        /// </returns>
        public static string ComputeHash(byte[] byteData,
            string hashAlgorithm,
            byte[] saltBytes,
            bool useBinHex = false)
        {
            if (byteData == null)
                return null;

            // Convert plain text into a byte array.
            byte[] plainTextWithSaltBytes;

            if (saltBytes != null)
            {
                // Allocate array, which will hold plain text and salt.
                plainTextWithSaltBytes =
                    new byte[byteData.Length + saltBytes.Length];

                // Copy plain text bytes into resulting array.
                for (int i = 0; i < byteData.Length; i++)
                    plainTextWithSaltBytes[i] = byteData[i];

                // Append salt bytes to the resulting array.
                for (int i = 0; i < saltBytes.Length; i++)
                    plainTextWithSaltBytes[byteData.Length + i] = saltBytes[i];
            }
            else
                plainTextWithSaltBytes = byteData;

            HashAlgorithm hash;

            // Make sure hashing algorithm name is specified.
            if (hashAlgorithm == null)
                hashAlgorithm = "";

            // Initialize appropriate hashing algorithm class.
            switch (hashAlgorithm.ToUpper())
            {
                case "SHA1":
                    hash = new SHA1Managed();
                    break;
                case "SHA256":
                    hash = new SHA256Managed();
                    break;
                case "SHA384":
                    hash = new SHA384Managed();
                    break;
                case "SHA512":
                    hash = new SHA512Managed();
                    break;
                case "HMACMD5":
                    hash = new HMACMD5(saltBytes);
                    break;
                case "HMACSHA1":
                    hash = new HMACSHA1(saltBytes);
                    break;
                case "HMACSHA256":
                    hash = new HMACSHA256(saltBytes);
                    break;
                case "HMACSHA512":
                    hash = new HMACSHA512(saltBytes);
                    break;
                default:
                    // default to MD5
                    hash = new MD5CryptoServiceProvider();
                    break;
            }

            byte[] hashBytes = hash.ComputeHash(plainTextWithSaltBytes);

            hash.Dispose();

            if (useBinHex)
                return BinaryToBinHex(hashBytes);

            return Convert.ToBase64String(hashBytes);
        }
Пример #9
0
 /// <summary>
 ///     根据文件路径得到文件的MD5值
 /// </summary>
 /// <param name="filePath">文件的路径</param>
 /// <returns>MD5值</returns>
 public static string GetMD5(string filePath)
 {
     try
     {
         var get_file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
         var md5 = new MD5CryptoServiceProvider();
         var hash_byte = md5.ComputeHash(get_file);
         var resule = System.BitConverter.ToString(hash_byte);
         resule = resule.Replace("-", "");
         md5.Clear();
         md5.Dispose();
         get_file.Close();
         get_file.Dispose();
         return resule;
     }
     catch (Exception e) {
         return e.ToString();
     }
 }
Пример #10
0
 public static string MD5Stream(System.IO.Stream stream)
 {
     System.Security.Cryptography.MD5CryptoServiceProvider md5 = null;
     string resule = string.Empty;
     if (stream != null)
     {
         try
         {
             md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
             byte[] hash = md5.ComputeHash(stream);
             resule = System.BitConverter.ToString(hash);
             resule = resule.Replace("-", "");
         }
         finally
         {
             if (md5 != null)
             {
                 md5.Dispose();
             }
         }
     }
     stream.Close();
     return resule;
 }
Пример #11
0
        public string MD5File(string fileName)
        {
            if(fileName.IsNull())
                throw new ArgumentNullException("fileName");

            byte[] retVal;

            using(var file = new FileStream(fileName, FileMode.Open))
            {
                var md5 = new MD5CryptoServiceProvider();
                retVal = md5.ComputeHash(file);
            #if false
                md5.Dispose();
            #endif
            }

            var sb = new StringBuilder();

            if(!retVal.IsNull())
            {
                for(var i = 0; i < retVal.Length; i++)
                    sb.Append(retVal[i].ToString("x2"));
            }

            return sb.ToString();
        }
Пример #12
0
        public string Md5(string value)
        {
            if(value.IsNull())
                throw new ArgumentNullException("value");

            var x = new MD5CryptoServiceProvider();
            var data = Encoding.UTF8.GetBytes(value);
            data = x.ComputeHash(data);
            #if false
            x.Dispose();
            #endif
            var ret = string.Empty;

            for(var i = 0; i < data.Length; i++)
                ret += data[i].ToString("x2").ToLower();

            return ret;
        }
Пример #13
0
        /// <summary>
        /// Computes the hash for the specified stream and compares
        /// it to the value in this object. CRC hashes are not supported 
        /// because there is no built-in support in the .net framework and
        /// a CRC implementation exceeds the scope of this project. If you
        /// attempt to Verify() a CRC hash a NotImplemented() exception will
        /// be thrown.
        /// </summary>
        /// <param name="istream">The stream to compute the hash for</param>
        /// <returns>True if the computed hash matches what's stored in this object.</returns>
        public bool Verify(Stream istream) {
            if (IsValid) {
                HashAlgorithm hashAlg = null;

                switch (m_algorithm) {
                    case FtpHashAlgorithm.SHA1:
                        hashAlg = new SHA1CryptoServiceProvider();
                        break;
#if !NET2
                    case FtpHashAlgorithm.SHA256:
                        hashAlg = new SHA256CryptoServiceProvider();
                        break;
                    case FtpHashAlgorithm.SHA512:
                        hashAlg = new SHA512CryptoServiceProvider();
                        break;
#endif
                    case FtpHashAlgorithm.MD5:
                        hashAlg = new MD5CryptoServiceProvider();
                        break;
                    case FtpHashAlgorithm.CRC:
                        throw new NotImplementedException("There is no built in support for computing CRC hashes.");
                    default:
                        throw new NotImplementedException("Unknown hash algorithm: " + m_algorithm.ToString());
                }

                try {
                    byte[] data = null;
                    string hash = "";

                    data = hashAlg.ComputeHash(istream);
                    if (data != null) {
                        foreach (byte b in data) {
                            hash += b.ToString("x2");
                        }

                        return (hash.ToUpper() == m_value.ToUpper());
                    }
                }
                finally {
#if !NET2 // .NET 2.0 doesn't provide access to Dispose() for HashAlgorithm
                    if (hashAlg != null)
                        hashAlg.Dispose();
#endif
                }
            }

            return false;
        }
Пример #14
0
        /// <summary>
        /// Returns the MD5 hash string of the input string.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <returns>The MD5 hash string of the input string.</returns>
        public static string ToMD5Hash(this string input)
        {
            var cryptoServiceProvider = new MD5CryptoServiceProvider();

            var newdata = Encoding.Default.GetBytes(input);

            var encrypted = cryptoServiceProvider.ComputeHash(newdata);

            cryptoServiceProvider.Dispose();

            return BitConverter.ToString(encrypted).Replace("-", string.Empty).ToLower();
        }