<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Michael E. Chancey Jr. &#187; RegEx</title>
	<atom:link href="http://michael.chanceyjr.com/tag/regex/feed/" rel="self" type="application/rss+xml" />
	<link>http://michael.chanceyjr.com</link>
	<description></description>
	<lastBuildDate>Fri, 20 May 2011 03:43:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Generic License Generation and Validation</title>
		<link>http://michael.chanceyjr.com/useful-code/generic-license-generation-and-validation/</link>
		<comments>http://michael.chanceyjr.com/useful-code/generic-license-generation-and-validation/#comments</comments>
		<pubDate>Fri, 20 May 2011 03:43:18 +0000</pubDate>
		<dc:creator>Michael E. Chancey Jr.</dc:creator>
				<category><![CDATA[Useful Code]]></category>
		<category><![CDATA[Activate]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C-Sharp]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Cryptography]]></category>
		<category><![CDATA[CSharp]]></category>
		<category><![CDATA[Generic]]></category>
		<category><![CDATA[License]]></category>
		<category><![CDATA[License Key]]></category>
		<category><![CDATA[RegEx]]></category>
		<category><![CDATA[Regular Expressions]]></category>
		<category><![CDATA[SHA256]]></category>
		<category><![CDATA[String]]></category>

		<guid isPermaLink="false">http://michael.chanceyjr.com/?p=696</guid>
		<description><![CDATA[This was thrown together to see how quickly I could come up with something simple to allow for offline license validation. Up until now I previously used an online system. As well as this works it requires that the user access the internet in order to validate the key (which opens itself to traffic sniffing [...]]]></description>
			<content:encoded><![CDATA[<p>This was thrown together to see how quickly I could come up with something simple to allow for offline license validation.  Up until now I previously used an online system.  As well as this works it requires that the user access the internet in order to validate the key (which opens itself to traffic sniffing and server simulation in order to hack).  This is by no means a perfect system but it should stump people long enough that they will feel better off just buying a key.</p>
<h3>//Code To Generate A License Key</h3>
<hr />
<pre class="brush: csharp;">
/// &lt;summary&gt;
/// GENERATE A UNIQUE KEY THAT CAN BE REVERESED TO VALIDATE WITHOUT INTERNET CONNECTION
/// &lt;/summary&gt;
/// &lt;param name=&quot;AppID&quot;&gt;APPLIATION GUID THE KEY SHOULD BE GERNEATED FOR&lt;/param&gt;
/// &lt;returns&gt;STRING VALUE REPRESENTING A VALID KEY&lt;/returns&gt;
static string GenerateKey(string AppID)
{
    int curSegment = 1;
    string newLicense = &quot;&quot;;
    string License = new string(Guid.NewGuid().ToString().Replace(&quot;-&quot;, &quot;&quot;).ToUpper().ToCharArray().Take(12).ToArray());

    //GENERATE 4 BYTE SEGMENTS WHICH ARE [HASH,HASH][KEY,KEY]
    //VALUES ARE TAKEN FROM THE HASH AT A MATHIMATICALLY COMPUTED LOCATION [(FIRST CHAR ASCII VALUE + SEGMENT) % 32] TO MAKE IT DEPENDENT ON SEGMENT LOCATION AND VALUE
    //THIS SHOULD MAKE IT HARDER TO &quot;GUESS&quot; A KEY OR BRUTE FORCE ONE OUT OF THE SYSTEM
    foreach (Match tmpMatch in Regex.Matches(License, &quot;[a-zA-Z0-9]{2}&quot;))
        newLicense += (newLicense != &quot;&quot; ? &quot;-&quot; : &quot;&quot;) + BitConverter.ToString(SHA256.Create().ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(tmpMatch.Value + AppID)).Skip((tmpMatch.Value.ToCharArray()[0] + curSegment++) % 32).Take(1).ToArray()).Replace(&quot;-&quot;, &quot;&quot;) + tmpMatch.Value;

    //RETURN THE NEW LICENSE VALUE TO THE CALLING FUNCTION
    return newLicense;
}
</pre>
<h3>//Code To Validate A License Key</h3>
<hr />
<pre class="brush: csharp;">
/// &lt;summary&gt;
/// VALIDATES A KEY THAT HAS BEEN GENERATED FOR THE GIVEN APPLICATION ID
/// &lt;/summary&gt;
/// &lt;param name=&quot;AppID&quot;&gt;APPLICATION THE KEY SHOULD BE VALIDATED AGAINST&lt;/param&gt;
/// &lt;param name=&quot;Key&quot;&gt;UNIQUE KEY TO COMPARE AGAINST&lt;/param&gt;
/// &lt;returns&gt;TRUE OR FALSE REPRESENTING THE VALIDATION OF THE GIVEN KEY AND APPLICATION ID&lt;/returns&gt;
public static bool ValidateKey(string Key)
{
    try
    {
        int curSegment = 1;
        string[] segments = Key.ToUpper().Split(new char[] { '-' });
        string AppID = ((GuidAttribute)Assembly.GetCallingAssembly().GetCustomAttributes(typeof(GuidAttribute), false)[0]).Value;

        foreach (string segment in segments)
        {
            //SPLIT THE SEGMENT INTO HASH VALUE AND KEY VALUE
            string hashedValue = segment.Substring(0, 2);
            string keyValue = segment.Substring(2, 2);

            //COMPUTE THE HASH USING THE REVERSE ALGORITHM FROM CREATING A HASH
            string hash = BitConverter.ToString(SHA256.Create().ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(keyValue + AppID)).Skip((keyValue.ToCharArray()[0] + curSegment++) % 32).Take(1).ToArray());

            //IF THE HASH DOES NOT MATCH WHAT IS BELIEVED SHOULD BE A MATCH THEN DROP OUT BECAUSE THE KEY IS INVALID
            if (hash != hashedValue)
                return false;
        }

        //IF WE MADE IT THIS FAR THEN THE KEY IS VALID
        return true;
    }
    catch
    {
        //IF THERE WAS AN ERROR WHILE ATTEMPTING TO VALIDATE THEN THE KEY MUST BE INVALID
        return false;
    }
}
</pre>



Share


	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=Generic%20License%20Generation%20and%20Validation%20-%20http%3A%2F%2Fmichael.chanceyjr.com%2Fuseful-code%2Fgeneric-license-generation-and-validation%2F" title="Twitter"><img src="http://michael.chanceyjr.com/site/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fmichael.chanceyjr.com%2Fuseful-code%2Fgeneric-license-generation-and-validation%2F&amp;title=Generic%20License%20Generation%20and%20Validation&amp;bodytext=This%20was%20thrown%20together%20to%20see%20how%20quickly%20I%20could%20come%20up%20with%20something%20simple%20to%20allow%20for%20offline%20license%20validation.%20%20Up%20until%20now%20I%20previously%20used%20an%20online%20system.%20%20As%20well%20as%20this%20works%20it%20requires%20that%20the%20user%20access%20the%20internet%20in%20order" title="Digg"><img src="http://michael.chanceyjr.com/site/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fmichael.chanceyjr.com%2Fuseful-code%2Fgeneric-license-generation-and-validation%2F&amp;title=Generic%20License%20Generation%20and%20Validation" title="StumbleUpon"><img src="http://michael.chanceyjr.com/site/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fmichael.chanceyjr.com%2Fuseful-code%2Fgeneric-license-generation-and-validation%2F&amp;title=Generic%20License%20Generation%20and%20Validation&amp;notes=This%20was%20thrown%20together%20to%20see%20how%20quickly%20I%20could%20come%20up%20with%20something%20simple%20to%20allow%20for%20offline%20license%20validation.%20%20Up%20until%20now%20I%20previously%20used%20an%20online%20system.%20%20As%20well%20as%20this%20works%20it%20requires%20that%20the%20user%20access%20the%20internet%20in%20order" title="del.icio.us"><img src="http://michael.chanceyjr.com/site/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://michael.chanceyjr.com/useful-code/generic-license-generation-and-validation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Testing Password Complexity</title>
		<link>http://michael.chanceyjr.com/useful-code/testing-password-complexity/</link>
		<comments>http://michael.chanceyjr.com/useful-code/testing-password-complexity/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 05:15:17 +0000</pubDate>
		<dc:creator>Michael E. Chancey Jr.</dc:creator>
				<category><![CDATA[Useful Code]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C-Sharp]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[CSharp]]></category>
		<category><![CDATA[Password Complexity]]></category>
		<category><![CDATA[RegEx]]></category>
		<category><![CDATA[Regular Expressions]]></category>

		<guid isPermaLink="false">http://michael.chanceyjr.com/?p=534</guid>
		<description><![CDATA[Password complexity is a huge deal when a company is applying for its compliance so below is a simple function to test for password complexity based on a rule set passed in. The following code allows for any of the following rules to be applied to a password. You can elect to require any or [...]]]></description>
			<content:encoded><![CDATA[<p>Password complexity is a huge deal when a company is applying for its compliance so below is a simple function to test for password complexity based on a rule set passed in.  The following code allows for any of the following rules to be applied to a password.  You can elect to require any or all of the following 1 upper case character, 1 lower case character, 1 numeric character and 1 special character.</p>
<h3>//Function</h3>
<hr/>
<pre class="brush: csharp;">
        /// &lt;summary&gt;
        /// FUNCTION FOR TESTING PASSWORD COMPLEXITY
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;s&quot;&gt;INCOMING PASSWORD TO TEST&lt;/param&gt;
        /// &lt;param name=&quot;minLength&quot;&gt;MIN LENGTH OF PASSWORD&lt;/param&gt;
        /// &lt;param name=&quot;maxLength&quot;&gt;MAX LENGTH OF PASSWORD&lt;/param&gt;
        /// &lt;param name=&quot;rule&quot;&gt;COMPARISON FLAGS CAN BE ANY COMBINATION OF THE FOLLOWING - UPPER, LOWER, NUMERIC, SPECIAL&lt;/param&gt;
        /// &lt;returns&gt;TRUE OR FALSE IF THE PASSWORD PASSES OR NOT&lt;/returns&gt;
        static bool ValidatePasswordComplexity(string s, int minLength, int maxLength, PasswordRules rule)
        {
            //CONSTANTS FOR COMPARISON GROUPS
            const string UPPER = &quot;(?=.*[A-Z])&quot;;
            const string LOWER = &quot;(?=.*[a-z])&quot;;
            const string NUMERIC = &quot;(?=.*[0-9])&quot;;
            const string SPECIAL = &quot;(?=.*[!@#$%^&amp;*()&lt;&gt;])&quot;;

            //CREATE A NEW VAR TO HOLD THE COMPARE STRING WHILE ITS ASSEMBLED
            StringBuilder tmpCompare = new StringBuilder(&quot;^&quot;);

            //CHECK IF UPPER IS A REQUIREMENT
            if ((rule &amp; PasswordRules.Upper) == PasswordRules.Upper)
                tmpCompare.Append(UPPER);

            //CHECK IF LOWER IS A REQUIREMENT
            if ((rule &amp; PasswordRules.Lower) == PasswordRules.Lower)
                tmpCompare.Append(LOWER);

            //CHECK IF NUMERIC IS A REQUIREMENT
            if ((rule &amp; PasswordRules.Numeric) == PasswordRules.Numeric)
                tmpCompare.Append(NUMERIC);

            //CHECK IF SPECIAL IS A REQUIREMENT
            if ((rule &amp; PasswordRules.Special) == PasswordRules.Special)
                tmpCompare.Append(SPECIAL);

            //APPEND THE LENGTH REQUIREMENTS
            tmpCompare.Append(string.Format(&quot;.{{{0},{1}}}&quot;, minLength, maxLength));

            //RETURN THE RESULT OF THE COMPARISON
            return Regex.Match(s, tmpCompare.ToString()).Success;
        }

        /// &lt;summary&gt;
        /// ERNUMERATIONS FOR PASSWORD COMPLEXITY TESTING
        /// &lt;/summary&gt;
        [Flags]
        public enum PasswordRules
        {
            Upper = 1,
            Lower = 2,
            Numeric = 4,
            Special = 8,
        }
</pre>
<h3>//Usage</h3>
<hr/>
<pre class="brush: csharp;">
        /// &lt;summary&gt;
        /// MAIN APPLICATION ENTRY POINT
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;args&quot;&gt;&lt;/param&gt;
        static void Main(string[] args)
        {
            //SOMETHING TO COMPARE
            string pwd = &quot;Password1&quot;;

            //TWO DIFFERENT SAMPLES TO SHOW THE PASSWORD MEETS THE REQUIREMENTS IF YOU ARE NOT LOOKING FOR A SPECIAL CHARACTER
            //BUT FAILS ONCE THE SPECIAL REQUIREMENT IS ADDED TO THE PARAMETER
            Console.WriteLine(&quot;{0}: {1}&quot;, pwd, ValidatePasswordComplexity(pwd, 6, 15, PasswordRules.Upper | PasswordRules.Lower | PasswordRules.Numeric));
            Console.WriteLine(&quot;{0}: {1}&quot;, pwd, ValidatePasswordComplexity(pwd, 6, 15, PasswordRules.Upper | PasswordRules.Lower | PasswordRules.Numeric | PasswordRules.Special));

            //WAIT FOR USER INPUT
            Console.ReadLine();
        }
</pre>



Share


	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=Testing%20Password%20Complexity%20-%20http%3A%2F%2Fmichael.chanceyjr.com%2Fuseful-code%2Ftesting-password-complexity%2F" title="Twitter"><img src="http://michael.chanceyjr.com/site/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fmichael.chanceyjr.com%2Fuseful-code%2Ftesting-password-complexity%2F&amp;title=Testing%20Password%20Complexity&amp;bodytext=Password%20complexity%20is%20a%20huge%20deal%20when%20a%20company%20is%20applying%20for%20its%20compliance%20so%20below%20is%20a%20simple%20function%20to%20test%20for%20password%20complexity%20based%20on%20a%20rule%20set%20passed%20in.%20%20The%20following%20code%20allows%20for%20any%20of%20the%20following%20rules%20to%20be%20applied%20to%20a" title="Digg"><img src="http://michael.chanceyjr.com/site/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fmichael.chanceyjr.com%2Fuseful-code%2Ftesting-password-complexity%2F&amp;title=Testing%20Password%20Complexity" title="StumbleUpon"><img src="http://michael.chanceyjr.com/site/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fmichael.chanceyjr.com%2Fuseful-code%2Ftesting-password-complexity%2F&amp;title=Testing%20Password%20Complexity&amp;notes=Password%20complexity%20is%20a%20huge%20deal%20when%20a%20company%20is%20applying%20for%20its%20compliance%20so%20below%20is%20a%20simple%20function%20to%20test%20for%20password%20complexity%20based%20on%20a%20rule%20set%20passed%20in.%20%20The%20following%20code%20allows%20for%20any%20of%20the%20following%20rules%20to%20be%20applied%20to%20a" title="del.icio.us"><img src="http://michael.chanceyjr.com/site/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://michael.chanceyjr.com/useful-code/testing-password-complexity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

