Cross Platform Development with Mono

Recently I moved my main home PC and Laptop over to the Linux Mint distribution, which for someone who has been a dedicated Windows users since I ever got my hands on a PC back around 1990, has gone amazingly well. My next little venture is to do a little cross platform development. Although I have moved away from Windows (not strictly true as I have set up Windows 8 in a Virtual Box environment in-case I need to switch over for something) I still really like the .NET environment and C# language, so I would like to carry on using it. I would like to learn another language like Python / Ruby, but I really don’t have the time at the moment as I have just changed job and I also have young kids at home which takes up a lot of my spare time.

Mono Development with C#
Mono Development with C#

Thankfully, there is the Mono .NET implementation that I can use and it is really well supported. Mono is a Free (as in freedom) implementation of the C# language and .NET runtime that is written to the ECMA-334 open specification. As well as being open source, Mono also has a company, Xamarin, sponsoring its development as they use the mono system for their IOS and Android application development system, of which you can also use for free (within limits).

My main interest at the moment is writing desktop applications for both Linux and Windows. Mono does support Windows Forms, but it isn’t as well supported as the Linux Gnome user interface library GTK. Mono has a version called GTK# and it is designed to be cross platform so you can use it with Windows and Mac OSX as-well.

#Develop – Open Source Alternative to Visual Studio

For my entire working career, I have been a Microsoft boy. I use Windows (including desktop and server operating systems) and I am a .NET fanboy. It’s a fun environment to program with, flexible and the tools are great. Also I have never really given open source much thought. Not because I don’t agree with it, or anything negative, but I have just never really bothered with it. Until recently that is.

LibreOffice
LibreOffice

About 7 years ago, I tried Open Office, and I didn’t have a very good experience. More recently I have been having more problems with Microsoft Office. It is slow, clunky, and quite unreliable in that I experience more crashes with it than I would like. I decided to try Open Office again, well more specifically Libre Office as that’s what the cool kids seem to recommend. This suite has had quite a while to mature, and you know what, I love it. It is fast, compact, and reliable. It interoperates with Microsoft Office documents perfectly and is a joy to use, so I have made this a permanent switch.

XMind
XMind

This got me thinking. If something like Libre Office is a slick as it is, and Free (Free as in cost and freedom with the source being open), then what other goodies are out there. This has led me on to switching over to many open source tools instead of commercial tools. I now frequently use Gimp and XMind. I am also looking to switch away from Enterprise Architect to an open source UML tool. I haven’t picked my final tool yet, but Modelio is looking very good.

Block Encrypter .NET Library

I have recently released a small open source library that I thought might be useful to people. The library is called Block Encrypter it is designed to make asymmetric encryption of  data in .NET / C# easier. The code in this library has been developed over the past year and used in my open source tools SafePad and Text Shredder. The way in which this library goes about encryption has been peer reviewed by many people in the open source community so should give you a level of comfort that it is secure in how it goes about encrypting data. Block Encrypter encrypts data using standard cryptographic primitives like AES, HMAC, PBKDF, and cryptographically secure random number generation.

Download the Block Encrypter .NET encryption library.
Download the Block Encrypter .NET encryption library.

I have previously discussed AES encryption in .NET in my cryptography series of articles. I also posted an article linking to some really useful videos by Patrick Townsend about how the AES algorithm works. If you are interested in symmetric cryptography I highly recommend watching them.

encryption

First lets look at some usage examples. The main object in the library to call is the Block Encrypter object and this contains methods that allow you to encrypt/decrypt strings or byte arrays of data.

Overview of the Library

The library itself is quite straight forward to use and there are not that many objects to get to grips with. The main entry point for the library is the BlockEncrypter object. This object will then call out to the GzipCompression object, Aes object, and the ByteHelpers object.

Block Encryter Class Diagram
Block Encryter Class Diagram

The library is also well covered in unit tests that exercise the majority of the functionality.

Phonetic String Matching : Metaphone

In the previous parts of this series I discussed the Soundex and Levenshtein Distance algorithms for phonetic string matching. In this next part of the series I want to introduce the Metaphone algorithm.

Phonetic String Matching with Metaphone
Phonetic String Matching with Metaphone

Metaphone is a phonetic algorithm for indexing words by their English pronunciation. It fundamentally improves on the Soundex algorithm by using information about variations and inconsistencies in English spelling and pronunciation to produce a more accurate encoding, which does a better job of matching words and names which sound similar. As with Soundex, similar sounding words should share the same keys.

Checking the Strength of a Password

In this article I want to talk about a recent password strength checker that I build for my open source application SafePad.

Password Strength Indicator in SafePad
Password Strength Indicator in SafePad

 First of all we have a public enumeration that contains the password score results.

Serializing POCOs into XML

