Dispose() protected final method

protected final Dispose ( bool disposing ) : void
disposing bool
return void
        /// <summary>
        /// SHA1Managed 算法的哈希值大小为 160 位
        /// 此类型的任何公共static成员都是线程安全的。但不保证所有实例成员都是线程安全的
        /// </summary>
        private static string Encrypt_static(string clearText, Encoding encode)
        {
            SHA1 sha = null;
            try
            {
                byte[] originalBytes = encode.GetBytes(clearText); //Encoding.UTF8.GetBytes(clearText);

                sha = new SHA1Managed();
                byte[] data = sha.ComputeHash(originalBytes);

                //return BitConverter.ToString(data); //将指定的字节数组的每个元素的数值转换为它的等效十六进制字符串表示形式
                StringBuilder builder = new StringBuilder();
                foreach (var item in data)
                {
                    builder.Append(item.ToString("X2"));//将该哈希作为 32 字符的十六进制格式字符串返回
                }

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

                sha = null;
            }
        }
Example #2
0
        public static string SHA1(string input)
        {
            System.Security.Cryptography.SHA1Managed sha1 = new System.Security.Cryptography.SHA1Managed();
            byte[] hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(input));
            sha1.Dispose();
            StringBuilder sb = new StringBuilder(hash.Length * 2);

            foreach (byte b in hash) // can be "x2" if you want lowercase
            {
                sb.Append(b.ToString("x2"));
            }
            return(sb.ToString());
        }