Dispose() protected final method

protected final Dispose ( bool disposing ) : void
disposing bool
return void
        /// <summary>
        /// SHA1CryptoServiceProvider 类的哈希大小为 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 SHA1CryptoServiceProvider();
                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 (Exception) { return clearText; }
            finally
            {
                if (sha != null)
                {
                    sha.Clear();
                    sha.Dispose();
                }

                sha = null;
            }
        }
 /// <summary>
 /// This will return the SHA-1 has of a string.
 /// </summary>
 /// <param name="myString"></param>
 /// <returns></returns>
 public static byte[] GetSha1(this string myString)
 {
     var data = myString.ToBytes();
     SHA1 sha = new SHA1CryptoServiceProvider();
     var result = sha.ComputeHash(data);
     sha.Dispose();
     return result;
 }
Beispiel #3
0
 public void SetKey(byte[] key)
 {
     Key = key;
     SHA1 digest = new SHA1CryptoServiceProvider();
     key = digest.ComputeHash(key);
     digest.Dispose();
     Log.Info("[ENCRYPTION-KEY]", DumpData(key));
     encryption = new ARC4(key);
     decryption = new ARC4(key);
 }
Beispiel #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public static byte[] GetSha1Bytes(byte[] buffer)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }

            System.Security.Cryptography.SHA1CryptoServiceProvider osha1 = new System.Security.Cryptography.SHA1CryptoServiceProvider();
            byte[] retVal = osha1.ComputeHash(buffer);
            osha1.Dispose();
            return(retVal);
        }
Beispiel #5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public static string GetSha1String(byte[] buffer)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }

            System.Security.Cryptography.SHA1CryptoServiceProvider osha1 = new System.Security.Cryptography.SHA1CryptoServiceProvider();
            byte[] retVal = osha1.ComputeHash(buffer);
            osha1.Dispose();
            return(BitConverter.ToString(retVal).Replace("-", ""));
        }
Beispiel #6
0
 public Encryption(byte[] key = null)
 {
     if (key == null)
     {
         key = new byte[8];
         Random generator = new Random();
         generator.NextBytes(key);
         generator = null;
     }
     Key = key;
     SHA1 digest = new SHA1CryptoServiceProvider();
     key = digest.ComputeHash(key);
     digest.Dispose();
     Log.Info("[ENCRYPTION-KEY]", DumpData(key));
     encryption = new ARC4(key);
     decryption = new ARC4(key);
 }
Beispiel #7
0
 public Encryption(byte[] key = null)
 {
     if (key == null)
     {
         key = new byte[8];
         Random generator = new Random();
         generator.NextBytes(key);
         generator = null;
     }
     Key = key;
     SHA1 digest = new SHA1CryptoServiceProvider();
     key = digest.ComputeHash(key);
     digest.Dispose();
     Console.Write("New key:");
     for (int i = 0; i < 20; i++)
     {
         Console.Write(" " + key[i]);
     }
     Console.WriteLine();
     encryption = new ARC4(key);
     decryption = new ARC4(key);
 }
Beispiel #8
0
        public void Login(String email, String password)
        {
            Encoding enc = Encoding.ASCII;
            byte[] buffer = new byte[0];
            String pass = "";
            try
            {
                buffer = enc.GetBytes(password);
            }
            catch (Exception e)
            {

            }
            SHA1CryptoServiceProvider cryptoTransformSHA1 = new SHA1CryptoServiceProvider();

            try
            {
                pass = BitConverter.ToString(cryptoTransformSHA1.ComputeHash(buffer)).Replace("-", "");
            }
            catch (Exception e)
            {

            }
            cryptoTransformSHA1.Dispose();
            pass = pass.ToLower();
            SocketMessage sm = new SocketMessage("LOG");
            sm.Arguments.Add(email);
            sm.Arguments.Add(pass);
            this.writeMessage(sm);
        }
