Moving over to the code bit, I define the following in MainWindow (Source of the form), which is the code behind:
using System;
using System.Linq;
using Gtk;
using System.Security.Cryptography;
using System.Text;
public partial class MainWindow: Gtk.Window
{
public MainWindow () : base (Gtk.WindowType.Toplevel)
{
Build ();
btnMd5.Clicked += OnBtnClick;
}
protected void OnDeleteEvent (object sender, DeleteEventArgs a)
{
Application.Quit ();
a.RetVal = true;
}
protected void OnBtnClick (object sender, EventArgs args)
{
var md5 = MD5CryptoServiceProvider.Create ();
byte[] plainTextBytes = Encoding.UTF8.GetBytes (tbPlainText.Buffer.Text);
byte[] hashBytes = md5.ComputeHash (plainTextBytes);
var sbuilder = new StringBuilder ();
sbuilder.Append(string.Join("",
hashBytes.Select(x => x.ToString("x2"))));
tbHash.Buffer.Text = sbuilder.ToString();
}
}
The code above instantiates a MD5CryptoServiceProvder instance, then computes a hash. We get a string representation of the MD5 hash using a StringBuilder and we use ToString("x2") - which assembles a hexidecimal string for us, which is the common way to represent a MD5 hash. A MD5 hash produces 128 bits = 16 bytes = 32 hexidecimal digits. A hexadecimal value can be 0-9 and A-F = 16 different values = one half byte.
We build up our GUI using the GUI designer inside Monodevelop. The GUI designer for GTK# in Monodevelop is called Stetic.
Stetic
No comments:
Post a Comment