<?xml version="1.0" ?><rss version="0.91"><channel><title>NullifyNetwork</title><link>http://www.nullify.net/</link><description>Technical fixes, development commentary and help articles by Simon Soanes</description><language>en-uk</language><copyright>Copyright 2010 Simon Soanes</copyright><lastBuildDate>Fri, 03 Sep 2010 02:56:13 GMT</lastBuildDate><generator>c3 RSS Module</generator><docs>http://www.nullify.net/Default.aspx?page=rss</docs><managingEditor>simon@nullifynetwork.com (Simon Soanes)</managingEditor><webMaster>simon@nullifynetwork.com (Simon Soanes)</webMaster><ttl>60</ttl><item><link>http://www.nullify.net/Article/343.aspx</link><title>Yubikey decryption/authentication class</title><description><![CDATA[<p>So I have encountered a situation where I need to use two factor authentication and recently bought a couple of <a href="http://www.yubico.com/">Yubico&nbsp;YubiKey</a>'s to investigate them as a potential solution where I can't use smart cards (mostly with other users who can't install the appropriate drivers or on locked down systems/internet cafe's).</p>
<p>They are fairly cheap (particularly in bulk) and provide similar functionality to an RSA Securid but without the need to type the password (they act as a keyboard and type onto the computer, but can't be modified by the computer in question). &nbsp;They also have two 'slots' for different tokens, you can touch the button once for one and touch and hold for a second one time key.</p>
<p>Whilst there's plenty of code examples around for them (the company are extremely open), I haven't found a simple, logical C# library/class I could use to do authentication locally (they supply ones for remote auth on their servers) - there were always multiple files and loads of code in peoples projects.</p>
<p>My intention is to write a Windows SubAuthentication DLL (in C++ but I learn in C#) to use with these keys which also still checks your normal password - so when remote desktop'ing, using something that authenticates to Windows or using the Windows Radius server (NPS) to authenticate VPN's you can choose to either supply your normal password, or if you're on a system you don't trust you can use your username/low security pin code and Yubikey.</p>
<p>So here's my take on the decoding of a Yubikey's input - note that I'm not interested in making a software token though so the class doesn't generate values for the random value (if you wanted to do this you just need to ask RNGCryptoProvider for a few bytes of random though) and there's also no incrementing of the sessions, in-session use counters or timestamps included - however I did include the code to encrypt it appropriately if you do supply all the values.</p>
<p>To use this class you just need to call it like so - where privateKey is a byte[] array with the private key in and otpCode is a string like &quot;fkfthfffktbnreffrghcldrffeclcgkt&quot; output from a touch of the Yubikey:-</p>
<pre style="font-family: consolas"><span style="color: #2b91af">YubiKey</span>&nbsp;otp&nbsp;=&nbsp;<span style="color: #2b91af">YubiKey</span>.FromModHex(privateKey,&nbsp;otpCode);<br /><span style="color: maroon">if</span>&nbsp;(otp.UidMatch(checkIdentity))<br />{<br />	<span style="color: #2b91af">Console</span>.WriteLine(<span style="color: blue">&quot;Decoded&nbsp;the&nbsp;YubiKey's&nbsp;OTP&nbsp;code:-&quot;</span>);<br />	<span style="color: #2b91af">Console</span>.WriteLine(<span style="color: blue">&quot;\tSession:\t&quot;</span>&nbsp;+&nbsp;otp.SessionCounter.ToString());<br />	<span style="color: #2b91af">Console</span>.WriteLine(<span style="color: blue">&quot;\tCount&nbsp;in&nbsp;session:\t&quot;</span>&nbsp;+&nbsp;otp.SessionUseCount.ToString());<br />	<span style="color: #2b91af">Console</span>.WriteLine(<span style="color: blue">&quot;\tTime&nbsp;(high):\t&quot;</span>&nbsp;+&nbsp;otp.TimestampHighPart.ToString());<br />	<span style="color: #2b91af">Console</span>.WriteLine(<span style="color: blue">&quot;\tTime&nbsp;(low):\t&quot;</span>&nbsp;+&nbsp;otp.TimestampLowPart.ToString());<br />	<span style="color: #2b91af">Console</span>.WriteLine(<span style="color: blue">&quot;\tTime&nbsp;(low):\t&quot;</span>&nbsp;+&nbsp;otp.Uid.ToString());<br />}<br /><span style="color: maroon">else</span><br />{<br />	<span style="color: #2b91af">Console</span>.WriteLine(<span style="color: blue">&quot;Failed&nbsp;to&nbsp;identify&nbsp;the&nbsp;keys&nbsp;correct&nbsp;OTP&quot;</span>);<br />}</pre>
<p>&nbsp;</p>
<p>And you can decode the private key/identity as they're displayed in the YubiKey personalisation tool with the following handy (but not super efficient) hex to byte algorithms:-</p>
<p>&nbsp;</p>
<pre style="font-family: consolas"><span style="color: maroon">public</span>&nbsp;<span style="color: maroon">static</span>&nbsp;<span style="color: maroon">byte</span>[]&nbsp;StringHexDecode(<span style="color: maroon">string</span>&nbsp;hexData)<br />{<br />	<span style="color: #2b91af">List</span>&lt;<span style="color: maroon">byte</span>&gt;&nbsp;data&nbsp;=&nbsp;<span style="color: maroon">new</span>&nbsp;<span style="color: #2b91af">List</span>&lt;<span style="color: maroon">byte</span>&gt;();<br />	<span style="color: maroon">foreach</span>&nbsp;(<span style="color: maroon">string</span>&nbsp;b&nbsp;<span style="color: maroon">in</span>&nbsp;hexData.Split(<span style="color: blue">'&nbsp;'</span>))<br />	{<br />		data.Add(<span style="color: #2b91af">Convert</span>.ToByte(b,&nbsp;16));<br />	}<br />	<span style="color: maroon">return</span>&nbsp;data.ToArray();<br />}<br /> <br /><span style="color: maroon">public</span>&nbsp;<span style="color: maroon">static</span>&nbsp;<span style="color: maroon">string</span>&nbsp;StringHexEncode(<span style="color: maroon">byte</span>[]&nbsp;hexData)<br />{<br />	<span style="color: maroon">return</span>&nbsp;<span style="color: #2b91af">BitConverter</span>.ToString(hexData).Replace(<span style="color: blue">'-'</span>,&nbsp;<span style="color: blue">'&nbsp;'</span>);<br />}</pre>
<p>And&nbsp;finally here's the actual Yubikey class including the algorithm to decrypt the data that the USB dongles actually output in 'ModHex' which is an odd keyboard language independent format but seems to work quite well:-</p>
<p>&nbsp;</p>
<pre style="font-family: consolas"><pre style="font-family: consolas"><span style="color: gray">///</span><span style="color: green">&nbsp;</span><span style="color: gray">&lt;summary&gt;</span><br /><span style="color: gray">///</span><span style="color: green">&nbsp;A&nbsp;YubiKey&nbsp;OTP&nbsp;is&nbsp;symmetric&nbsp;two-factor&nbsp;auth&nbsp;key,&nbsp;this&nbsp;class&nbsp;allows&nbsp;decoding&nbsp;and&nbsp;validating&nbsp;them</span><br /><span style="color: gray">///</span><span style="color: green">&nbsp;</span><span style="color: gray">&lt;/summary&gt;</span>
<span style="color: maroon">public</span>&nbsp;<span style="color: maroon">class</span>&nbsp;<span style="color: #2b91af">YubiKey</span>
{<br />	<span style="color: maroon">public</span>&nbsp;YubiKey()<br />	{<br />	}<br /> <br />	<span style="color: maroon">private</span>&nbsp;<span style="color: maroon">const</span>&nbsp;<span style="color: maroon">int</span>&nbsp;CRC_OK_RESIDUE&nbsp;=&nbsp;0xf0b8;

	<span style="color: gray">///</span><span style="color: green">&nbsp;</span><span style="color: gray">&lt;summary&gt;</span><br />	<span style="color: gray">///</span><span style="color: green">&nbsp;Unique&nbsp;(secret)&nbsp;ID.</span><br />	<span style="color: gray">///</span><span style="color: green">&nbsp;</span><span style="color: gray">&lt;/summary&gt;</span>
	<span style="color: maroon">public</span>&nbsp;<span style="color: maroon">byte</span>[]&nbsp;Uid&nbsp;=&nbsp;<span style="color: maroon">new</span>&nbsp;<span style="color: maroon">byte</span>[6];
	<span style="color: gray">///</span><span style="color: green">&nbsp;</span><span style="color: gray">&lt;summary&gt;</span><br />	<span style="color: gray">///</span><span style="color: green">&nbsp;Session&nbsp;counter&nbsp;(incremented&nbsp;by&nbsp;1&nbsp;at&nbsp;each&nbsp;startup).&nbsp;&nbsp;High&nbsp;bit</span><br />	<span style="color: gray">///</span><span style="color: green">&nbsp;indicates&nbsp;whether&nbsp;caps-lock&nbsp;triggered&nbsp;the&nbsp;token.</span><br />	<span style="color: gray">///</span><span style="color: green">&nbsp;</span><span style="color: gray">&lt;/summary&gt;</span><br />	<span style="color: maroon">public</span>&nbsp;<span style="color: #2b91af">UInt16</span>&nbsp;SessionCounter;
	<span style="color: gray">///</span><span style="color: green">&nbsp;</span><span style="color: gray">&lt;summary&gt;</span><br />	<span style="color: gray">///</span><span style="color: green">&nbsp;Timestamp&nbsp;incremented&nbsp;by&nbsp;approx&nbsp;8Hz&nbsp;(low&nbsp;part).</span><br />	<span style="color: gray">///</span><span style="color: green">&nbsp;</span><span style="color: gray">&lt;/summary&gt;</span><br />	<span style="color: maroon">public</span>&nbsp;<span style="color: #2b91af">UInt16</span>&nbsp;TimestampLowPart;
	<span style="color: gray">///</span><span style="color: green">&nbsp;</span><span style="color: gray">&lt;summary&gt;</span><br />	<span style="color: gray">///</span><span style="color: green">&nbsp;Timestamp&nbsp;(high&nbsp;part).</span><br />	<span style="color: gray">///</span><span style="color: green">&nbsp;</span><span style="color: gray">&lt;/summary&gt;</span><br />	<span style="color: maroon">public</span>&nbsp;<span style="color: maroon">byte</span>&nbsp;TimestampHighPart;<br />	<span style="color: gray">///</span><span style="color: green">&nbsp;</span><span style="color: gray">&lt;summary&gt;</span><br />	<span style="color: gray">///</span><span style="color: green">&nbsp;Number&nbsp;of&nbsp;times&nbsp;used&nbsp;within&nbsp;session&nbsp;+&nbsp;activation&nbsp;flags.</span><br />	<span style="color: gray">///</span><span style="color: green">&nbsp;</span><span style="color: gray">&lt;/summary&gt;</span>
	<span style="color: maroon">public</span>&nbsp;<span style="color: maroon">byte</span>&nbsp;SessionUseCount;
	<span style="color: gray">///</span><span style="color: green">&nbsp;</span><span style="color: gray">&lt;summary&gt;</span><br />	<span style="color: gray">///</span><span style="color: green">&nbsp;Pseudo-random&nbsp;value.</span><br />	<span style="color: gray">///</span><span style="color: green">&nbsp;</span><span style="color: gray">&lt;/summary&gt;</span><br />	<span style="color: maroon">public</span>&nbsp;<span style="color: #2b91af">UInt16</span>&nbsp;RandomValue;
	<span style="color: gray">///</span><span style="color: green">&nbsp;</span><span style="color: gray">&lt;summary&gt;</span><br />	<span style="color: gray">///</span><span style="color: green">&nbsp;CRC16&nbsp;value&nbsp;of&nbsp;all&nbsp;fields.</span><br />	<span style="color: gray">///</span><span style="color: green">&nbsp;</span><span style="color: gray">&lt;/summary&gt;</span><br />	<span style="color: maroon">public</span>&nbsp;<span style="color: #2b91af">UInt16</span>&nbsp;CRC;<br /> <br />	<span style="color: gray">///</span><span style="color: green">&nbsp;</span><span style="color: gray">&lt;summary&gt;</span><br />	<span style="color: gray">///</span><span style="color: green">&nbsp;Does&nbsp;the&nbsp;included&nbsp;UID&nbsp;match&nbsp;the&nbsp;one&nbsp;we&nbsp;expected?</span><br />	<span style="color: gray">///</span><span style="color: green">&nbsp;</span><span style="color: gray">&lt;/summary&gt;</span><br />	<span style="color: gray">///</span><span style="color: green">&nbsp;</span><span style="color: gray">&lt;param&nbsp;name=</span><span style="color: gray">&quot;uid&quot;</span><span style="color: gray">&gt;&lt;/param&gt;</span><br />	<span style="color: gray">///</span><span style="color: green">&nbsp;</span><span style="color: gray">&lt;returns&gt;&lt;/returns&gt;</span><br />	<span style="color: maroon">public</span>&nbsp;<span style="color: maroon">bool</span>&nbsp;UidMatch(<span style="color: maroon">byte</span>[]&nbsp;uid)<br />	{<br />		<span style="color: maroon">for</span>&nbsp;(<span style="color: maroon">int</span>&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;uid.Length;&nbsp;i++)<br />		{<br />			<span style="color: maroon">if</span>&nbsp;(Uid[i]&nbsp;!=&nbsp;uid[i])<br />			{<br />				<span style="color: maroon">return</span>&nbsp;<span style="color: maroon">false</span>;<br />			}<br />		}<br />		<span style="color: maroon">return</span>&nbsp;<span style="color: maroon">true</span>;<br />	}<br /> <br /><span style="color: blue">	#region</span>&nbsp;Post&nbsp;Decryption&nbsp;Conversion<br /> <br />	<span style="color: maroon">private</span>&nbsp;<span style="color: maroon">static</span>&nbsp;<span style="color: maroon">int</span>&nbsp;calculateCrc(<span style="color: maroon">byte</span>[]&nbsp;b)<br />	{<br />		<span style="color: maroon">int</span>&nbsp;crc&nbsp;=&nbsp;0xffff;<br /> <br />		<span style="color: maroon">for</span>&nbsp;(<span style="color: maroon">int</span>&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;b.Length;&nbsp;i&nbsp;+=&nbsp;1)<br />		{<br />			crc&nbsp;^=&nbsp;b[i]&nbsp;&amp;&nbsp;0xFF;<br />			<span style="color: maroon">for</span>&nbsp;(<span style="color: maroon">int</span>&nbsp;j&nbsp;=&nbsp;0;&nbsp;j&nbsp;&lt;&nbsp;8;&nbsp;j++)<br />			{<br />				<span style="color: maroon">int</span>&nbsp;n&nbsp;=&nbsp;crc&nbsp;&amp;&nbsp;1;<br />				crc&nbsp;&gt;&gt;=&nbsp;1;<br />				<span style="color: maroon">if</span>&nbsp;(n&nbsp;!=&nbsp;0)<br />				{<br />					crc&nbsp;^=&nbsp;0x8408;<br />				}<br />			}<br />		}<br />		<span style="color: maroon">return</span>&nbsp;crc;<br />	}<br /> <br />	<span style="color: maroon">internal</span>&nbsp;<span style="color: maroon">static</span>&nbsp;<span style="color: #2b91af">YubiKey</span>&nbsp;OtpFromRawByteArray(<span style="color: maroon">byte</span>[]&nbsp;input)<br />	{<br />		<span style="color: maroon">if</span>&nbsp;(input.Length&nbsp;&lt;&nbsp;16)<br />		{<br />			<span style="color: maroon">throw</span>&nbsp;<span style="color: maroon">new</span>&nbsp;<span style="color: #2b91af">YubiKeyException</span>(<span style="color: blue">&quot;Invalid&nbsp;OTP&nbsp;data&nbsp;-&nbsp;the&nbsp;amount&nbsp;supplied&nbsp;was<br /><span class="Apple-style-span" style="color: rgb(0, 0, 0); ">		</span>insufficient&nbsp;for&nbsp;a&nbsp;six&nbsp;byte&nbsp;identity.&quot;</span>);<br />		}<br /> <br />		<span style="color: maroon">if</span>&nbsp;(calculateCrc(input)&nbsp;!=&nbsp;CRC_OK_RESIDUE)<br />		{<br />			<span style="color: maroon">throw</span>&nbsp;<span style="color: maroon">new</span>&nbsp;<span style="color: #2b91af">YubiKeyException</span>(<span style="color: blue">&quot;CRC&nbsp;was&nbsp;invalid&nbsp;on&nbsp;that&nbsp;OTP&quot;</span>);<br />		}<br /> <br />		<span style="color: #2b91af">YubiKey</span>&nbsp;u&nbsp;=&nbsp;<span style="color: maroon">new</span>&nbsp;<span style="color: #2b91af">YubiKey</span>();<br />		u.Uid&nbsp;=&nbsp;input.Take(6).ToArray();<br />		u.SessionCounter&nbsp;=&nbsp;<span style="color: #2b91af">BitConverter</span>.ToUInt16(input,&nbsp;6);<br />		u.TimestampLowPart&nbsp;=&nbsp;<span style="color: #2b91af">BitConverter</span>.ToUInt16(input,&nbsp;8);<br />		u.TimestampHighPart&nbsp;=&nbsp;input[10];<br />		u.SessionUseCount&nbsp;=&nbsp;input[11];<br />		u.RandomValue&nbsp;=&nbsp;<span style="color: #2b91af">BitConverter</span>.ToUInt16(input,&nbsp;12);<br />		u.CRC&nbsp;=&nbsp;<span style="color: #2b91af">BitConverter</span>.ToUInt16(input,&nbsp;14);<br />		<span style="color: maroon">return</span>&nbsp;u;<br />	}<br /> <br />	<span style="color: maroon">internal</span>&nbsp;<span style="color: maroon">static</span>&nbsp;<span style="color: maroon">byte</span>[]&nbsp;RawByteArrayFromOtp(<span style="color: #2b91af">YubiKey</span>&nbsp;input)<br />	{<br />		<span style="color: #2b91af">List</span>&lt;<span style="color: maroon">byte</span>&gt;&nbsp;data&nbsp;=&nbsp;<span style="color: maroon">new</span>&nbsp;<span style="color: #2b91af">List</span>&lt;<span style="color: maroon">byte</span>&gt;();<br />		data.AddRange(input.Uid);<br />		data.AddRange(<span style="color: #2b91af">BitConverter</span>.GetBytes(input.SessionCounter));<br />		data.AddRange(<span style="color: #2b91af">BitConverter</span>.GetBytes(input.TimestampLowPart));<br />		data.Add(input.TimestampHighPart);<br />		data.Add(input.SessionUseCount);<br />		data.AddRange(<span style="color: #2b91af">BitConverter</span>.GetBytes(input.RandomValue));<br />		data.AddRange(<span style="color: #2b91af">BitConverter</span>.GetBytes(input.CRC));<br />		<span style="color: maroon">return</span>&nbsp;data.ToArray();<br />	}<br /><span style="color: blue">	#endregion</span><br /> <br /><span style="color: blue">	#region</span>&nbsp;Cryptographic&nbsp;wrapper<br />	<span style="color: maroon">internal</span>&nbsp;<span style="color: maroon">byte</span>[]&nbsp;AESEncrypt(<span style="color: maroon">byte</span>[]&nbsp;data,&nbsp;<span style="color: maroon">byte</span>[]&nbsp;key,&nbsp;<span style="color: maroon">byte</span>[]&nbsp;iv)<br />	{<br />		<span style="color: #2b91af">Aes</span>&nbsp;enc&nbsp;=&nbsp;<span style="color: #2b91af">Aes</span>.Create();<br />		enc.Key&nbsp;=&nbsp;key;<br />		enc.IV&nbsp;=&nbsp;iv;<br />		enc.Padding&nbsp;=&nbsp;<span style="color: #2b91af">PaddingMode</span>.None;<br />		<span style="color: maroon">using</span>&nbsp;(ICryptoTransform&nbsp;transform&nbsp;=&nbsp;enc.CreateEncryptor())<br />		{<br />			<span style="color: maroon">byte</span>[]&nbsp;output&nbsp;=&nbsp;transform.TransformFinalBlock(data,&nbsp;0,&nbsp;data.Length);<br />			<span style="color: maroon">return</span>&nbsp;output;<br />		}<br />	}<br /> <br />	<span style="color: maroon">internal</span>&nbsp;<span style="color: maroon">static</span>&nbsp;<span style="color: maroon">byte</span>[]&nbsp;AESDecrypt(<span style="color: maroon">byte</span>[]&nbsp;data,&nbsp;<span style="color: maroon">byte</span>[]&nbsp;key,&nbsp;<span style="color: maroon">byte</span>[]&nbsp;iv)<br />	{<br />		<span style="color: #2b91af">Aes</span>&nbsp;aesImplementation&nbsp;=&nbsp;<span style="color: #2b91af">Aes</span>.Create();<br />		aesImplementation.Key&nbsp;=&nbsp;key;<br />		aesImplementation.IV&nbsp;=&nbsp;iv;<br />		aesImplementation.Padding&nbsp;=&nbsp;<span style="color: #2b91af">PaddingMode</span>.None;<br /> <br />		<span style="color: maroon">using</span>&nbsp;(ICryptoTransform&nbsp;transform&nbsp;=&nbsp;aesImplementation.CreateDecryptor())<br />		{<br />			<span style="color: maroon">byte</span>[]&nbsp;output&nbsp;=&nbsp;transform.TransformFinalBlock(data,&nbsp;0,&nbsp;data.Length);<br /> <br />			<span style="color: maroon">return</span>&nbsp;output;<br />		}<br />	}<br /><span style="color: blue">	#endregion</span><br /> <br /><span style="color: blue">	#region</span>&nbsp;ModHex&nbsp;Support<br /> <br />	<span style="color: maroon">private</span>&nbsp;<span style="color: maroon">const</span>&nbsp;<span style="color: maroon">string</span>&nbsp;alphabet&nbsp;=&nbsp;<span style="color: blue">&quot;cbdefghijklnrtuv&quot;</span>;<br /> <br />	<span style="color: maroon">internal</span>&nbsp;<span style="color: maroon">static</span>&nbsp;<span style="color: maroon">string</span>&nbsp;ModHexEncode(<span style="color: maroon">byte</span>[]&nbsp;data)<br />	{<br />		<span style="color: #2b91af">StringBuilder</span>&nbsp;result&nbsp;=&nbsp;<span style="color: maroon">new</span>&nbsp;<span style="color: #2b91af">StringBuilder</span>();<br /> <br />		<span style="color: maroon">for</span>&nbsp;(<span style="color: maroon">int</span>&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;data.Length;&nbsp;i++)<br />		{<br />			result.Append(alphabet[(data[i]&nbsp;&gt;&gt;&nbsp;4)&nbsp;&amp;&nbsp;0xf]);<br />			result.Append(alphabet[data[i]&nbsp;&amp;&nbsp;0xf]);<br />		}<br /> <br />		<span style="color: maroon">return</span>&nbsp;result.ToString();<br />	}<br /> <br />	<span style="color: maroon">internal</span>&nbsp;<span style="color: maroon">static</span>&nbsp;<span style="color: maroon">byte</span>[]&nbsp;ModHexDecode(<span style="color: #2b91af">String</span>&nbsp;s)<br />	{<br />		<span style="color: #2b91af">List</span>&lt;<span style="color: maroon">byte</span>&gt;&nbsp;baos&nbsp;=&nbsp;<span style="color: maroon">new</span>&nbsp;<span style="color: #2b91af">List</span>&lt;<span style="color: maroon">byte</span>&gt;();<br />		<span style="color: maroon">int</span>&nbsp;len&nbsp;=&nbsp;s.Length;<br /> <br />		<span style="color: maroon">bool</span>&nbsp;toggle&nbsp;=&nbsp;<span style="color: maroon">false</span>;<br />		<span style="color: maroon">int</span>&nbsp;keep&nbsp;=&nbsp;0;<br /> <br />		<span style="color: maroon">for</span>&nbsp;(<span style="color: maroon">int</span>&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;len;&nbsp;i++)<br />		{<br />			<span style="color: maroon">char</span>&nbsp;ch&nbsp;=&nbsp;s[i];<br />			<span style="color: maroon">int</span>&nbsp;n&nbsp;=&nbsp;alphabet.IndexOf(ch.ToString().ToLower());<br />			<span style="color: maroon">if</span>&nbsp;(n&nbsp;==&nbsp;-1)<br />			{<br />				<span style="color: maroon">throw</span>&nbsp;<span style="color: maroon">new</span>&nbsp;<span style="color: #2b91af">YubiKeyException</span>(s&nbsp;+&nbsp;<span style="color: blue">&quot;&nbsp;is&nbsp;not&nbsp;properly&nbsp;encoded&quot;</span>);<br />			}<br /> <br />			toggle&nbsp;=&nbsp;!toggle;<br /> <br />			<span style="color: maroon">if</span>&nbsp;(toggle)<br />			{<br />				keep&nbsp;=&nbsp;n;<br />			}<br />			<span style="color: maroon">else</span><br />			{<br />				baos.Add((<span style="color: maroon">byte</span>)((keep&nbsp;&lt;&lt;&nbsp;4)&nbsp;|&nbsp;n));<br />			}<br />		}<br />		<span style="color: maroon">return</span>&nbsp;baos.ToArray();<br />	}<br /> <br /><span style="color: blue">	#endregion</span><br /> <br /><span style="color: blue">	#region</span>&nbsp;Factory&nbsp;helpers<br />	<span style="color: gray">///</span><span style="color: green">&nbsp;</span><span style="color: gray">&lt;summary&gt;</span><br />	<span style="color: gray">///</span><span style="color: green">&nbsp;Create&nbsp;a&nbsp;Yubikey&nbsp;object&nbsp;from&nbsp;the&nbsp;mod&nbsp;hex&nbsp;and&nbsp;the&nbsp;private&nbsp;key</span><br />	<span style="color: gray">///</span><span style="color: green">&nbsp;</span><span style="color: gray">&lt;/summary&gt;</span><br />	<span style="color: gray">///</span><span style="color: green">&nbsp;</span><span style="color: gray">&lt;param&nbsp;name=</span><span style="color: gray">&quot;privateKey&quot;</span><span style="color: gray">&gt;</span><span style="color: green">The&nbsp;private&nbsp;key&nbsp;to&nbsp;decrypt&nbsp;with</span><span style="color: gray">&lt;/param&gt;</span><br />	<span style="color: gray">///</span><span style="color: green">&nbsp;</span><span style="color: gray">&lt;param&nbsp;name=</span><span style="color: gray">&quot;modHex&quot;</span><span style="color: gray">&gt;</span><span style="color: green">The&nbsp;modhex&nbsp;content</span><span style="color: gray">&lt;/param&gt;</span><br />	<span style="color: gray">///</span><span style="color: green">&nbsp;</span><span style="color: gray">&lt;returns&gt;</span><span style="color: green">A&nbsp;yubikey&nbsp;object</span><span style="color: gray">&lt;/returns&gt;</span><br />	<span style="color: maroon">public</span>&nbsp;<span style="color: maroon">static</span>&nbsp;<span style="color: #2b91af">YubiKey</span>&nbsp;FromModHex(<span style="color: maroon">byte</span>[]&nbsp;privateKey,&nbsp;<span style="color: maroon">string</span>&nbsp;modHex)<br />	{<br />		<span style="color: green">//no&nbsp;IV&nbsp;in&nbsp;use</span><br />		<span style="color: maroon">byte</span>[]&nbsp;inputOTP&nbsp;=&nbsp;ModHexDecode(modHex);<br />		<span style="color: maroon">byte</span>[]&nbsp;decrypted&nbsp;=&nbsp;AESDecrypt(inputOTP,&nbsp;privateKey,&nbsp;<span style="color: maroon">new</span>&nbsp;<span style="color: maroon">byte</span>[16]);<br />		<span style="color: maroon">return</span>&nbsp;OtpFromRawByteArray(decrypted);<br />	}<br /> <br />	<span style="color: gray">///</span><span style="color: green">&nbsp;</span><span style="color: gray">&lt;summary&gt;</span><br />	<span style="color: gray">///</span><span style="color: green">&nbsp;Convert&nbsp;this&nbsp;yubikey&nbsp;instance&nbsp;into&nbsp;a&nbsp;mod-hex&nbsp;OTP&nbsp;string</span><br />	<span style="color: gray">///</span><span style="color: green">&nbsp;</span><span style="color: gray">&lt;/summary&gt;</span><br />	<span style="color: gray">///</span><span style="color: green">&nbsp;</span><span style="color: gray">&lt;param&nbsp;name=</span><span style="color: gray">&quot;privateKey&quot;</span><span style="color: gray">&gt;</span><span style="color: green">The&nbsp;private&nbsp;key&nbsp;to&nbsp;encrypt&nbsp;with</span><span style="color: gray">&lt;/param&gt;</span><br />	<span style="color: gray">///</span><span style="color: green">&nbsp;</span><span style="color: gray">&lt;returns&gt;</span><span style="color: green">The&nbsp;modhext&nbsp;content</span><span style="color: gray">&lt;/returns&gt;</span><br />	<span style="color: maroon">public</span>&nbsp;<span style="color: maroon">string</span>&nbsp;ToModHex(<span style="color: maroon">byte</span>[]&nbsp;privateKey)<br />	{<br />		<span style="color: maroon">byte</span>[]&nbsp;clearotp&nbsp;=&nbsp;RawByteArrayFromOtp(<span style="color: maroon">this</span>);<br />		<span style="color: maroon">byte</span>[]&nbsp;encrypted&nbsp;=&nbsp;AESEncrypt(clearotp,&nbsp;privateKey,&nbsp;<span style="color: maroon">new</span>&nbsp;<span style="color: maroon">byte</span>[16]);<br />		<span style="color: maroon">return</span>&nbsp;ModHexEncode(encrypted);<br />	}<br /><span style="color: blue">	#endregion</span><br /> <br />	<span style="color: maroon">public</span>&nbsp;<span style="color: maroon">class</span>&nbsp;<span style="color: #2b91af">YubiKeyException</span>&nbsp;:&nbsp;<span style="color: #2b91af">Exception</span><br />	{<br />		<span style="color: maroon">public</span>&nbsp;YubiKeyException(<span style="color: maroon">string</span>&nbsp;message)&nbsp;:&nbsp;<span style="color: maroon">base</span>(message)<br />		{<br /> <br />		}<br />	}
}</pre>
</pre>
<p>Once decrypted it is a simple matter to verify it is a key with a particular code (the simple act of decryption should do that but there's a UID included inside too) and that the Session counter and SessionUseCount have been incremented appropriately to prevent re-use of existing keys.</p>
<p>Note that if you are trying to decode a key which has a prefix added you are interested in the last 32 characters of the output - the first few characters by default on a key are a static identifier so that it's possible to identify which encryption key to use to decrypt the token; I've turned this off and favour the user having to enter their own username/password prior to using the key.</p>]]></description><guid>http://www.nullify.net/ViewArticle.aspx?article=343</guid><pubDate>Sat, 31 Jul 2010 16:58:27 GMT</pubDate><category>CodeBlog</category></item>
<item><link>http://www.nullify.net/Article/342.aspx</link><title>Sorry for the lack of posts!</title><description><![CDATA[<p>Just a quick note that posting will resume at some point as I'm going to start some more hobby projects, due to some issues with Zorg Solutions though I took a contract and I try to avoid posting about anything work related on my blog.</p>
<p>I've not been doing much proper software development out of work, just helping the odd person with things and making sure I am up-to-speed with things like Windows Azure, Visual Studio 2010 and the new features of C++0x (I ended up doing quite a bit of C++ at my previous job at one point, and found I can do stuff in it with just a bit more time spent working out what library to use than C#, so it seems sensible to keep up to speed with it!).</p>]]></description><guid>http://www.nullify.net/ViewArticle.aspx?article=342</guid><pubDate>Sat, 05 Jun 2010 11:27:44 GMT</pubDate><category>Blog</category></item>
<item><link>http://www.nullify.net/Article/341.aspx</link><title>Luhn check digit algorithm</title><description><![CDATA[<p>Here's one from my distant past but which may be useful to me at some point today.  I can actually see plenty of quicker ways to achieve this but it's also well tested for generating/checking credit card check digits:-</p>
<!-- code formatted by http://manoli.net/csharpformat/ -->
<pre class="csharpcode"><span class="rem">/// &lt;summary&gt;</span>
    <span class="rem">/// Creates/appends Luhn/Mod10 check digits to a specified numeric string</span>
    <span class="rem">/// Goes belly up with a text string.</span>
    <span class="rem">/// &lt;/summary&gt;</span>
    <span class="kwrd">public</span> <span class="kwrd">class</span> LuhnCheck
    {
        <span class="rem">/// &lt;summary&gt;</span>
        <span class="rem">/// Initialise the LuhnCheck class with a number</span>
        <span class="rem">/// &lt;/summary&gt;</span>
        <span class="rem">/// &lt;param name=&quot;numberToAddDigitTo&quot;&gt;The number to do work on&lt;/param&gt;</span>
        <span class="kwrd">public</span> LuhnCheck(<span class="kwrd">string</span> numberToAddDigitTo)
        {
            _numberToAddDigitTo = numberToAddDigitTo;
            setupok = <span class="kwrd">true</span>;
        }
        <span class="rem">/// &lt;summary&gt;</span>
        <span class="rem">/// Initialise the LuhnCheck class without a number to start with</span>
        <span class="rem">/// &lt;/summary&gt;</span>
        <span class="kwrd">public</span> LuhnCheck()
        {
            _numberToAddDigitTo = <span class="str">&quot;&quot;</span>;
        }

        <span class="kwrd">private</span> <span class="kwrd">string</span> _numberToAddDigitTo;
        <span class="rem">/// &lt;summary&gt;</span>
        <span class="rem">/// The number to work on</span>
        <span class="rem">/// &lt;/summary&gt;</span>
        <span class="kwrd">public</span> <span class="kwrd">string</span> NumberToAddDigitTo
        {
            get
            {
                <span class="kwrd">return</span> _numberToAddDigitTo;
            }
            set
            {
                _numberToAddDigitTo = <span class="kwrd">value</span>;
                setupok = <span class="kwrd">true</span>;
            }
        }
        <span class="kwrd">private</span> <span class="kwrd">bool</span> setupok = <span class="kwrd">false</span>;

        <span class="rem">/// &lt;summary&gt;</span>
        <span class="rem">/// Add the check digit to the string and return the whole string</span>
        <span class="rem">/// &lt;/summary&gt;</span>
        <span class="rem">/// &lt;returns&gt;The whole string with added check digit&lt;/returns&gt;</span>
        <span class="kwrd">public</span> <span class="kwrd">string</span> AddCheckDigit()
        {
            <span class="kwrd">if</span> (setupok)
            {
                NumberToAddDigitTo += Convert.ToString(CreateCheckDigit());
                <span class="kwrd">return</span> NumberToAddDigitTo;
            }
            <span class="kwrd">else</span>
            {
                <span class="kwrd">throw</span> <span class="kwrd">new</span> ArgumentException(<span class="str">&quot;Please specify the number to add the digit to.&quot;</span>);
            }
        }
        <span class="rem">/// &lt;summary&gt;</span>
        <span class="rem">/// Creates a check digit without using it</span>
        <span class="rem">/// &lt;/summary&gt;</span>
        <span class="rem">/// &lt;returns&gt;The check digit&lt;/returns&gt;</span>
        <span class="kwrd">public</span> <span class="kwrd">int</span> CreateCheckDigit()
        {
            <span class="kwrd">if</span> (setupok)
            {
                <span class="kwrd">return</span> CreateCheckDigit(NumberToAddDigitTo);
            }
            <span class="kwrd">else</span>
            {
                <span class="kwrd">throw</span> <span class="kwrd">new</span> ArgumentException(<span class="str">&quot;Please specify the number to calculate the digit for.&quot;</span>);
            }
        }
        <span class="rem">/// &lt;summary&gt;</span>
        <span class="rem">/// Creates the check digit for a specified numeric string - it does not add it, only calculates it</span>
        <span class="rem">/// &lt;/summary&gt;</span>
        <span class="rem">/// &lt;param name=&quot;numberToAddDigitTo&quot;&gt;The number to work the check digit out for&lt;/param&gt;</span>
        <span class="rem">/// &lt;returns&gt;The check digit&lt;/returns&gt;</span>
        <span class="kwrd">public</span> <span class="kwrd">int</span> CreateCheckDigit(<span class="kwrd">string</span> numberToAddDigitTo)
        {
            <span class="kwrd">string</span> validChars = <span class="str">&quot;0123456789&quot;</span>;
            <span class="kwrd">foreach</span> (<span class="kwrd">char</span> c <span class="kwrd">in</span> numberToAddDigitTo)
            {
                <span class="kwrd">if</span> (!(validChars.IndexOf(c) &gt;= 0))
                    <span class="kwrd">throw</span> <span class="kwrd">new</span> FormatException(<span class="str">&quot;The value passed in was not a valid number&quot;</span>);
            }

            <span class="kwrd">string</span> tocheckdigit = numberToAddDigitTo + <span class="str">&quot;0&quot;</span>; <span class="rem">//We add zero to use it to calculate</span>
            <span class="rem">//the number from the correct point - since we'll be alternating between multiplying it by one or two</span>
            <span class="kwrd">int</span> strlen = tocheckdigit.Length;
            <span class="kwrd">int</span> ncheck = 0;
            <span class="kwrd">int</span> nweight = 1; <span class="rem">//this will alternate between one and two</span>
            <span class="kwrd">int</span> intermediary = 0;
            <span class="kwrd">string</span> tosplit = <span class="str">&quot;&quot;</span>;

            <span class="kwrd">for</span> (<span class="kwrd">int</span> i = 1; i &lt;= strlen; i++) <span class="rem">//loop through the card number string</span>
            {
                intermediary = nweight * Convert.ToInt32(Convert.ToString(tocheckdigit[strlen - i])); <span class="rem">//make intermediary</span>
<span class="rem">//be the multiple of the variance and the card number</span>
                <span class="kwrd">if</span> (intermediary &gt; 9) <span class="rem">//if it's bigger than ten...</span>
                {
                    tosplit = Convert.ToString(intermediary);
                    <span class="rem">//Split the two digits</span>
                    <span class="kwrd">int</span> firstdigit = Convert.ToInt32(Convert.ToString(tosplit[0]));
                    <span class="kwrd">int</span> seconddigit = Convert.ToInt32(Convert.ToString(tosplit[1]));
                    <span class="rem">//Then add them together</span>
                    intermediary = firstdigit + seconddigit;
                }
                <span class="rem">//Add the intermediary result to the rolling total</span>
                ncheck += intermediary;
                <span class="rem">//Change the variance as per the Luhn formula</span>
                <span class="kwrd">if</span> (nweight == 2)
                {
                    nweight = 1;
                }
                <span class="kwrd">else</span>
                {
                    nweight = 2;
                }
            }
            <span class="rem">//ncheck now has the total</span>
            <span class="kwrd">int</span> remainder = 0;
            <span class="rem">//Work out the remainder</span>
            Math.DivRem(ncheck, 10, <span class="kwrd">out</span> remainder);
            <span class="rem">//return ten minus the remainder (if it's zero then it divides perfectly by ten so is valid,</span>
            <span class="rem">//but since we're CREATING a check digit we want to know how far off the result is and how much we</span>
	    <span class="rem">//need to correct it using the check digit)</span>
            <span class="kwrd">if</span> (10 - remainder == 10)
            {
                <span class="kwrd">return</span> 0;
            }
            <span class="kwrd">else</span>
            {
                <span class="kwrd">return</span> 10 - remainder;
            }

        }
        <span class="rem">/// &lt;summary&gt;</span>
        <span class="rem">/// Whether the number stored in the object is a valid MOD 10/Luhn check digited number</span>
        <span class="rem">/// &lt;/summary&gt;</span>
        <span class="kwrd">public</span> <span class="kwrd">bool</span> IsValid
        {
            get
            {
                <span class="kwrd">if</span> (CreateCheckDigit() == 0)
                {
                    <span class="kwrd">return</span> <span class="kwrd">true</span>;
                }
                <span class="kwrd">else</span>
                {
                    <span class="kwrd">return</span> <span class="kwrd">false</span>;
                }
            }
        }
    }</pre>]]></description><guid>http://www.nullify.net/ViewArticle.aspx?article=341</guid><pubDate>Wed, 31 Mar 2010 10:41:03 GMT</pubDate><category>CodeBlog</category></item>
<item><link>http://www.nullify.net/Article/340.aspx</link><title>Command Line Arguments in .NET are Broken</title><description><![CDATA[<p>Attempting to pass paths with a space or any other command line argument that requires being enclosed in quotes was causing me some mild frustration, it looks like the .NET implementation that fills args is incorrectly mangling the data that is passed into it. &nbsp;A major bug that shouldn't have gotten through QA exists somewhere...</p>
<p>If you pass a path into a program that is foreach-ing over the args collection through the debug properties in visual studio (and presumably through anywhere else!) you go from:-</p>
<p><span style="font-family: 'Courier New'; ">program.exe /path &quot;c:\my documents\&quot; /dostuff 1</span></p>
<p>Turn into two rather than four arguments:-</p>
<p>args[0] is <span style="font-family: 'Courier New'; ">/path</span><br />
args[1] is&nbsp;<span style="font-family: 'Courier New'; ">c:\my documents&quot; /dostuff 1</span></p>
<p>Which is downright strange, as you'd expect to get:-</p>
<p>args[0] is <span style="font-family: 'Courier New'; ">/path</span><br />
args[1] is&nbsp;<span style="font-family: 'Courier New'; ">c:\my documents\</span><br />
args[2] is <span style="font-family: 'Courier New'; ">/dostuff</span><br />
args[3] is <span style="font-family: 'Courier New'; ">1</span></p>
<p>What on earth are they doing to cause such a mess to command line arguments... Anyway, whilst I'm not sure when this started but thankfully the Environment.CommandLine is left intact and undamaged, so you can do something like this to fix the problem:-</p>
<!-- code formatted by http://manoli.net/csharpformat/ -->
<pre class="csharpcode"><span class="kwrd">private</span> <span class="kwrd">static</span> <span class="kwrd">string</span>[] FixedExtractCmdLineElements()
        {
            List&lt;<span class="kwrd">string</span>&gt; elements = <span class="kwrd">new</span> List&lt;<span class="kwrd">string</span>&gt;();

            <span class="kwrd">string</span>[] firstParts = Environment.CommandLine.Split(<span class="str">' '</span>);

            <span class="rem">//reparse it</span>
            StringBuilder temporaryPart = <span class="kwrd">new</span> StringBuilder(<span class="str">&quot;&quot;</span>);
            <span class="kwrd">bool</span> inside = <span class="kwrd">false</span>;
            <span class="kwrd">foreach</span> (<span class="kwrd">string</span> part <span class="kwrd">in</span> firstParts)
            {
                <span class="rem">//are we inside a quoted part, or did we just find a quote?</span>
                <span class="kwrd">if</span> (!inside &amp;&amp; !part.Contains(<span class="str">&quot;\&quot;&quot;</span>))
                {
                    elements.Add(part);
                }
                <span class="kwrd">else</span>
                {
                    <span class="kwrd">if</span> (inside)
                    {
                        <span class="rem">//we are still inside a quote...</span>
                        temporaryPart.Append(<span class="str">&quot; &quot;</span> + part);

                        <span class="kwrd">if</span> (part.Contains(<span class="str">&quot;\&quot;&quot;</span>))
                        {
                            <span class="rem">//then we are also at the end!</span>
                            elements.Add(temporaryPart.Replace(<span class="str">&quot;\&quot;&quot;</span>, <span class="str">&quot;&quot;</span>).ToString()); <span class="rem">//add the part minus its quotes to the array</span>
                            <span class="rem">//all done!</span>
                            inside = <span class="kwrd">false</span>;
                        }
                    }
                    <span class="kwrd">else</span>
                    {
                        <span class="rem">//else we just found a quote!</span>
                        inside = <span class="kwrd">true</span>;
                        temporaryPart = <span class="kwrd">new</span> StringBuilder(part);
                    }
                }
            }

            <span class="kwrd">return</span> elements.ToArray();
        }</pre>]]></description><guid>http://www.nullify.net/ViewArticle.aspx?article=340</guid><pubDate>Wed, 24 Mar 2010 13:34:52 GMT</pubDate><category>CodeBlog</category></item>
