Exemplo n.º 1
0
        void SetupControls(HashingEventArgs e)
        {
            // I'm not entirely sure why I'm creating controls by hand.
            // Though it likely has something to do with some initial error regarding cross-thread access.
            // At this point though, it's probably safe to do all this in the designer, but I'm leaving it this way because it works.

            this.Visible = true;
            String md5_hash = e.ChecksumValue;

            // Measuring the size of strings and such.
            Graphics g              = CreateGraphics();
            Font     font_checksum  = new Font("Segoe UI Mono", 24, FontStyle.Regular);
            Font     font_button    = new Font("Segoe UI", 12, FontStyle.Regular);
            SizeF    size_hash      = g.MeasureString(md5_hash, font_checksum);
            SizeF    size_clipboard = g.MeasureString(bClipboard, font_button);
            SizeF    size_exit      = g.MeasureString(bExit, font_button);

            // Textbox Properties
            TextBox txtChecksum = new TextBox
            {
                Name      = "txtChecksum",
                Margin    = new Padding(0),
                Location  = new Point(0, 0),
                TextAlign = HorizontalAlignment.Center,
                Font      = font_checksum,
                AutoSize  = false,
                Height    = (int)Math.Ceiling(size_hash.Height),
                Width     = (int)Math.Ceiling(size_hash.Width),
                Text      = md5_hash
            };

            txtChecksum.Select(txtChecksum.Text.Length, 0); // Deselect the text in the box.

            // Button (Clipboard) Properties
            Button btnClipboard = new Button
            {
                Margin    = new Padding(0),
                Location  = new Point(0, txtChecksum.Location.Y + txtChecksum.Height),
                TextAlign = ContentAlignment.MiddleCenter,
                Font      = font_button,
                AutoSize  = true,
                Height    = (int)Math.Ceiling(size_clipboard.Height),
                Width     = txtChecksum.Width / 3,
                Text      = bClipboard
            };

            btnClipboard.Click += new EventHandler(btnClipboard_Click);

            // Button (Compare) Properties
            Button btnCompare = new Button
            {
                Margin    = new Padding(0),
                Location  = new Point(btnClipboard.Right, txtChecksum.Location.Y + txtChecksum.Height),
                TextAlign = ContentAlignment.MiddleCenter,
                Font      = font_button,
                AutoSize  = true,
                Height    = (int)Math.Ceiling(size_exit.Height),
                Width     = txtChecksum.Width / 3,
                Text      = bCompare
            };

            btnCompare.Click += new EventHandler(btnCompare_Click);

            // Textbox (Compare) Properties
            TextBox txtCompare = new TextBox
            {
                Name      = "txtCompare",
                Margin    = new Padding(0),
                Location  = btnCompare.Location,
                AutoSize  = false,
                TextAlign = HorizontalAlignment.Center,
                Font      = btnCompare.Font,
                Height    = btnCompare.Height,
                Width     = btnCompare.Width,
                Text      = tCompare,
                Visible   = false
            };

            txtCompare.GotFocus    += new EventHandler(txtCompare_GotFocus);
            txtCompare.LostFocus   += new EventHandler(txtCompare_LostFocus);
            txtCompare.TextChanged += new EventHandler(txtCompare_TextChanged);
            txtCompare.Select(txtCompare.Text.Length, 0); // Deselect the text in the box.

            // Button (Exit) Properties
            Button btnExit = new Button
            {
                Margin    = new Padding(0),
                Location  = new Point(btnCompare.Right, txtChecksum.Location.Y + txtChecksum.Height),
                TextAlign = ContentAlignment.MiddleCenter,
                Font      = font_button,
                AutoSize  = true,
                Height    = (int)Math.Ceiling(size_exit.Height),
                Width     = txtChecksum.Width / 3,
                Text      = bExit
            };

            btnExit.Click += new EventHandler(btnExit_Click);

            // Form Properties
            this.Text         = "MD5 Checksum";
            this.Width        = 1;
            this.Height       = 1;
            this.AutoSizeMode = AutoSizeMode.GrowOnly;
            this.AutoSize     = true;
            this.Controls.Add(txtChecksum);
            this.Controls.Add(btnClipboard);
            this.Controls.Add(btnCompare);
            btnCompare.Parent.Controls.Add(txtCompare);
            this.Controls.Add(btnExit);

            this.Left = (Screen.PrimaryScreen.WorkingArea.Width - this.Width) / 2;
            this.Top  = (Screen.PrimaryScreen.WorkingArea.Height - this.Height) / 2;
        }