Beispiel #9
0
 /// <summary>
 /// SHA1 加密,返回大写字符串
 /// </summary>
 /// <param name="content">需要加密字符串</param>
 /// <param name="encode">指定加密编码</param>
 /// <returns>返回40位大写字符串</returns>
 public static string SHA1(string content, Encoding encode)
 {
     try
     {
         SHA1 sha1 = new SHA1CryptoServiceProvider();
         byte[] bytes_in = encode.GetBytes(content);
         byte[] bytes_out = sha1.ComputeHash(bytes_in);
         sha1.Dispose();
         string result = BitConverter.ToString(bytes_out);
         result = result.Replace("-", "");
         return result;
     }
     catch (Exception ex)
     {
         throw new Exception("SHA1加密出错:" + ex.Message);
     }
 }
Beispiel #10
0
        public string Sha1(string value)
        {
            if(value.IsNull())
                throw new ArgumentNullException("value");

            var x = new SHA1CryptoServiceProvider();
            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;
        }
        /// <summary>
        /// Get signature for the chunk.
        /// </summary>
        /// <param name="array">The data of the chunk.</param>
        /// <returns>The signature instance.</returns>
        private SignatureObject GetSignature(byte[] array)
        {
            if (this.FileContent.Length <= 250 * 1024 * 1024)
            {
                SHA1 sha = new SHA1CryptoServiceProvider();
                byte[] temp = sha.ComputeHash(array);
                sha.Dispose();

                SignatureObject signature = new SignatureObject();
                signature.SignatureData = new BinaryItem(temp);
                return signature;
            }
            else
            {
                throw new NotImplementedException("When the file size is larger than 250MB, the signature method is not implemented.");
            }
        }
        /// <summary>
        /// Get signature with SHA1 algorithm.
        /// </summary>
        /// <param name="array">The input data.</param>
        /// <returns>An instance of SignatureObject.</returns>
        private SignatureObject GetSHA1Signature(byte[] array)
        {
            SHA1 sha = new SHA1CryptoServiceProvider();
            byte[] temp = sha.ComputeHash(array);
            sha.Dispose();

            SignatureObject signature = new SignatureObject();
            signature.SignatureData = new BinaryItem(temp);
            return signature;
        }
        /// <summary>
        /// Get the signature for single chunk.
        /// </summary>
        /// <param name="header">The data of file header.</param>
        /// <param name="dataFile">The data of data file.</param>
        /// <returns>An instance of SignatureObject.</returns>
        private SignatureObject GetSingleChunkSignature(byte[] header, byte[] dataFile)
        {
            SHA1 sha = new SHA1CryptoServiceProvider();
            byte[] headerSignature = sha.ComputeHash(header);
            sha.Dispose();
            byte[] singleSignature = null;

            if (SharedContext.Current.CellStorageVersionType.MinorVersion >= 2)
            {
                singleSignature = new byte[dataFile.Length];

                for (int i = 0; i < headerSignature.Length; i++)
                {
                    singleSignature[i] = (byte)(headerSignature[i] ^ dataFile[i]);
                }
            }
            else
            {
                List<byte> tmp = new List<byte>();
                tmp.AddRange(headerSignature);
                tmp.AddRange(dataFile);

                singleSignature = tmp.ToArray(); 
            }

            SignatureObject signature = new SignatureObject();
            signature.SignatureData = new BinaryItem(singleSignature);

            return signature;
        }
 /// <summary>
 /// Obtain a hash value for a given string.
 /// </summary>
 /// <param name="value"></param>
 /// <returns></returns>
 public static string GetHash(string value)
 {
     byte[] clearBytes = Encoding.UTF8.GetBytes(value);
     SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
     sha1.ComputeHash(clearBytes);
     byte[] hashedBytes = sha1.Hash;
     sha1.Dispose();
     return BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();
 }
Beispiel #15
0
        public static string GenerateFileHash(Stream Source)
        {
            SHA1 shar = new SHA1CryptoServiceProvider();

            byte[] result = shar.ComputeHash(Source);
            shar.Dispose();

            StringBuilder output = new StringBuilder();

            foreach (byte x in result)
            {
                output.Append(string.Format("{0:x2}", x));
            }

            return output.ToString();
        }