Example #1
0
        static void Main(string[] args)
        {
            //geração de hash com MD5
            StreamReader sr = new StreamReader("Arquivo.txt");

            String mensagem = sr.ReadToEnd();

            sr.Close();

            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

            byte[] bytes = Encoding.Default.GetBytes(mensagem);

            byte[] hash = md5.ComputeHash(bytes);

            Gravar(hash, md5.GetType().Name);

            Process.Start("notepad", String.Format("{0}.hash", md5.GetType().Name));
        }
Example #2
0
        /// <summary>
        /// Generate a checksum.
        /// </summary>
        /// <param name="fullPath">The full path of the file to generate a checksum for.</param>
        public void GenerateChecksum(string fullPath)
        {
            using (FileStream file = new FileStream(fullPath, FileMode.Open,FileAccess.Read,FileShare.Read))
            {
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] retVal = md5.ComputeHash(file);
                file.Close();

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < retVal.Length; i++)
                {
                    sb.Append(retVal[i].ToString("x2"));
                }

                Checksum = sb.ToString();
                ChecksumType = md5.GetType().ToString();
            }
        }
Example #3
0
        /// <summary>
        /// Verify a checksum.
        /// </summary>
        /// <param name="fullPath">The path to the file.</param>
        public void VerifyChecksum(string fullPath)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            if (md5.GetType().ToString().Equals(ChecksumType))
            {
                using (FileStream file = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {

                    byte[] retVal = md5.ComputeHash(file);
                    file.Close();

                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < retVal.Length; i++)
                    {
                        sb.Append(retVal[i].ToString("x2"));
                    }

                    if (!Checksum.Equals(sb.ToString()))
                        throw new ApplicationException("Checksum does not match for file " + fullPath);
                }
            }
            else
            {
                throw new ApplicationException("Unknown checksum type: " + ChecksumType);
            }
        }