<?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"
	>

<channel>
	<title>unitstep.net &#187; PHP</title>
	<atom:link href="http://unitstep.net/blog/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://unitstep.net</link>
	<description>the home of peter chng</description>
	<pubDate>Sun, 05 Oct 2008 17:42:16 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.2</generator>
	<language>en</language>
			<item>
		<title>Decoding Google Maps Encoded Polylines using PHP</title>
		<link>http://unitstep.net/blog/2008/08/02/decoding-google-maps-encoded-polylines-using-php/</link>
		<comments>http://unitstep.net/blog/2008/08/02/decoding-google-maps-encoded-polylines-using-php/#comments</comments>
		<pubDate>Sat, 02 Aug 2008 20:32:39 +0000</pubDate>
		<dc:creator>Peter Chng</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[development]]></category>

		<category><![CDATA[google]]></category>

		<category><![CDATA[maps]]></category>

		<category><![CDATA[programming]]></category>

		<category><![CDATA[polylines]]></category>

		<guid isPermaLink="false">http://unitstep.net/?p=361</guid>
		<description><![CDATA[I&#8217;ve talked about the Google Maps encoded polyline format before. While there&#8217;s some nice utilities for encoding polylines that take the work out of implementing it yourself, I couldn&#8217;t find many polyline decoders.  
This made it somewhat tedious to decode them, as the only way to get the original list of points was to [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve talked about the <a href="http://code.google.com/apis/maps/documentation/polylinealgorithm.html">Google Maps encoded polyline format</a> <a href="/blog/2008/05/11/playing-with-google-maps-and-encoded-polylines/">before</a>. While there&#8217;s some nice utilities for <a href="http://facstaff.unca.edu/mcmcclur/GoogleMaps/EncodePolyline/">encoding polylines</a> that take the work out of implementing it yourself, I couldn&#8217;t find many polyline <em>decoders</em>.  </p>
<p>This made it somewhat tedious to decode them, as the only way to get the original list of points was to create a <a href="http://code.google.com/apis/maps/documentation/overlays.html#Encoded_Polylines"><code>GPolyline</code></a> and then pull out the points from that object.  This is not ideal since the work must always be done on the client side with JavaScript and using Google Maps.</p>
<p>To solve this, I quickly ported the algorithm over to <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> from the JavaScript source.  Please feel free to download/modify/use this script.</p>
<div class="download">
<a class="icon" href="http://unitstep.net/wordpress/wp-content/uploads/2008/08/decodepolylinetoarray.zip">Google Maps Polyline Decoder in <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym></a>
</div>
<p>Since the encoded polyline format offers numerous benefits (and because I had data already stored in this format) I did not want to move away from it. At the same time, I needed access to the points for working with things like <a href="http://code.google.com/apis/maps/documentation/staticmaps/">Google Static Maps</a>, which curiously does not accept the encoded polyline format for displaying paths. (Probably to reduce resource usage on their end, since decoding takes CPU time)</p>
<p>Thankfully the polyline decoding algorithm was <a href="http://facstaff.unca.edu/mcmcclur/GoogleMaps/EncodePolyline/decode.js">already available</a> at Mark McClure&#8217;s site.  I spent a few minutes understanding the process and porting it over to <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym>.  The source is attached above and is released under an MIT license.  Basically, the only change I had to make was to use some <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> functions to convert characters to their ASCII code, since <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> doesn&#8217;t have a <code>charCodeAt()</code> function.</p>
<p>Please let me know if you find it to be useful.</p>
<hr/>Copyright &copy; 2008 <strong><a href="http://unitstep.net">unitstep.net</a></strong>. This Feed is for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact <strong><a href="mailto:webmaster@unitstep.net">webmaster@unitstep.net</a></strong> for more information.<br/><span style="float: right;font-size: 7pt"><a href="http://blog.taragana.com/index.php/archive/wordpress-plugins-provided-by-taraganacom/">Plugin</a> by <a href="http://www.taragana.com/">Taragana</a></span>]]></content:encoded>
			<wfw:commentRss>http://unitstep.net/blog/2008/08/02/decoding-google-maps-encoded-polylines-using-php/feed/</wfw:commentRss>
		</item>
		<item>
		<title>CakePHP and error/exception handling</title>
		<link>http://unitstep.net/blog/2008/05/11/cakephp-and-errorexception-handling/</link>
		<comments>http://unitstep.net/blog/2008/05/11/cakephp-and-errorexception-handling/#comments</comments>
		<pubDate>Sun, 11 May 2008 18:40:08 +0000</pubDate>
		<dc:creator>Peter Chng</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[cakephp]]></category>

		<category><![CDATA[development]]></category>

		<category><![CDATA[exceptions]]></category>

		<guid isPermaLink="false">http://unitstep.net/?p=321</guid>
		<description><![CDATA[
I&#8217;m currently using CakePHP and finding it to be quite useful.  The &#8220;automagic&#8221; handling of tables is useful for basic relationships, though more complicated setups usually require manual work.  The MVC implementation has also clearly drawn inspiration from Ruby on Rails, which may be advantageous to some, though this has no bearing on [...]]]></description>
			<content:encoded><![CDATA[<p class="image align-right"><img src="http://unitstep.net/wordpress/wp-content/uploads/2008/05/cakephp.png" alt="Copyright CakePHP" title="cakephp" width="150" height="139" /></p>
<p>I&#8217;m currently using <a href="http://cakephp.org/">CakePHP</a> and finding it to be quite useful.  The &#8220;automagic&#8221; handling of tables is useful for basic relationships, though more complicated setups usually require manual work.  The MVC implementation has also clearly drawn inspiration from <a href="http://www.rubyonrails.org/">Ruby on Rails</a>, which may be advantageous to some, though this has no bearing on me.  Though there are a few things that nag me about CakePHP (such as lack of a good testing suite, though that&#8217;s supposedly <a href="http://bakery.cakephp.org/articles/view/testing-models-with-cakephp-1-2-test-suite">fixed in 1.2</a>, which really should be marked as version 2.0), overall it&#8217;s a great framework that adds badly-needed structure to <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> and has saved me time.</p>
<p>One thing I&#8217;d like to see, however, is a proper exception handling model.  I realize this would <a href="http://gophp5.org/">require making it <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> 5-only</a>, but in my opinion, <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> 5 adds some sorely-need features, such as the aforementioned exception handling model and a class/object system more in line with other languages.</p>
<h3>Signaling intent</h3>
<p>I&#8217;ve written about the <a href="/blog/2007/11/25/exception-handling-in-javascript/">benefits of exception handling</a> before, so I won&#8217;t delve into those details again.  In short, using exceptions is a better way for a method to signal to a caller that something went wrong rather than just returning a special/reserved value as an error code.</p>
<p>In particular, saving of models would benefit from this.  Currently, if you want to check if the model was saved properly, you have to do something like this:</p>
<pre><code>if ($this-&gt;ModelName-&gt;save($this-&gt;data))
{
  // Model saved properly, can continue normally here.
  ...
}
else
{
 // Model did not save properly.
}</code></pre>
<p>It&#8217;s a little bit kludgy, but isn&#8217;t actually that bad since Cake simplifies form validation a lot with the <acronym class="uttInitialism" title="HyperText Markup Language">HTML</acronym> helper in views.  You can simply put something like <code>$html-&gt;tagErrorMsg('Post/title', 'Title is required.')</code> in your view to display that error message when a particular field does not validate.  This removes a lot of the tedium out of making your forms return proper error messages in situations with bad input.</p>
<h3>Exceptional cases</h3>
<p>However, in some situations you might want to execute your own logic when a call to <code>Model::save()</code> fails.  In this case, having that method throw a proper exception would allow the error to be propagated to the controller nicely and would remove the need to make a call to <code>Model::invalidFields()</code> to get the reason.</p>
<p>Coming from a Java background, this makes more sense to me, since I consider its exception handling model to be one of the language&#8217;s strong points.  It has numerous benefits to improving readability and maintainability of code.</p>
<p>However, there are some drawbacks, since this requires the use of <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> 5.  As CakePHP is designed to be <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> 4 and 5 compatible, exception handling is really not an option.  However, since <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> 4 is so old, I&#8217;d eventually like to see a <a href="http://gophp5.org/">move to <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> 5</a>, especially for its better OOP model and exception handling.  Additionally, <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> 6 is just around the corner, so I don&#8217;t really see a need to stick with <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> 4 for that much longer.</p>
<p>Additionally, if you&#8217;re just saving one model (as the result of a form submission) it doesn&#8217;t really make sense, nor is it necessary, to have exception handling since CakePHP already takes care of displaying error message for the invalid fields:</p>
<pre><code>// Controller:
...
if ($this-&gt;Post-&gt;save($this-&gt;data))
{
  $this-&gt;flash('Your post has been saved.','/posts');
}
...

// View:
...
&lt;?php echo $html-&gt;input('Post/title', array('size' =&gt; '40'))?&gt;
&lt;?php echo $html-&gt;tagErrorMsg('Post/title', 'Title is required.') ?&gt;
...</code></pre>
<p>In the above example, taken from <a href="http://manual.cakephp.org/view/326/the-cake-blog-tutorial">CakePHP&#8217;s own blog tutorial</a>, you don&#8217;t really need exception handling since the appropriate actions are automatically handling by Cake.  If the save succeeds, a positive message will be displayed and the user forwarded on.  Otherwise, the controller proceeds to render the given view, where <code>$html-&gt;tagErrorMsg</code> displays the given error message if the associated field was marked as invalid by the model during the save attempt.</p>
<p>For more complicated situations where you&#8217;re saving multiple related models in a single controller action, exception handling would be helpful in reducing the number of branches, since you wouldn&#8217;t need to have so many <code>if ... else</code> blocks muddling up the flow of the code.  However, it&#8217;s not straightforward to integrate this into Cake, since for most of the time, people won&#8217;t need the exception handling capability if they&#8217;re just doing a simple save as outlined above. </p>
<p>The easiest way I see of accomplishing this is to define your own method that wraps around the existing <code>Model::save()</code> method so that proper exceptions can be thrown.  This would, of course, create confusion as then there would be <em>two</em> &#8217;save&#8217; methods. </p>
<p>All things considered, Cake is still a strong framework, so I will continue using it.  For all the slight concerns I have, it&#8217;s still very much beneficial to me.  If there&#8217;s one more thing I could ask for from Cake though, it would be for better documentation!  Maybe I should start contributing&#8230;</p>
<hr/>Copyright &copy; 2008 <strong><a href="http://unitstep.net">unitstep.net</a></strong>. This Feed is for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact <strong><a href="mailto:webmaster@unitstep.net">webmaster@unitstep.net</a></strong> for more information.<br/><span style="float: right;font-size: 7pt"><a href="http://blog.taragana.com/index.php/archive/wordpress-plugins-provided-by-taraganacom/">Plugin</a> by <a href="http://www.taragana.com/">Taragana</a></span>]]></content:encoded>
			<wfw:commentRss>http://unitstep.net/blog/2008/05/11/cakephp-and-errorexception-handling/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Password salting and the modified Challenge-Response system</title>
		<link>http://unitstep.net/blog/2008/04/28/password-salting-and-the-modified-challenge-response-system/</link>
		<comments>http://unitstep.net/blog/2008/04/28/password-salting-and-the-modified-challenge-response-system/#comments</comments>
		<pubDate>Tue, 29 Apr 2008 01:12:42 +0000</pubDate>
		<dc:creator>Peter Chng</dc:creator>
		
		<category><![CDATA[Ajax]]></category>

		<category><![CDATA[CHAP]]></category>

		<category><![CDATA[JavaScript]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[authentication]]></category>

		<category><![CDATA[chap-php]]></category>

		<category><![CDATA[login]]></category>

		<category><![CDATA[passwords]]></category>

		<category><![CDATA[security]]></category>

		<category><![CDATA[challenge-response]]></category>

		<category><![CDATA[salting]]></category>

		<guid isPermaLink="false">http://unitstep.net/?p=312</guid>
		<description><![CDATA[Since I released my demo/example modified Challenge-Response Ajax PHP Login System about a month ago (which was based on ideas from Paul Johnston), I&#8217;ve been receiving some questions about why salting was not incorporated into the system.  In particular, there was a discussion at a Dutch-language forums, which I somewhat understood after Google translation. [...]]]></description>
			<content:encoded><![CDATA[<p>Since I released my demo/example <a href="/blog/2008/03/29/a-challenge-response-ajax-php-login-system/">modified Challenge-Response Ajax <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> Login System</a> about a month ago (which was based on ideas from <a href="http://pajhome.org.uk/crypt/md5/auth.html">Paul Johnston</a>), I&#8217;ve been receiving some questions about why <a href="http://en.wikipedia.org/wiki/Salt_(cryptography)">salting</a> was not incorporated into the system.  In particular, there was a <a href="http://gathering.tweakers.net/forum/list_messages/1284731///salt">discussion</a> at a Dutch-language forums, which I somewhat understood after <a href="http://www.google.com/translate?u=http%3A%2F%2Fgathering.tweakers.net%2Fforum%2Flist_messages%2F1284731%2F%2F%2Fsalt&#038;langpair=nl%7Cen&#038;hl=en&#038;ie=UTF8">Google translation</a>. </p>
<p>Here, I&#8217;ll try to address some of these concerns and answer some questions.</p>
<h3>Salt of the earth</h3>
<p>To understand why salting is used, we must first understand exactly what it is.  Just like regular salt, in cryptography, salting is used to alter the &#8220;taste&#8221; or output of some process. </p>
<p>In the case of password storage, most people would realize that you should <em>never</em> store lists of users&#8217; passwords in plaintext, because if that data is ever compromised, attackers will gain access not only the compromised accounts but could potentially use the passwords to gain access to user accounts on other services/sites.  This is because people often reuse the same passwords across multiple account/services.</p>
<p>The easiest way to avoid this is to store the <em>hash</em> of the password.  A hash is a one-way function, that is, once you compute the hash of a value, you cannot obtain the original from just the hashed value.  Some typical hash functions are MD5 or the more secure family of SHA hash functions.</p>
<p>However, this still doesn&#8217;t fully conceal passwords.  If an attacker were to obtain the list of hashed passwords, they could try a dictionary-based attack to discover the original inputs.  This involves hashing common words to see if they hash to the correct value.  Since people often use common words or combinations of such, a dictionary-based attack has the advantage of having far fewer combinations that the attacker needs to try compared to a true brute-force attack.</p>
<h3>Adding variability</h3>
<p>This problem can be mitigated by salting, which basically amounts to combining the password with additional input before passing it into the hash function.  This alters the end output from the hash function so that a dictionary-based attack cannot be used, <strong>provided the salt is kept a secret</strong>.  A comparison example:</p>
<pre><code>Without salting:
hash(A) -&gt; B;

With salting:
hash (A + S) -&gt; C;</code></pre>
<p>In the first example, salting was not used before hashing.  Assuming the value &#8216;A&#8217; is a common word or phrase, an attacker can use a dictionary-attack to determine what the value of &#8216;A&#8217; was. (I.E. what value hashes to the value of &#8216;C&#8217;)</p>
<p>With salting, things become more difficult.  If the attacker does not know the value of the salt (S), they cannot use a dictionary-based attack because the actual input will not be a common word or phrase.  Instead, they must try all values in the dictionary <em>with</em> all possible values of the salt.  In fact, every bit of the added salt value doubles the number of computations in a dictionary-based attack.  The important point to retain here is the <strong>salt value must be kept a secret</strong> in order to obtain this benefit.</p>
<p>One other benefit of salting is to ensure that two accounts with the same password don&#8217;t produce the same hash.  This can be accomplished by making different accounts have different salts.  The obvious benefit of this is to decrease the information leakage from the hashes, as otherwise, equivalent hashes would infer equivalent passwords.</p>
<h3>Salting and the modified Challenge-Response System</h3>
<p>With the <a href="/blog/2008/03/29/a-challenge-response-ajax-php-login-system/">modified challenge-response system</a>, it isn&#8217;t clear to me how salting could be used to improve it.  Here&#8217;s a quick re-cap of how it works:</p>
<blockquote><p>
Signup:</p>
<p>1. Server sends random1<br />
2. Client sends hex_sha1(hex_hmac_sha1(password, random1))</p>
<p>Login:</p>
<p>1. Server sends random1 and random2<br />
2. Client sends hex_hmac_sha1(password, random1) and hex_sha1(hex_hmac_sha1(password, random2))
</p></blockquote>
<p>To login, the user must present two values: One to verify that they know the password, and the second value, which is used to set the response that must be computed for the <em>next login</em>.</p>
<p>Because the client must compute the next response value, it&#8217;s a bit tricky to implement salting in a way that&#8217;s beneficial. </p>
<p>One possible method would be to further hash the second value (<code>hex_sha1(hex_hmac_sha1(password, random1))</code>) with a <strong>server secret</strong> before storing it in the database.  This would complicate things should the database be compromised but not the server secret, since a dictionary attack would become much harder with the extra variability of a server secret.  In this case, the work flow would look something like this:</p>
<h4>Signup</h4>
<ol>
<li>Server sends random1</li>
<li>Client sends hex_sha1(hex_hmac_sha1(password, random1)) [<strong>Let's call this value `hashed_challenge_response` for brevity</strong>]</li>
<li>Server stores hex_hmac_sha1(server_secret, hashed_challenge_response)</li>
</ol>
<h4>Login:</h4>
<ol>
<li>Server sends random1 and random2</li>
<li>Client sends hex_hmac_sha1(password, random1) and hex_sha1(hex_hmac_sha1(password, random2))</li>
<li>Server computes hex_sha1(hex_hmac_sha1(password, random1)) [<strong>Call this `hashed_challenge_response_received`</strong>]</li>
<li>Server checks if hex_hmac_sha1(server_secret, hashed_challenge_response_received) equals the value previously stored.  If so, authentication was successful and a new challenge-response is stored based on the second value received.</li>
</ol>
<p>Note that this method <strong>would not improve the login security anymore</strong>, since an attacker who captured the intermediate traffic of a successful login could still conduct an offline dictionary attack, which this challenge-response system is unfortunately susceptible to.</p>
<p>However, the modified system does benefit from the use of the `random1` and `random2` challenge strings, which are stored alongside the response values.  Since these are random and different for each user (and for each subsequent login), accounts with the same password will not have the same hash-response.  This effectively gives the second lesser benefit of salting.</p>
<p>The modified challenge-response system also suffers from the inability of the server to enforce strong passwords.  Because the initial value sent during registration is a hashed value of the password and a random value, the server cannot be aware of any properties of the password such as its length or composition.  The only aspect that could be enforced server-side would be to make sure the password was not blank!  My suggestion is to (attempt to) enforce password complexity using JavaScript on the client side.  Such rules can obviously be circumvented but are better than nothing.</p>
<h3>Conclusion</h3>
<p>Hopefully I&#8217;ve shed some light on the topic of salting in relation to the modified challenge-response Ajax <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> login system.  I&#8217;m no security expert, so you should not take my advise as scripture.  Please don&#8217;t hesitate to give me your comments or feedback!</p>
<hr/>Copyright &copy; 2008 <strong><a href="http://unitstep.net">unitstep.net</a></strong>. This Feed is for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact <strong><a href="mailto:webmaster@unitstep.net">webmaster@unitstep.net</a></strong> for more information.<br/><span style="float: right;font-size: 7pt"><a href="http://blog.taragana.com/index.php/archive/wordpress-plugins-provided-by-taraganacom/">Plugin</a> by <a href="http://www.taragana.com/">Taragana</a></span>]]></content:encoded>
			<wfw:commentRss>http://unitstep.net/blog/2008/04/28/password-salting-and-the-modified-challenge-response-system/feed/</wfw:commentRss>
		</item>
		<item>
		<title>A Challenge-Response Ajax PHP Login System</title>
		<link>http://unitstep.net/blog/2008/03/29/a-challenge-response-ajax-php-login-system/</link>
		<comments>http://unitstep.net/blog/2008/03/29/a-challenge-response-ajax-php-login-system/#comments</comments>
		<pubDate>Sun, 30 Mar 2008 01:53:11 +0000</pubDate>
		<dc:creator>Peter Chng</dc:creator>
		
		<category><![CDATA[Ajax]]></category>

		<category><![CDATA[CHAP]]></category>

		<category><![CDATA[JavaScript]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[authentication]]></category>

		<category><![CDATA[chap-php]]></category>

		<category><![CDATA[login]]></category>

		<category><![CDATA[passwords]]></category>

		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://unitstep.net/blog/2008/03/29/a-challenge-response-ajax-php-login-system/</guid>
		<description><![CDATA[A while ago, (okay, a long while ago) I wrote about a way to improve the security of login/authentication with web applications.  The process involved using challenge-response during authentication to prevent passwords from being transmitted in plaintext.  The idea was not mine, but instead the work of a smart fellow named Paul Johnston. [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago, (okay, a <em>long</em> while ago) I wrote about <a href="/blog/2006/09/19/using-a-chap-login-system-to-improve-security/">a way to improve the security</a> of login/authentication with web applications.  The process involved using challenge-response during authentication to prevent passwords from being transmitted in plaintext.  The <a href="http://pajhome.org.uk/crypt/md5/auth.html">idea was not mine</a>, but instead the work of a smart fellow named <a href="http://pajhome.org.uk/">Paul Johnston</a>.   At the time, I &#8220;hoped to present an actual implementation&#8221; sometime in the future, but never got around to it.  I finally had some time and decided to put together a working example using <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> and JavaScript. </p>
<h3>Download the source</h3>
<p>Please feel free to download and try out the first public release of the CHAP-PHP login system.  The zip file has the full source and provides an example how to implement the system both on the client and server side.</p>
<div class="download">
<a class="icon" href='http://unitstep.net/wordpress/wp-content/uploads/2008/03/chap-php-051.zip' title='CHAP-PHP-0.5.1'>Download CHAP-PHP-0.5.1</a>
</div>
<p>The same demo available in the zip file is also <a href="/projects/CHAP-PHP/src/demo/index.php">available here</a>.</p>
<h3>Improving authentication security with Challenge-Response</h3>
<p>Challenge-Response is the basis for many authentication systems.  In such a situation, a server may have to authenticate a user by verifying their credentials, usually in the form of a password.  However, transmitting plaintext passwords over connections that are not secure can lead to compromises.  In such a situation, <dfn>challenge-response</dfn> may be used.  This usually involves the server sending a <strong>random challenge</strong> string to the client, which must then produce an appropriate <strong>response</strong> that can only be computed using the challenge and the password.  This response is then sent to the server, which can then verify if the right password was used to generate it.  The response is usually computed by hashing a value that depends on the challenge and the password, thus it is not possible to obtain the password from the response, which might have been sniffed on an insecure connection. </p>
<p>However, such a traditional challenge-response has the downside that the plaintext password (or a password-equivalent) must be known to the server at some point.  Paul Johnston came up with <a href="http://pajhome.org.uk/crypt/md5/auth.html">an idea for an alternative system</a> a while ago that overcomes these shortcomings.  (Though it is not free from weaknesses itself)  It is this &#8220;Alternative System&#8221; that the above release is based upon.  Here is a quick explanation of the system, adapted from Johnston&#8217;s site:</p>
<blockquote cite="http://pajhome.org.uk/crypt/md5/auth.html"><p>
Signup:</p>
<p>   1. Server sends random1<br />
   2. Client sends hex_sha1(hex_hmac_sha1(password, random1))</p>
<p>Login:</p>
<p>   1. Server sends random1 and random2<br />
   2. Client sends hex_hmac_sha1(password, random1) and hex_sha1(hex_hmac_sha1(password, random2))
</p></blockquote>
<p>During registration, the value sent by the client is stored.  During login, the user must present a value <em>that when hashed</em> produces the value provided at registration.  Because of the non-reversibility property of hashes, knowing the value passed during registration does not allow an attacker to login.  The only way to produce the valid response is to know the actual plaintext password, which is never transmitted or known by the server.  In this system, challenges are linked to a user and must be stored, since it must be known what challenge was used to produce the response.</p>
<p>The second random challenge (random2) and the second value sent by the client during login are used to prevent replay attacks.  Upon successful login, the second value provided by the client becomes the next response, equivalent to the value first provided during registration.  Thus, for the next login, the value that is sent must hash to equal this value.  This also means that the challenges are updated/changed each time a login is successful.  This has the unfortunate downside of revealing when a user has logged in, since the challenge presented at login will be different.  (Challenges must be publicly available)</p>
<p>For a more thorough explanation, I suggest that you <a href="http://pajhome.org.uk/crypt/md5/auth.html">read Johnston&#8217;s article on the subject</a>.</p>
<h3>Getting it to work</h3>
<p>Understanding the process above leads to the conclusion that user-login is now a two-stage process.  Since the challenges are tied to a user, the username must first be known to the server in order to retrieve the challenge for that user.  The client can then use the challenge to produce the response to send to the server for authentication.</p>
<p>Having a two-stage login form would be very unfriendly to users.  Thus, the main challenge is to make it <em>appear</em> as if nothing out of the ordinary is happening.  This is where Ajax comes into play.  When the form is submitted, the event is prevented from occurring normally.  Instead, the username is first retrieved from the form and sent to the server via an Ajax call in order to retrieve the associated challenge.  Once the challenge is received, the appropriate responses are computed, inserted into the form and then the form is submitted.</p>
<p>The current system also works in the case that JavaScript is not enabled/available on the client side.  In this case, challenge-response will not be available, since JavaScript is used to compute the responses.  The server-side <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> scripts infer that JavaScript <em>was not</em> enabled on the client-side if proper challenge-responses are not received, and thus treat the password as plaintext.  In this case, passwords are transmitted in plaintext.  With this code, you have the choice of allowing this &#8220;insecure&#8221; login to proceed or not.</p>
<p>Note that the CHAP-PHP is more of a module than a full-fledged system, since it&#8217;s not intended to be used on its own but instead as part of some application.  It might be a bit confusing if you&#8217;re a non-developer, but I&#8217;ve tried to make it as straightforward and simple as possible so that it will be easy to integrate with existing code bases/frameworks/sites. </p>
<p>Please don&#8217;t hesitate to <a href="/contact">contact me</a> with your questions, comments or suggestions.</p>
<h3>Disclaimer and warning</h3>
<p>You should not use this as the basis for authentication for sensitive data/websites.  I am not a security expert.  At this point, this is more of a proof-of-concept then something concrete.  It is intended to be the starting point for perhaps something more secure and to show that there are alternatives for more secure authentication when SSL is not available.</p>
<h4>Revision History</h4>
<ul class="note less">
<li>0.5 - First Public Release - 2008-03-29</li>
<li>0.5.1 - Fixed file-based storage to be more robust - 2008-03-30</li>
</ul>
<hr/>Copyright &copy; 2008 <strong><a href="http://unitstep.net">unitstep.net</a></strong>. This Feed is for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact <strong><a href="mailto:webmaster@unitstep.net">webmaster@unitstep.net</a></strong> for more information.<br/><span style="float: right;font-size: 7pt"><a href="http://blog.taragana.com/index.php/archive/wordpress-plugins-provided-by-taraganacom/">Plugin</a> by <a href="http://www.taragana.com/">Taragana</a></span>]]></content:encoded>
			<wfw:commentRss>http://unitstep.net/blog/2008/03/29/a-challenge-response-ajax-php-login-system/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Eclipse: The best and only IDE you&#8217;ll ever need*</title>
		<link>http://unitstep.net/blog/2008/02/10/eclipse-the-best-and-only-ide-youll-ever-need/</link>
		<comments>http://unitstep.net/blog/2008/02/10/eclipse-the-best-and-only-ide-youll-ever-need/#comments</comments>
		<pubDate>Mon, 11 Feb 2008 00:39:16 +0000</pubDate>
		<dc:creator>Peter Chng</dc:creator>
		
		<category><![CDATA[JavaScript]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[development]]></category>

		<category><![CDATA[eclipse]]></category>

		<category><![CDATA[java]]></category>

		<category><![CDATA[programming]]></category>

		<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://unitstep.net/blog/2008/02/10/eclipse-the-best-and-only-ide-youll-ever-need/</guid>
		<description><![CDATA[
* Sensationalist headline inspired by previous posts
Eclipse is my IDE of choice, as you&#8217;l probably have noticed from some of my previous articles.  I had been wanting to write an article about why I use it (and why I switched to it), but kept putting it off.  Recently, Matt Mullenweg wrote about his [...]]]></description>
			<content:encoded><![CDATA[<p class="img align-right"><img src='http://unitstep.net/wordpress/wp-content/uploads/2008/02/eclipse-ide.thumbnail.jpg' alt='Copyright The Eclipse Foundation' /></p>
<p class="note less">* Sensationalist headline inspired by <a href="/blog/2007/10/16/sirreals-g15-plugin-the-best-and-only-logitech-g15-sdk-applet-youll-ever-need/">previous posts</a></p>
<p>Eclipse is my IDE of choice, as you&#8217;l probably have noticed from some of my <a href="/blog/2008/01/19/using-assemblas-trac-with-eclipse-mylyn-xml-rpc-access/">previous articles</a>.  I had been wanting to write an article about why I use it (and why I switched to it), but kept putting it off.  Recently, Matt Mullenweg <a href="http://ma.tt/2008/02/wither-dreamweaver/">wrote about his problems with Dreamweaver</a>, and this perhaps prompted me to organize my notes on why I&#8217;ve chosen to use Eclipse.  Don&#8217;t get me wrong - I&#8217;m not advocating that you immediately switch and throw out your current editing tool (the headline above, as noted, is purely for sensationalism) - but rather I&#8217;m just urging you to consider Eclipse for your next project.</p>
<h3>Changing Gears</h3>
<p>Like many, before switching to Eclipse I had been using a pure text editor, Ultraedit, for most of my web-development activities.  Ultraedit seemed fine for <a href="/blog/2007/06/10/ultraedit-php-5-and-the-function-list/">most things</a>, offering basic features like code highlight and autocompletion.  However, it lacked a certain finesse when it came to dealing with larger projects.  For example, if you&#8217;d defined a class, its members wouldn&#8217;t be available for autocompletion.  Something else was needed.  I finally decided to take the plunge, and switch over to Eclipse for all my development towards the end of the summer last year.</p>
<p>Some might wonder why I was even using a text-editor in the first place for development.  For those coming from a traditional programming/development background, the idea of not using an IDE (Integrated Development Environment) is silly.  This is because a lot of programming languages are compiled, and in this case, it just makes sense to use an IDE since it&#8217;s easier to write code, compile and debug using one tool instead of multiple ones. </p>
<p>However, for scripting languages, especially those meant to run on a web server, one can &#8220;get away&#8221; with not using an IDE quite easily.  This is because the scripts are not run standalone but are almost always executed in the context of a web server; thus you&#8217;re usually editing code that you then run on a development web server, without the need for a special tool like a compiler.  Additionally, it&#8217;s easy to view the output using any web browser.  These reasons are what allowed me to persist in using a text-editor for so long.</p>
<h3>No turning back</h3>
<p>However, once I started using Eclipse, I was hooked.  I downloaded Eclipse PDT (<acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> Development Tools), which is basically a version of Eclipse bundled with the tools/plugins necessary for setting up a <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> development environment.  Besides offering everything Ultraedit did, it also offered nice features like easy &#8216;Todo&#8217; lists, (just type &#8216;todo&#8217; anywhere in a comment and it&#8217;s automatically indexed by Eclipse into a list), code completion for built-in <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> functions and your own as well as a multitude of other advanced features that IDEs have.  Oh, and it&#8217;s also FOSS. (Free and Open Source Software)</p>
<p>However, perhaps the best part about Eclipse is its robust and well-supported plugin system.  This allows Eclipse to pretty much assume any feature that someone is willing to write a plugin for.  This is what really sold me on Eclipse, because this almost makes its abilities endless.  Some of the plugins I use are <a href="http://subclipse.tigris.org/">Subclipse</a> for SVN integration, <a href="http://www.eclipse.org/mylyn/">Mylyn for Trac integration</a> and <a href="http://labs.adobe.com/technologies/jseclipse/">JSEclipse</a> for JavaScript editing.  This is part of the reason why Eclipse is the <a href="http://en.wikipedia.org/wiki/List_of_Eclipse-based_software">basis for many other IDEs</a> out there.</p>
<p>Some other nice features are the ability to link the IDE in with the <a href="http://www.zend.com/en/community/pdt#debugger">Zend Debugger</a>, thus allowing for proper debugging sessions with <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym>.</p>
<h3>Spoiled</h3>
<p>However, I&#8217;ve been pampered somewhat and have found a few things to complain about, at least when it comes to Eclipse PDT.  I use Eclipse JDT (Java) a work and its advanced <a href="http://en.wikipedia.org/wiki/Refactoring">refactoring abilities</a> are a feature I find myself wanting in the PDT version.  Have you ever found yourself wanting to rename a variable to something more descriptive but putting it off because you&#8217;re afraid you&#8217;ll mess something up by forgetting to change the name somewhere?</p>
<p>With some IDEs, you&#8217;re left having to just do a search-and-replace in order to accomplish what should be a trivial name refactor.  Even if your editor supports regex searches, things can still be tricky - what if you&#8217;ve used the same name, but in a different context, and thus shouldn&#8217;t change the variable there?  The point is, the process still has to be human-supervised and is tedious.  With Eclipse JDT&#8217;s advanced refactoring, you can rename the variable once - and the IDE is smart enough to know where else to change it to keep the code consistent - <strong>very</strong> neat, and I was amazed when I first used it.  Other refactoring abilities include extracting methods out of blocks of code in order to clean up lengthy methods.  All of this makes your life 10 times easier and allows you focus on real programming rather than annoying tasks.</p>
<p>However, Eclipse PDT doesn&#8217;t support this for <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> code, yet.  I hear that it may be supported in a <a href="http://wiki.eclipse.org/PDT/1.5_Features_Proposal">later release</a>, so I have my fingers crossed.  Perhaps accomplishing these refactoring tasks is easier in Java because of its compiled nature or because the JDT project has received more attention.  It&#8217;s definitely possible in <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym>, as some IDEs, such as the <a href="http://www.zend.com/en/products/studio/">Zend Studio</a> (which is based on Eclipse) support this ability.  Zend Studio, however, is a commercial solution and I haven&#8217;t tried it out yet.</p>
<h3>Nothing&#8217;s perfect</h3>
<p>Eclipse does have its downsides as compared to a traditional text editor.  First of all, it&#8217;s a memory hog - though most IDEs are.  I have regularly seen Eclipse eat up 300-400 MB of RAM if I&#8217;ve been using it for a long time.  However, it should be noted that I have not had it crash once, so it&#8217;s been rock-solid as far as stability goes.  Nonetheless, I recommend you to have at least 2 GB of memory if you really want to use it properly, since you&#8217;re likely to have other programs open.  This is especially important if you&#8217;re running Windows Vista.  RAM is quiet cheap nowadays, and you can easily pick up 2 GB for $50 or less and upgrading is a painless process, so there&#8217;s no reason not to.</p>
<h3>Finishing up</h3>
<p>Eclipse has changed my life.  Okay, so perhaps I&#8217;m exaggerating a bit.  But, I can say that development, at least for me, would be much harder without Eclipse.  If you&#8217;re still using a text editor for development, I urge you to give Eclipse a try - just for 30 days, and see how you like it.  I don&#8217;t guarantee results as good as mine, but you may be pleasantly surprised.</p>
<hr/>Copyright &copy; 2008 <strong><a href="http://unitstep.net">unitstep.net</a></strong>. This Feed is for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact <strong><a href="mailto:webmaster@unitstep.net">webmaster@unitstep.net</a></strong> for more information.<br/><span style="float: right;font-size: 7pt"><a href="http://blog.taragana.com/index.php/archive/wordpress-plugins-provided-by-taraganacom/">Plugin</a> by <a href="http://www.taragana.com/">Taragana</a></span>]]></content:encoded>
			<wfw:commentRss>http://unitstep.net/blog/2008/02/10/eclipse-the-best-and-only-ide-youll-ever-need/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