<item><link>http://www.nullify.net/Article/339.aspx</link><title>Quick AES encryption methods in .NET</title><description><![CDATA[<p>This is mostly so I don't have to ever type this in again, but it's most useful when you're also using an RSACryptoProvider to encrypt and send the key to the other end where you're decrypting whatever you are sending, but I thought these might be of use to someone... &nbsp;There's really nothing complicated to these at all.</p>
<p>Note that the iv (initialisation vector) should be some random data that you pass with the encrypted data, but it does not <em>need </em>to be protected. &nbsp;It's used to prevent two duplicate bits of input data outputting the exact same (and therefore being obvious), and to prevent someone making you encrypt something special in a way that you can have your private key exposed.</p>
<!-- code formatted by http://manoli.net/csharpformat/ -->
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">string</span> AESEncrypt(<span class="kwrd">string</span> input, <span class="kwrd">byte</span>[] key, <span class="kwrd">byte</span>[] iv)
{
    Aes enc = Aes.Create();
    enc.Key = key;
    enc.IV = iv;
    <span class="kwrd">using</span> (ICryptoTransform transform = enc.CreateEncryptor())
    {
        <span class="kwrd">byte</span>[] data = Encoding.Unicode.GetBytes(input);
        <span class="kwrd">byte</span>[] output = transform.TransformFinalBlock(data, 0, data.Length);

        <span class="kwrd">return</span> Convert.ToBase64String(output);
    }
}