I couldn't think of a suitable image, so here is a programming Dog.
I couldn’t think of a suitable image, so here is a programming Dog.

I am writing this short article as every time I need to serialize a C# object into XML I keep on forgetting how to do it (probably as I am getting old) so I thought I would post a snippet here. More for my own benefit as I am forgetful.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace HauntedHouseSoftware.SecureNotePad.DomainObjects
{
    public sealed class ApplicationSettings
    {
        public int WindowPositionX { get; set; }
        public int WindowPositionY { get; set; }
        public int WindowWidth { get; set; }
        public int WindowHeight { get; set; }
        public FormWindowState FormWindowState { get; set; }
    }
}

First lets start with a simple class that we want to serialize into XML. The class above, ApplicationSettings, was taken from my open source application SafePad. This simple class contains some window position setting that need to be serialized to XML.

Phonetic String Matching : Levenshtein Distance

In the last article I discussed an algorithm for creating Soundex codes. In this article I want to show another algorithm called the Levenshtein Distance algorithm or as otherwise known the Edit Distance algorithm. The Levenshtein Distance algorithm is strictly a phonetic algorithm but it calculates how many edits you need to do to turn string A into String B. This can be illustrated in the diagram below.

Levenshtein Distance Calculated the Number of Changes from One String to Another.
Levenshtein Distance Calculated the Number of Changes from One String to Another.

In the example above, the Levenshtein distance between “kitten” and “sitting” is 3, since the following three edits change one into the other, and there is no way to do it with fewer than three edits:

Phonetic String Matching : Soundex

A phonetic algorithm is an algorithm for indexing of words by their pronunciation. Most phonetic algorithms were developed for use with the English language; consequently, applying the rules to words in other languages might not give a meaningful result.

A phonetic algorithm is an algorithm for indexing of words by their pronunciation.
A phonetic algorithm is an algorithm for indexing of words by their pronunciation.

Typically in our applications we will try to compare literal strings, which only gets you so far. Sometimes we need to be a little cleverer and compare strings based on how they sound as opposed to how they are spelt. In this series of articles I want to show some implementations for some different phonetic comparison algorithms that you can use in your applications. Feel free to take the code from these articles and use them in your software.

Getting the MAC Address for a Machine on the Network

I was working on some code recently where I needed to get the MAC Address for a particular IP Address on the network. I settled on this snippet below. I thought I would share it. It uses a native call to the SendARP method in the iphlpapi.dll. The description below states what the SendARP method does according to the MSDN Documentation.

The SendARP function sends an Address Resolution Protocol (ARP) request to obtain the physical address that corresponds to the specified destination IPv4 address.

This snippet worked for IP4 addresses which was all I needed. I haven’t tried to solve this for IP6 addresses yet. If you have done this for IP6 then it would be great if you can leave a comment.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;

namespace Utility
{
    public sealed class NativeMethods
    {
        [DllImport("iphlpapi.dll", ExactSpelling = true)]
        private static extern int SendARP(int DestIP, int SrcIP, byte[] pMacAddr, ref uint PhyAddrLen);

        private NativeMethods() { }

        public static string GetMacAddress(string ipAddress)
        {
            if (string.IsNullOrEmpty(ipAddress))
            {
                throw new ArgumentNullException("ipAddress");
            }

            IPAddress IP = IPAddress.Parse(ipAddress);
            byte[] macAddr = new byte[6];
            uint macAddrLen = (uint)macAddr.Length;

            if (SendARP((int)IP.Address, 0, macAddr, ref macAddrLen) != 0)
            {
                throw new CardVaultException("ARP command failed");
            }

            string[] str = new string[(int)macAddrLen];

            for (int i = 0; i < macAddrLen; i++)
            {
                str[i] = macAddr[i].ToString("x2");
            }

            return string.Join(":", str).ToUpper();
        }
    }
}
Participate with Coding in the Trenches on Facebook
Participate with Coding in the Trenches on Facebook by Click the button above.

Getting your Machines IP Address

I was working on some code recently where I needed to get the IP Address of the current machine that the code was executing on. I settled on this snippet below. I thought I would share it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;

namespace Utility
{
    public sealed class MachineIPAddress
    {
        private MachineIPAddress() { }

        public static string RetrieveIPAddress
        {
            get
            {
                var hostEntry = Dns.GetHostEntry(Dns.GetHostName());
                var ip = (from addr in hostEntry.AddressList
                          where String.Compare(addr.AddressFamily.ToString(), "InterNetwork", StringComparison.OrdinalIgnoreCase) == 0
                          select addr.ToString()
                    ).FirstOrDefault();

                return ip;
            }
        }
    }
}
Participate with Coding in the Trenches on Facebook
Participate with Coding in the Trenches on Facebook by Click the button above.
%d bloggers like this: