/// <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 GetSHA1(string source)
		{
			var sha1 = new SHA1Managed();
			var bytes = sha1.ComputeHash(Encoding.UTF8.GetBytes(source));
			var result = BitConverter.ToString(bytes).Replace("-", string.Empty).ToLower();
			sha1.Clear();
			return result;
		}
Example #3
0
        /// <summary>
        /// Encrypt a string depend on sha256 algorithm
        /// </summary>
        /// <param name="input"></param>
        /// <returns>Encrypted string.</returns>
        public static byte[] SHA1Hash(string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return null;
            }

            SHA1Managed hashAlgorithm = new SHA1Managed();
            try
            {
                return hashAlgorithm.ComputeHash(Encoding.Default.GetBytes(input));
            }
            finally
            {
                hashAlgorithm.Clear();
            }
        }
        /// <summary>
        /// Generates a base64 encoded SHA1 hash from the input string.
        /// </summary>
        /// <param name="input">The input string to be hashed.</param>
        public static string GenerateSHA1(string input)
        {
            // extract password as unmanaged byte array
            SHA1Managed sha = new SHA1Managed();
            try
            {
                // generate hash using SHA1 provider
                byte[] passwordData = Encoding.UTF8.GetBytes(input);
                byte[] hashedPasswordData = sha.ComputeHash(passwordData);
                string hashedPassword = Convert.ToBase64String(
                    hashedPasswordData);

                // return hash
                return hashedPassword;
            }

            // ensure the hash is cleared from memory
            finally
            {
                sha.Clear();
            }
        }
Example #5
0
        static byte[] SHAHash(byte[] input)
        {
            SHA1Managed sha = new SHA1Managed();

            byte[] output = sha.ComputeHash( input );

            sha.Clear();

            return output;
        }
Example #6
0
        public string SHA1Encrypt(string strIN)
        {
            byte[] tmpByte;
            SHA1 sha1 = new SHA1Managed();

            tmpByte = sha1.ComputeHash(System.Text.Encoding.ASCII.GetBytes(strIN));
            string strResult = BitConverter.ToString(tmpByte);
            strResult = strResult.Replace("-", "");
            sha1.Clear();
            sha1 = null;
            return strResult;
        }
Example #7
0
        /// <summary>
        /// Updates the Content Info in the Tmd.
        /// Tmd and Contents must be in the same Directory
        /// </summary>
        /// <param name="tmdfile"></param>
        public static void UpdateTmdContents(string tmdfile)
        {
            FileStream tmd = new FileStream(tmdfile, FileMode.Open, FileAccess.ReadWrite);

            tmd.Seek(0x1de, SeekOrigin.Begin);
            int contentcount = Tools.HexStringToInt(tmd.ReadByte().ToString("x2") + tmd.ReadByte().ToString("x2"));

            for (int i = 0; i < contentcount; i++)
            {
                int oldsize = 0;
                int contentpos = 0x1e4 + (36 * i);

                tmd.Seek(contentpos, SeekOrigin.Begin);
                string id = tmd.ReadByte().ToString("x2") + tmd.ReadByte().ToString("x2") + tmd.ReadByte().ToString("x2") + tmd.ReadByte().ToString("x2");
                string index = "0000" + tmd.ReadByte().ToString("x2") + tmd.ReadByte().ToString("x2");
                string type = tmd.ReadByte().ToString("x2") + tmd.ReadByte().ToString("x2");

                if (type != "0001") continue;

                try
                {
                    oldsize = Tools.HexStringToInt(tmd.ReadByte().ToString("x2") +
                        tmd.ReadByte().ToString("x2") +
                        tmd.ReadByte().ToString("x2") +
                        tmd.ReadByte().ToString("x2") +
                        tmd.ReadByte().ToString("x2") +
                        tmd.ReadByte().ToString("x2") +
                        tmd.ReadByte().ToString("x2") +
                        tmd.ReadByte().ToString("x2"));
                }
                catch { }

                byte[] oldsha1 = new byte[20];
                tmd.Read(oldsha1, 0, oldsha1.Length);
                string fileName = id;

                if (!File.Exists(tmdfile.Remove(tmdfile.LastIndexOf('/') + 1) + fileName + ".app"))
                    fileName = index;

                if (File.Exists(tmdfile.Remove(tmdfile.LastIndexOf('/') + 1) + fileName + ".app"))
                {
                    byte[] content = Wii.Tools.LoadFileToByteArray(tmdfile.Remove(tmdfile.LastIndexOf('/') + 1) + fileName + ".app");
                    int newsize = content.Length;

                    if (newsize != oldsize)
                    {
                        byte[] changedsize = Tools.FileLengthToByteArray(newsize);

                        tmd.Seek(contentpos + 8, SeekOrigin.Begin);
                        for (int x = 8; x > changedsize.Length; x--) tmd.WriteByte(0x00);
                        tmd.Write(changedsize, 0, changedsize.Length);
                    }

                    SHA1Managed sha1 = new SHA1Managed();
                    byte[] newsha1 = sha1.ComputeHash(content);
                    sha1.Clear();

                    if (Tools.CompareByteArrays(newsha1, oldsha1) == false)
                    {
                        tmd.Seek(contentpos + 16, SeekOrigin.Begin);
                        tmd.Write(newsha1, 0, newsha1.Length);
                    }
                }
                else
                {
                    throw new Exception("At least one content file wasn't found!");
                }
            }

            tmd.Close();
        }