<span class="kwrd">private</span> <span class="kwrd">static</span> <span class="kwrd">string</span> AESDecrypt(<span class="kwrd">string</span> input, <span class="kwrd">byte</span>[] key, <span class="kwrd">byte</span>[] iv)
{
    Aes aesImplementation = Aes.Create();
    aesImplementation.Key = key;
    aesImplementation.IV = iv;

    <span class="kwrd">using</span> (ICryptoTransform transform = aesImplementation.CreateDecryptor())
    {
        <span class="kwrd">byte</span>[] data = Convert.FromBase64String(input);
        <span class="kwrd">byte</span>[] output = transform.TransformFinalBlock(data, 0, data.Length);

        <span class="kwrd">return</span> Encoding.Unicode.GetString(output);
    }
}</pre>]]></description><guid>http://www.nullify.net/ViewArticle.aspx?article=339</guid><pubDate>Fri, 19 Mar 2010 15:05:33 GMT</pubDate><category>CodeBlog</category></item>
<item><link>http://www.nullify.net/Article/338.aspx</link><title>WCF serialisation and deserialisation without sending the object over WCF...</title><description><![CDATA[<p>I needed to send some data over another medium to WCF and wanted to retain my current objects and classes, but also to retain the serialisation format for ease of direct input at the other end of this communications 'tube'. &nbsp;I was actually sending these messages over XMPP after encrypting them, which is what the next post will be about... &nbsp;It's worth noting that serialization using WCF is marginally faster than the more capable XmlSerializer but at the same time this has its own quirks and tends to produce more verbose XML.</p>
<p>So here's methods to serialise and deserialise DataContract marked up objects to XML&nbsp;strings:-</p>
<!-- code formatted by http://manoli.net/csharpformat/ -->
<pre class="csharpcode">
        <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">string</span> ContractObjectToXml&lt;T&gt;(T obj)
        {
            DataContractSerializer dataContractSerializer = <span class="kwrd">new</span> DataContractSerializer(obj.GetType());

            String text;
            <span class="kwrd">using</span> (MemoryStream memoryStream = <span class="kwrd">new</span> MemoryStream())
            {
                dataContractSerializer.WriteObject(memoryStream, obj);
                text = Encoding.UTF8.GetString(memoryStream.ToArray());
            }
            <span class="kwrd">return</span> text;
        }

        <span class="kwrd">public</span> <span class="kwrd">static</span> T XmlToContractObject&lt;T&gt;(<span class="kwrd">string</span> data)
        {
            DataContractSerializer dataContractSerializer = <span class="kwrd">new</span> DataContractSerializer(<span class="kwrd">typeof</span>(T));

            <span class="kwrd">byte</span>[] binary = Encoding.UTF8.GetBytes(data);
            <span class="kwrd">using</span> (MemoryStream memoryStream = <span class="kwrd">new</span> MemoryStream(binary))
            {
                <span class="kwrd">object</span> o = dataContractSerializer.ReadObject(memoryStream);
                <span class="kwrd">return</span> (T)o;
            }
        }<br /><br type="_moz" /></pre>