Exemplo n.º 2
0
 private void splash_OnHashingFinished(object sender, HashingEventArgs e)
 {
     splash.Invoke(new CloseDelegate(splash.Close));
     this.Invoke(new ShowDelegate(this.Show));
     this.Invoke(new SetupControlsDelegate(this.SetupControls), e);
 }
Exemplo n.º 3
0
        public void GetMD5(Object strFilePath)
        {
            String     FilePath  = strFilePath.ToString();
            MD5        MD5Hasher = MD5.Create();
            long       offset    = 0;
            FileStream fs        = null;

            try
            {
                fs = new FileStream(FilePath, FileMode.Open);
            }
            catch (Exception)
            {
                // If an exception is thrown, it's likely that the file is in use.
                MessageBox.Show("File is currently in use by another application.");
                Application.Exit();
            }

            if (fs == null)
            {
                // I can't think of a single reason why it would still be null...
                // But just in case, let's handle it.
                // NOTE: I realize this is a very poor way of "handling" exceptions.
                Application.Exit();
            }

            long   filesize   = fs.Length;
            double onePercent = filesize / 100D;
            HashingStatusUpdateEventArgs update;

            byte[] buffer = new byte[bufferSize];
            while (fs.Read(buffer, 0, buffer.Length) > 0)
            {
                long length        = filesize - offset;
                int  currentLength = buffer.Length;
                if (buffer.Length > length)
                {
                    currentLength = (int)length;
                }
                offset += MD5Hasher.TransformBlock(buffer, 0, currentLength, buffer, 0);
                double percentComplete = Math.Round((fs.Position / onePercent), 2);
                update = new HashingStatusUpdateEventArgs(percentComplete, fs.Length, fs.Position);
                HashingStatusUpdate(this, update);
            }
            fs.Close();
            MD5Hasher.TransformFinalBlock(new Byte[0], 0, 0);
            byte[] bHash = MD5Hasher.Hash;
            update = new HashingStatusUpdateEventArgs(100, filesize, filesize); // Finished.
            HashingStatusUpdate(this, update);

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < bHash.Length; i++)
            {
                sb.Append(bHash[i].ToString("x2")); // Return as Hex, duh.
            }
            String sHash = sb.ToString();

            HashingEventArgs e = new HashingEventArgs(sHash);

            if (HashingFinished != null)
            {
                HashingFinished(this, e);
            }
        }
Exemplo n.º 4
0
 private void splash_OnHashingFinished(object sender, HashingEventArgs e)
 {
     splash.Invoke(new CloseDelegate(splash.Close));
     this.Invoke(new ShowDelegate(this.Show));
     this.Invoke(new SetupControlsDelegate(this.SetupControls), e);
 }