<p>(Google thinks I&nbsp;should spell Serialise incorrectly as Serialize so I'm very conflicted about which I&nbsp;should use in this article as anyone searching would probably look for the American spelling...!)</p>]]></description><guid>http://www.nullify.net/ViewArticle.aspx?article=338</guid><pubDate>Fri, 19 Mar 2010 15:03:37 GMT</pubDate><category>CodeBlog</category></item>
<item><link>http://www.nullify.net/Article/336.aspx</link><title>SQL Compact Edition with multiple threads</title><description><![CDATA[<p>I was experiencing problems with SQL compact edition (complete .NET framework freezes) and found that not only is it not thread safe (fairly obvious) but it's doesn't like to even have multiple instances of itself on the same thread - it deadlocks a lot if it does. &nbsp;I also found that if it runs out of instances then it unloads from memory and has to be reloaded again afterwards, so avoiding that can result in a many tens of thousands of percent performance improvement.</p>
<p>So here's a small class I wrote to track the connection per thread based on someone elses on the Internet (they were just creating one per thread, in this one it is creating one per thread and maintaining the lifespan of that connection to close it when the thread it was for has been closed). &nbsp;I'll probably add per-connection-string pooling but figured that the important thing is the concept of the solution to the COM deadlocks and total freezing of the framework in this.</p>
<!-- code formatted by http://manoli.net/csharpformat/ -->
<pre class="csharpcode"><span class="rem">/// &lt;summary&gt;</span>
<span class="rem">/// Manages open connections on a per-thread basis</span>
<span class="rem">/// &lt;/summary&gt;</span>
<span class="kwrd">public</span> <span class="kwrd">abstract</span> <span class="kwrd">class</span> SqlCeConnectionPool
{
    <span class="kwrd">private</span> <span class="kwrd">static</span> Dictionary&lt;<span class="kwrd">int</span>, IDbConnection&gt; threadConnectionMap = <span class="kwrd">new</span> Dictionary&lt;<span class="kwrd">int</span>, IDbConnection&gt;();

    <span class="kwrd">private</span> <span class="kwrd">static</span> Dictionary&lt;<span class="kwrd">int</span>, Thread&gt; threadMap = <span class="kwrd">new</span> Dictionary&lt;<span class="kwrd">int</span>, Thread&gt;();

    <span class="rem">/// &lt;summary&gt;</span>
    <span class="rem">/// The connection map</span>
    <span class="rem">/// &lt;/summary&gt;</span>
    <span class="kwrd">public</span> <span class="kwrd">static</span> Dictionary&lt;<span class="kwrd">int</span>, IDbConnection&gt; ThreadConnectionMap
    {
        get { <span class="kwrd">return</span> SqlCeConnectionPool.threadConnectionMap; }
    }

    <span class="rem">/// &lt;summary&gt;</span>
    <span class="rem">/// Gets the connection string.</span>
    <span class="rem">/// &lt;/summary&gt;</span>
    <span class="rem">/// &lt;value&gt;The connection string.&lt;/value&gt;</span>
    <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">string</span> ConnectionString
    {
        get;
        set;
    }

    <span class="rem">/// &lt;summary&gt;</span>
    <span class="rem">/// Gets a connection for this thread, maintains one open one of each.</span>
    <span class="rem">/// &lt;/summary&gt;</span>
    <span class="rem">/// &lt;remarks&gt;Don't do this with anything but SQL compact edition or you'll run out of connections - compact edition is not</span>
    <span class="rem">/// connection pooling friendly and unloads itself too often otherwise so that is why this class exists&lt;/remarks&gt; </span>
    <span class="rem">   &nbsp;/// &lt;returns&gt;An open connection&lt;/returns&gt;</span>
    <span class="kwrd">public</span> <span class="kwrd">static</span> IDbConnection Connection
    {
        get
        {
            <span class="kwrd">lock</span> (threadConnectionMap)
            {
                <span class="rem">//do some quick maintenance on existing connections (closing those that have no thread)</span>
                List&lt;<span class="kwrd">int</span>&gt; removeItems = <span class="kwrd">new</span> List&lt;<span class="kwrd">int</span>&gt;();
                <span class="kwrd">foreach</span> (var kvp <span class="kwrd">in</span> threadConnectionMap)
                {
                    <span class="kwrd">if</span> (threadMap.ContainsKey(kvp.Key))
                    {
                        <span class="kwrd">if</span> (!threadMap[kvp.Key].IsAlive)
                        {
                            <span class="rem">//close the connection</span>
                            kvp.Value.Close();
                            removeItems.Add(kvp.Key);
                        }
                    }
                    <span class="kwrd">else</span>
                    {
                        kvp.Value.Close();
                        removeItems.Add(kvp.Key);
                    }
                }
                <span class="kwrd">foreach</span> (<span class="kwrd">int</span> i <span class="kwrd">in</span> removeItems)
                {
                    threadMap.Remove(i);
                    threadConnectionMap.Remove(i);
                }

                <span class="rem">//now issue the appropriate connection for our current thread</span>
                <span class="kwrd">int</span> threadId = Thread.CurrentThread.ManagedThreadId;

                IDbConnection connection = <span class="kwrd">null</span>;
                <span class="kwrd">if</span> (threadConnectionMap.ContainsKey(threadId))
                {
                    connection = threadConnectionMap[threadId];
                }
                <span class="kwrd">else</span>
                {
                    connection = <span class="kwrd">new</span> SqlCeConnection(ConnectionString);
                    connection.Open();
                    threadConnectionMap.Add(threadId, connection);
                    threadMap.Add(threadId, Thread.CurrentThread);
                }

                <span class="kwrd">return</span> connection;
            }
        }
    }
}</pre>]]></description><guid>http://www.nullify.net/ViewArticle.aspx?article=336</guid><pubDate>Wed, 03 Mar 2010 09:26:03 GMT</pubDate><category>CodeBlog</category></item>
<item><link>http://www.nullify.net/Article/335.aspx</link><title>Zorganiser almost ready for beta test...</title><description><![CDATA[<p>My integration/automation system 'Zorganiser' is almost ready for a closed beta test (albeit with a limited set of features but enough to be useful!). &nbsp;It'll let you control automated tasks on any number of computers, so you can do things like tell 50 webservers to install a particular application, run a particular Windows Workflow or to just shift data around between your installed agents - for example to take data from one business application and deliver it to another in a slightly tweaked format regardless of where the two applications are. &nbsp;You can also configure one event to trigger another - so the workflow could poll something and then trigger an event across all your workstations or servers.</p>
<p>I already have a few people who are interested in testing it, but if you're also interested please e-mail me at <a href="mailto:simon@zorg.co.uk?subject=Zorganiser%20Beta%20Tester">my work address</a>!</p>]]></description><guid>http://www.nullify.net/ViewArticle.aspx?article=335</guid><pubDate>Tue, 02 Mar 2010 17:04:35 GMT</pubDate><category>Blog</category></item>
<item><link>http://www.nullify.net/Article/334.aspx</link><title>Dedicated server hosting - almost a year with 1and1...</title><description><![CDATA[<p>Due to a variety of my circumstances changing and the way the exchange rate was fluctuating I changed from having all-US servers to using <a href="http://order.1and1.co.uk/xml/order/Server?affiliate_id=189976">rented dedicated servers from 1and1 internet</a> - billed in UK pounds. Right now it's particularly appropriate with both the Euro and the Pound hugely devalued against the US dollar.</p>
<p>I didn't expect much - the plan I picked was&nbsp;half literally half the price I was paying before but I got a higher specification server, unlimited bandwidth with no concern about overages anymore or anything of the sort and a significant improvement on the latency to my Texas based servers (which is understandable given the speed of light, but the network is also less congested and I can regularly get 85Mbps and less than 20ms to the UK when testing it).</p>
<p>So I've been with them almost a year now and the availability has far exceeded that of my previous datacenter - I've had about 2 minutes downtime the entire time, so I thought I would mention it as it's relevant to a project I'm working on at the moment...</p>
<p>Fortunately that does mean I've not managed to see if their support is any good, but the service certainly is.</p>
<p>Be warned that their network setup is not really conducive to VMWare or virtualisation in general - they use 1 ip sized subnets and gateways that you can always access even though they aren't in your subnet through some TCP/IP stack tweaks (I think it's &quot;All subnets are local&quot; combined with the switches having some custom configuration, but I've not yet figured it out and it's the first time I've seen something similar done). &nbsp;Their network does have a nice Cisco switch based firewall for every custom because of the strange setup though, and it lets you have public IP's from different subnets so it's got its advantages (no renumbering!).</p>]]></description><guid>http://www.nullify.net/ViewArticle.aspx?article=334</guid><pubDate>Tue, 02 Mar 2010 16:58:54 GMT</pubDate><category>Blog</category></item>
<item><link>http://www.nullify.net/Article/333.aspx</link><title>File dialog filter string format</title><description><![CDATA[<p>Just a very quick reminder to myself that the file dialogs filter string (OpenFileDialog.Filter and SaveFileDialog.Filter) format it is &quot;Description|*.ext;AnotherDescription|*.txt&quot; etc.</p>
<p>I always seem to get this around the wrong way for some reason.</p>]]></description><guid>http://www.nullify.net/ViewArticle.aspx?article=333</guid><pubDate>Fri, 19 Feb 2010 13:00:09 GMT</pubDate><category>Blog</category></item>
<item><link>http://www.nullify.net/Article/332.aspx</link><title>MethodAccessException: System.Runtime.CompilerServices.StrongBox`1..ctor(System.__Canon)</title><description><![CDATA[<p>I'm trying to use a Linq datacontext from a partial trust environment (well, after Assert'ing my way back up) and I'm getting the spectacularly verbose error:-</p>
<p>System.Runtime.CompilerServices.StrongBox`1..ctor(System.__Canon)</p>
<p>Which looks like it's lacking an actual human-readable error but at least it's distinct!</p>
<p>In my case rather than asserting the individually needed parts I had to assert full-trust from my intermediary class (the one that is signed by a strongname to allow it to always be full-trust in my custom AppDomain - see <a href="http://www.nullify.net/Article/315.aspx">my article on creating a restricted AppDomain</a>&nbsp;- see one of the comments in the code example) between the untrusted plugins (things inside the appdomain with the restrictions applied can call into this and anything the intermediary class does is unrestricted but even things it calls get the inherited permissions from the caller) and the Linq data context user:-</p>
<p><span class="Apple-tab-span" style="white-space: pre; ">			</span>PermissionSet set = new PermissionSet(PermissionState.Unrestricted); //fulltrust especially for Linq :(<br />
<span class="Apple-tab-span" style="white-space: pre; ">			</span>//removed my carefully crafted assert set for Linq/SQLCE in exchange for the above being unrestricted:-<br />
<span class="Apple-tab-span" style="white-space:pre">			</span>//set.AddPermission(new FileIOPermission(PermissionState.Unrestricted));<br />
<span class="Apple-tab-span" style="white-space:pre">			</span>//set.AddPermission(new AspNetHostingPermission(AspNetHostingPermissionLevel.Unrestricted));<br />
<span class="Apple-tab-span" style="white-space:pre">			</span>set.Assert();</p>
<p><span class="Apple-tab-span" style="white-space: pre; ">			</span>//Make the call to whatever uses the linq datacontext</p>
<div>Which was a little annoying, but worth knowing. &nbsp;(The assertion will revert when the stack pops from the method this is in, however do be aware not to allow anything under this method to allow the plugin to execute a delegate or something).</div>
<div>&nbsp;</div>
<div>I did find a possible workaround of moving a member variable I was accessing into a local variable in the Linq query, but this didn't work in my case where asserting this did.</div>
<div>&nbsp;</div>]]></description><guid>http://www.nullify.net/ViewArticle.aspx?article=332</guid><pubDate>Thu, 18 Feb 2010 10:00:50 GMT</pubDate><category>Blog</category></item>
<item><link>http://www.nullify.net/Article/331.aspx</link><title>CSV to Datatable parser</title><description><![CDATA[<p>I'm sure there's an easier way to do this, but I've been maintaining my own class to do the job for a while now and keep needing to find it in my code library every time I use it - I figure it might be useful to others:-&nbsp;</p>
<!-- code formatted by http://manoli.net/csharpformat/ -->
<pre class="csharpcode">
<span class="kwrd">using</span> System;
<span class="kwrd">using</span> System.Collections.Generic;
<span class="kwrd">using</span> System.Text;
<span class="kwrd">using</span> System.Data;

<span class="kwrd">namespace</span> CSVParser
{
    <span class="rem">/// &lt;summary&gt;</span>
    <span class="rem">/// Simon's stock CSV parser class</span>
    <span class="rem">/// &lt;/summary&gt;</span>
    <span class="kwrd">public</span> <span class="kwrd">class</span> CSVParser
    {
        <span class="rem">/// &lt;summary&gt;</span>
        <span class="rem">/// Takes the CSV files contents and creates a data table from it</span>
        <span class="rem">/// &lt;/summary&gt;</span>
        <span class="rem">/// &lt;param name="csvFileContents"&gt;The entire contents of a CSV file split by line (rather than the filename)&lt;/param&gt;</span>
        <span class="rem">/// &lt;param name="validateLayout"&gt;Validate the file to check conformance of the layout with expected standards&lt;/param&gt;</span>
        <span class="rem">/// &lt;param name="topRowIsHeader"&gt;The top row is the header row, take it as the column names rather than data&lt;/param&gt;</span>
        <span class="rem">/// &lt;returns&gt;A datatable&lt;/returns&gt;</span>
        <span class="kwrd">public</span> <span class="kwrd">static</span> DataTable DataTableFromCSV(<span class="kwrd">string</span>[] csvFileContents, <span class="kwrd">bool</span> validateLayout, <span class="kwrd">bool</span> topRowIsHeader)
        {
            DataTable outputDataTable = <span class="kwrd">new</span> DataTable();
            List&lt;<span class="kwrd">string</span>[]&gt; csvFileRows = <span class="kwrd">new</span> List&lt;<span class="kwrd">string</span>[]&gt;();
            List&lt;<span class="kwrd">string</span>&gt; columns = <span class="kwrd">new</span> List&lt;<span class="kwrd">string</span>&gt;();

            <span class="preproc">#region</span> Pre-parse the file
            <span class="kwrd">int</span> columnCount = 0;

            <span class="kwrd">bool</span> gotHeaders = <span class="kwrd">false</span>;
            <span class="kwrd">int</span> rowNumber = 0;

            <span class="kwrd">foreach</span> (<span class="kwrd">string</span> line <span class="kwrd">in</span> csvFileContents)
            {
                <span class="kwrd">string</span>[] parts = ExtractCSVElements(line);

                <span class="rem">//initial set of header names but only if the top row is header option is set</span>
                <span class="kwrd">if</span> (!gotHeaders &amp;&amp; topRowIsHeader)
                {
                    columns.AddRange(parts);
                    columnCount = parts.Length;
                    gotHeaders = <span class="kwrd">true</span>;
                }
                <span class="kwrd">else</span>
                {
                    <span class="kwrd">if</span> (parts.Length &gt; 0)
                    {
                        csvFileRows.Add(parts);
                    }
                }

                <span class="kwrd">if</span> (parts.Length &gt; columnCount)
                {
                    <span class="rem">//if set to validate the layout and that the first row contains the headers then we know any extra columns are wrong</span>
                    <span class="kwrd">if</span> (validateLayout &amp;&amp; gotHeaders)
                    {
                        <span class="kwrd">throw</span> <span class="kwrd">new</span> Exception(<span class="str">"Row has extra data columns: "</span> + rowNumber.ToString());
                    }

                    <span class="rem">//new column detected mid-data-set!</span>
                    <span class="kwrd">for</span> (<span class="kwrd">int</span> i = columnCount; i &lt; parts.Length; i++)
                    {
                        columns.Add(<span class="str">"Column "</span> + i.ToString());
                    }

                    columnCount = parts.Length;
                }

                <span class="rem">//we always ignore zero length rows as the last line can be empty</span>
                <span class="kwrd">if</span> (parts.Length &lt; columnCount &amp;&amp; parts.Length != 0)
                {
                    <span class="kwrd">if</span> (validateLayout)
                    {
                        <span class="kwrd">throw</span> <span class="kwrd">new</span> Exception(<span class="str">"Row has missing data columns: "</span> + rowNumber.ToString());
                    }
                }


                rowNumber++;
            }

            <span class="preproc">#endregion</span>

            <span class="preproc">#region</span> Build the data tables layout and data

            <span class="rem">//columns</span>
            <span class="kwrd">foreach</span> (<span class="kwrd">string</span> column <span class="kwrd">in</span> columns)
            {
                outputDataTable.Columns.Add(column);
            }

            <span class="rem">//rows</span>
            <span class="kwrd">foreach</span> (<span class="kwrd">string</span>[] row <span class="kwrd">in</span> csvFileRows)
            {
                outputDataTable.Rows.Add(row);
            }
            <span class="preproc">#endregion</span>

            <span class="kwrd">return</span> outputDataTable;
        }

        <span class="rem">/// &lt;summary&gt;</span>
        <span class="rem">/// Extract the elements of a line from a CSV file with support for quotes</span>
        <span class="rem">/// &lt;/summary&gt;</span>
        <span class="rem">/// &lt;param name="line"&gt;The data to parse&lt;/param&gt;</span>
        <span class="kwrd">private</span> <span class="kwrd">static</span> <span class="kwrd">string</span>[] ExtractCSVElements(<span class="kwrd">string</span> line)
        {
            List&lt;<span class="kwrd">string</span>&gt; elements = <span class="kwrd">new</span> List&lt;<span class="kwrd">string</span>&gt;();

            <span class="rem">//do the initial split, based on commas</span>
            <span class="kwrd">string</span>[] firstParts = line.Split(<span class="str">','</span>);

            <span class="rem">//reparse it</span>
            StringBuilder temporaryPart = <span class="kwrd">new</span> StringBuilder(<span class="str">""</span>);
            <span class="kwrd">bool</span> inside = <span class="kwrd">false</span>;
            <span class="kwrd">foreach</span> (<span class="kwrd">string</span> part <span class="kwrd">in</span> firstParts)
            {
                <span class="rem">//are we inside a quoted part, or did we just find a quote?</span>
                <span class="kwrd">if</span> (!inside &amp;&amp; !part.Contains(<span class="str">"\""</span>))
                {
                    elements.Add(part);
                }
                <span class="kwrd">else</span>
                {
                    <span class="kwrd">if</span> (inside)
                    {
                        <span class="rem">//we are still inside a quote...</span>
                        temporaryPart.Append(<span class="str">","</span> + part);

                        <span class="kwrd">if</span> (part.Contains(<span class="str">"\""</span>))
                        {
                            <span class="rem">//then we are also at the end!</span>
                            elements.Add(temporaryPart.Replace(<span class="str">"\""</span>, <span class="str">""</span>).ToString()); <span class="rem">//add the part minus its quotes to the array</span>
                            <span class="rem">//all done!</span>
                            inside = <span class="kwrd">false</span>;
                        }
                    }
                    <span class="kwrd">else</span>
                    {
                        <span class="rem">//else we just found a quote!</span>
                        inside = <span class="kwrd">true</span>;
                        temporaryPart = <span class="kwrd">new</span> StringBuilder(part);
                    }
                }
            }

            <span class="kwrd">return</span> elements.ToArray();
        }
    }
}
</pre>]]></description><guid>http://www.nullify.net/ViewArticle.aspx?article=331</guid><pubDate>Tue, 09 Feb 2010 10:41:44 GMT</pubDate><category>CodeBlog</category></item>
<item><link>http://www.nullify.net/Article/330.aspx</link><title>Come work for me in Basingstoke!</title><description><![CDATA[<p><strong>Edit: </strong>Due to some investment problems I can no longer employ anyone, sorry :(</p>
<p>My new company - <a href="http://www.zorg.co.uk/">Zorg Solutions</a> Ltd is now looking for at least one software developer to fill out a team, preferably you'll be an enthusiastic multi-skilled person but we're flexible and will distribute work based on what you can do.</p>
<p>Do you have a background as a C# software developer that has done either Winforms or ASP.NET in the past? &nbsp;You will need to have at least a couple of years of experience developing with the .NET framework but I will be willing to take hobby or open source work as experience if it's provable that you did it. &nbsp;This could be a great opportunity to jump into the software development world for someone - there's no requirement for a degree or other certification. &nbsp;Any experience of Windows Azure, Silverlight or WPF would be an advantage.</p>
<p>Or do you have any experience (hobby or professionally) in electronics, firmware, C/C++? &nbsp;We have contracts for some hardware development and the opportunity to work with and program the hardware we'll build - probably Atmel microprocessors or PIC microcontrollers, but even some Linux on ARM9 based chips.</p>
<p>If you answered yes to either of these questions and would be willing to commute to Basingstoke then I am interested in hearing from you! &nbsp;Any experience of the following would make you stand out but is not essential:-</p>
<ul>
    <li>SQL experience (preferably SQL server's T-SQL dialect)</li>
    <li>Windows Workflow Foundation</li>
    <li>Biztalk</li>
    <li>Silverlight</li>
    <li>Mono</li>
</ul>
<p>Salary will be negotiable (but as an idea we would be willing to pay either side of &pound;35k) and based on experience. &nbsp;Working hours and benefits will be fully negotiable - we can be very flexible and understand if you have personal commitments. &nbsp;Training and an MSDN subscription will also be provided!</p>
<p><strong>If you apply (just send me your CV) or contact me now to discuss in more detail then you will be ahead of the conventional advertisements for these roles - we'll be putting them out conditional on responses to this posting and a few other enquiries - so you have nothing to lose! &nbsp;</strong>No recruiters yet please!</p>
<p><strong>Edit:</strong> Due to some investment problems I can no longer employ anyone, sorry :(</p>]]></description><guid>http://www.nullify.net/ViewArticle.aspx?article=330</guid><pubDate>Fri, 29 Jan 2010 11:05:18 GMT</pubDate><category>Work</category></item>
<item><link>http://www.nullify.net/Article/329.aspx</link><title>My new company!</title><description><![CDATA[<p>I finally set up my new companies website (thanks to the snow here totally blocking the ability to get to the office!) - it's lacking content at the moment but the design is in place and I think it looks excellent and embodies the company quite well.</p>
<p><a href="http://www.zorg.co.uk/">http://www.zorg.co.uk/</a>&nbsp;</p>
<p>What do you think? &nbsp;Anything I should change?</p>
<p>I went through the images on <a href="http://en.fotolia.com/partner/201462992">fotolia</a> for quite a while before settling on that one for the front page. &nbsp;We're using SIP phones for the telephony and I am hoping to have my automation system at the alpha stage within a few months so I'll be looking for people to test it before we go public with a beta (Google style!). &nbsp;I already have a prototype Workflow designer but at the moment there's not enough to use in production yet.</p>
<p>I am hoping that this product will help in a lot of situations, everything from automated deployment of complex systems, testing your software without needing to sit there and do it manually to automating business processes (or virtual machine operations like creation, start-up and shut down!).</p>
<p>All as a simple and easy on-line service you can optionally subscribe to (there will be a free offering to get to try it!).</p>]]></description><guid>http://www.nullify.net/ViewArticle.aspx?article=329</guid><pubDate>Thu, 07 Jan 2010 14:16:26 GMT</pubDate><category>Blog</category></item>
<item><link>http://www.nullify.net/Article/328.aspx</link><title>Changes are Inevitable</title><description><![CDATA[<p>Today is my last day at my current employer, a company called <a href="http://www.easytrace.co.uk/">EasyTrace</a> which has gone from a few people in a cow-shed just outside Basingstoke selling a DOS product to a market leader selling modern software I can feel proud to have written the vast majority of. &nbsp;Over a year ago now they were also&nbsp;bought by <a href="http://www.rm.com">RM PLC</a> and the RM people I have worked with have been enthusiastic and fun to interact with.</p>
<p>I have been there for over five and a half years and would like to wish all the present and ex-employees, suppliers and RM people who have helped out&nbsp;over the years&nbsp;the very best for the future.</p>
<p>Everyone has been amazing and put in a lot of long hours and effort to make a happy client base of schools, colleges, hospitals and big companies. &nbsp;It has been a wild ride, and it has been a pleasure working with everyone.</p>
<p>I will be providing some help to the company after the relocation to the new offices in Abingdon this Christmas but I am mostly taking this opportunity to set up a new business and have some more relaxing fun for a while exploring an idea I have. &nbsp;It also hopefully means I'll have the opportunity to get to some conferences and write more technical blog posts - I'm fully aware I haven't made any decent ones since May as I have been spending a lot of time on work projects even out of hours recently.</p>
<p>So, here's to the future!</p>]]></description><guid>http://www.nullify.net/ViewArticle.aspx?article=328</guid><pubDate>Fri, 18 Dec 2009 08:19:26 GMT</pubDate><category>Blog</category></item>
<item><link>http://www.nullify.net/Article/327.aspx</link><title>Web form usability study</title><description><![CDATA[<p>This is an excellent usability study comparing the major webmail providers sign-up forms out there, it almost certainly has an applicability to all software development to a degree:-</p>
<p><a href="http://www.cxpartners.co.uk/thoughts/web_forms_design_guidelines_an_eyetracking_study.htm">http://www.cxpartners.co.uk/thoughts/web_forms_design_guidelines_an_eyetracking_study.htm</a>&nbsp;</p>
<p>So take a look.</p>
<p>It covers things like making name fields appear as one entry so they require less thought to enter, and making mobile phone fields not take an age to decide what bit goes in which box. &nbsp;There were also some good basic layout hints and talk of when to use groupings and when not to!</p>]]></description><guid>http://www.nullify.net/ViewArticle.aspx?article=327</guid><pubDate>Sat, 05 Dec 2009 14:13:30 GMT</pubDate><category>Blog</category></item>
<item><link>http://www.nullify.net/Article/326.aspx</link><title>From one extreme to the other...</title><description><![CDATA[<p>With development getting more and more complex and thousands of lines of code being required to do things that used to take a couple of hundred, it's nice to see a simpler approach to things. &nbsp;Sometimes the complexity has great benefit (like reuse and flexibility), but when learning that isn't necessarily first on your mind...</p>
<p>I just noticed a blog post on the Microsoft Small Basic blog and have since had a quick try... It looks like an awesome educational tool - easy to use and I certainly remember playing with logo and basic when I was younger - this provides both of those combined (along with decent graphics and no overhead).</p>
<p>So the blog is here:-</p>
<p><a href="http://blogs.msdn.com/smallbasic/">http://blogs.msdn.com/smallbasic/</a></p>
<p>And the program itself (which is basically a tiny IDE) is at <a href="http://smallbasic.com/">http://smallbasic.com/</a>&nbsp;- hopefully it will let a new generation of children find programming as fun as it was for me when I was young.</p>
<p><strong>Edit: </strong>Yes this is no match for C# which is a wonderful language and has a vast framework beside it, but this IS able to make a logo turtle move around in three lines on screen...</p>]]></description><guid>http://www.nullify.net/ViewArticle.aspx?article=326</guid><pubDate>Sun, 15 Nov 2009 11:00:20 GMT</pubDate><category>Blog</category></item>
<item><link>http://www.nullify.net/Article/324.aspx</link><title>Wow, another September over!</title><description><![CDATA[<p>For those that don't know me, I&nbsp;work in an education sector linked business - which means that when the schools/colleges and universities&nbsp;go back we always get a vast increase in the amount of calls and number of issues that people raise.</p>
<p>So I've been trying to make up for the development time that was lost doing support by working later here and there.&nbsp; Thankfully&nbsp;September is now&nbsp;over so I&nbsp;can return to having a personal life and possibly doing more research/blogging!</p>
<p>I&nbsp;have several projects I&nbsp;am investigating right now and hopefully one of them is getting closer to being something I&nbsp;can talk about and that will be able to have a bit of an ecosystem sit on top of it.&nbsp; For all those people that were disappointed that Oslo wasn't a platform, this might make up for it, depending on what you were expecting&nbsp;:)</p>]]></description><guid>http://www.nullify.net/ViewArticle.aspx?article=324</guid><pubDate>Sun, 04 Oct 2009 18:33:20 GMT</pubDate><category>Blog</category></item>
<item><link>http://www.nullify.net/Article/323.aspx</link><title>Interesting MSDN WCF links</title><description><![CDATA[<p>I am looking to merge both a P2P WCF service (for LAN&nbsp;support and accessible hops) and a publish-subscribe server to coordinate everything, this is just a post to keep track of the URL's related to doing this as I&nbsp;find them&nbsp;:)</p>
<p>WCF&nbsp;can't use its PeerChannel to connect to both hosts on the internet and local network at the same time, so there'll need to be two&nbsp;PeerChannel's and a Publish-Subscriber service bus connection to work out where everything else is.</p>
<p>Publish-Subscriber:&nbsp;<a href="http://msdn.microsoft.com/en-us/library/ms752254.aspx">http://msdn.microsoft.com/en-us/library/ms752254.aspx</a><br />
PeerResolvers: <a href="http://msdn.microsoft.com/en-us/library/aa702771.aspx">http://msdn.microsoft.com/en-us/library/aa702771.aspx</a><br />
Writing a custom peer resolver (new one needs to handle non-local hosts by requesting from the publish subscribe service): <a href="http://msdn.microsoft.com/en-us/library/ms751466.aspx">http://msdn.microsoft.com/en-us/library/ms751466.aspx</a><br />
Sample WCF&nbsp;P2P chat app: <a href="http://msdn.microsoft.com/en-us/library/ms751502.aspx">http://msdn.microsoft.com/en-us/library/ms751502.aspx</a></p>
<p>(This post will be updated with more links as I&nbsp;find them)</p>
<p><strong>Update:&nbsp;</strong>What I&nbsp;didn't realise when I&nbsp;started looking into this is that the PeerResolver service is not a magic 'find everything on your network' concept, but rather just a WCF service you register and unregister at, so it's basically already the public-subscriber list I&nbsp;was looking at. &nbsp;That leaves the question of if the PeerResolver service does that, how on earth is it supposed to find things on the local network? &nbsp;Well that would be the PNRP service, which happens to not be compatible with loads of OS' and isn't installed by default. &nbsp;Great one there guys!</p>
<p>So instead I'll just make my own UDP transport for the peer resolver, and if anything I'll get to learn how to make a new transport :).</p>]]></description><guid>http://www.nullify.net/ViewArticle.aspx?article=323</guid><pubDate>Tue, 18 Aug 2009 07:00:03 GMT</pubDate><category>Blog</category></item>
<item><link>http://www.nullify.net/Article/322.aspx</link><title>Pictures of a coral reef in a tank?</title><description><![CDATA[<p>So this is a little different to my normal posts that are all 100% technical, but&nbsp;via the Omnima site (company who sell some embedded components) there's a link to a review of one of the products they sell which is used for monitoring stuff, which links to a specialist site on aquariums.</p>
<p>Well, it has some <a href="http://www.advancedaquarist.com/2007/2/aquarium/view">amazing photos&nbsp;of reefs</a>&nbsp;on it which I&nbsp;thought were worth linking to.</p>]]></description><guid>http://www.nullify.net/ViewArticle.aspx?article=322</guid><pubDate>Sun, 16 Aug 2009 19:28:49 GMT</pubDate><category>Blog</category></item>
</channel></rss>