Exemplo n.º 5
0
        void SetupControls(HashingEventArgs e)
        {
            // I'm not entirely sure why I'm creating controls by hand.
            // Though it likely has something to do with some initial error regarding cross-thread access.
            // At this point though, it's probably safe to do all this in the designer, but I'm leaving it this way because it works.

            this.Visible = true;
            String md5_hash = e.ChecksumValue;

            // Measuring the size of strings and such.
            Graphics g = CreateGraphics();
            Font font_checksum = new Font("Segoe UI Mono", 24, FontStyle.Regular);
            Font font_button = new Font("Segoe UI", 12, FontStyle.Regular);
            SizeF size_hash = g.MeasureString(md5_hash, font_checksum);
            SizeF size_clipboard = g.MeasureString(bClipboard, font_button);
            SizeF size_exit = g.MeasureString(bExit, font_button);

            // Textbox Properties
            TextBox txtChecksum = new TextBox
                {
                    Name = "txtChecksum",
                    Margin = new Padding(0),
                    Location = new Point(0, 0),
                    TextAlign = HorizontalAlignment.Center,
                    Font = font_checksum,
                    AutoSize = false,
                    Height = (int)Math.Ceiling(size_hash.Height),
                    Width = (int)Math.Ceiling(size_hash.Width),
                    Text = md5_hash
                };
            txtChecksum.Select(txtChecksum.Text.Length, 0); // Deselect the text in the box.

            // Button (Clipboard) Properties
            Button btnClipboard = new Button
                {
                    Margin = new Padding(0),
                    Location = new Point(0, txtChecksum.Location.Y + txtChecksum.Height),
                    TextAlign = ContentAlignment.MiddleCenter,
                    Font = font_button,
                    AutoSize = true,
                    Height = (int)Math.Ceiling(size_clipboard.Height),
                    Width = txtChecksum.Width / 3,
                    Text = bClipboard
                };
            btnClipboard.Click += new EventHandler(btnClipboard_Click);

            // Button (Compare) Properties
            Button btnCompare = new Button
                {
                    Margin = new Padding(0),
                    Location = new Point(btnClipboard.Right, txtChecksum.Location.Y + txtChecksum.Height),
                    TextAlign = ContentAlignment.MiddleCenter,
                    Font = font_button,
                    AutoSize = true,
                    Height = (int)Math.Ceiling(size_exit.Height),
                    Width = txtChecksum.Width / 3,
                    Text = bCompare
                };
            btnCompare.Click += new EventHandler(btnCompare_Click);

            // Textbox (Compare) Properties
            TextBox txtCompare = new TextBox
                {
                    Name = "txtCompare",
                    Margin = new Padding(0),
                    Location = btnCompare.Location,
                    AutoSize = false,
                    TextAlign = HorizontalAlignment.Center,
                    Font = btnCompare.Font,
                    Height = btnCompare.Height,
                    Width = btnCompare.Width,
                    Text = tCompare,
                    Visible = false
                };

            txtCompare.GotFocus += new EventHandler(txtCompare_GotFocus);
            txtCompare.LostFocus += new EventHandler(txtCompare_LostFocus);
            txtCompare.TextChanged += new EventHandler(txtCompare_TextChanged);
            txtCompare.Select(txtCompare.Text.Length, 0); // Deselect the text in the box.

            // Button (Exit) Properties
            Button btnExit = new Button
                {
                    Margin = new Padding(0),
                    Location = new Point(btnCompare.Right, txtChecksum.Location.Y + txtChecksum.Height),
                    TextAlign = ContentAlignment.MiddleCenter,
                    Font = font_button,
                    AutoSize = true,
                    Height = (int)Math.Ceiling(size_exit.Height),
                    Width = txtChecksum.Width / 3,
                    Text = bExit
                };
            btnExit.Click += new EventHandler(btnExit_Click);

            // Form Properties
            this.Text = "MD5 Checksum";
            this.Width = 1;
            this.Height = 1;
            this.AutoSizeMode = AutoSizeMode.GrowOnly;
            this.AutoSize = true;
            this.Controls.Add(txtChecksum);
            this.Controls.Add(btnClipboard);
            this.Controls.Add(btnCompare);
            btnCompare.Parent.Controls.Add(txtCompare);
            this.Controls.Add(btnExit);

            this.Left = (Screen.PrimaryScreen.WorkingArea.Width - this.Width) / 2;
            this.Top = (Screen.PrimaryScreen.WorkingArea.Height - this.Height) / 2;
        }
Exemplo n.º 6
0
        public void GetMD5(Object strFilePath)
        {
            String FilePath = strFilePath.ToString();
            MD5 MD5Hasher = MD5.Create();
            long offset = 0;
            FileStream fs = null;

            try
            {
                fs = new FileStream(FilePath, FileMode.Open);
            }
            catch (Exception)
            {
                // If an exception is thrown, it's likely that the file is in use.
                MessageBox.Show("File is currently in use by another application.");
                Application.Exit();
            }

            if (fs == null)
            {
                // I can't think of a single reason why it would still be null...
                // But just in case, let's handle it.
                // NOTE: I realize this is a very poor way of "handling" exceptions.
                Application.Exit();
            }

            long filesize = fs.Length;
            double onePercent = filesize / 100D;
            HashingStatusUpdateEventArgs update;

            byte[] buffer = new byte[bufferSize];
            while (fs.Read(buffer, 0, buffer.Length) > 0)
            {
                long length = filesize - offset;
                int currentLength = buffer.Length;
                if (buffer.Length > length)
                {
                    currentLength = (int)length;
                }
                offset += MD5Hasher.TransformBlock(buffer, 0, currentLength, buffer, 0);
                double percentComplete = Math.Round((fs.Position / onePercent), 2);
                update = new HashingStatusUpdateEventArgs(percentComplete, fs.Length, fs.Position);
                HashingStatusUpdate(this, update);
            }
            fs.Close();
            MD5Hasher.TransformFinalBlock(new Byte[0], 0, 0);
            byte[] bHash = MD5Hasher.Hash;
            update = new HashingStatusUpdateEventArgs(100, filesize, filesize); // Finished.
            HashingStatusUpdate(this, update);

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < bHash.Length; i++)
            {
                sb.Append(bHash[i].ToString("x2")); // Return as Hex, duh.
            }
            String sHash = sb.ToString();

            HashingEventArgs e = new HashingEventArgs(sHash);
            if (HashingFinished != null)
            {
                HashingFinished(this, e);
            }
        }