<?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>Dereleased</title>
	<atom:link href="http://www.dereleased.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dereleased.com</link>
	<description>Information Saturation</description>
	<lastBuildDate>Sat, 10 Sep 2011 20:41:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>The importance of ZVals and Circular References</title>
		<link>http://www.dereleased.com/2011/04/27/the-importance-of-zvals-and-circular-references/</link>
		<comments>http://www.dereleased.com/2011/04/27/the-importance-of-zvals-and-circular-references/#comments</comments>
		<pubDate>Thu, 28 Apr 2011 03:40:59 +0000</pubDate>
		<dc:creator>Clark</dc:creator>
				<category><![CDATA[PHP Development]]></category>
		<category><![CDATA[PHP Quirks]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.dereleased.com/?p=170</guid>
		<description><![CDATA[Just a quick post for now. Do you know how PHP&#8217;s symbol table works? To put it in nutshell, symbols are stored in one place and values (also called ZVals) are stored in another. Normally, this abstraction will mean nothing to you, but take the following sample code: $foo = &#38;$bar; $bar = &#38;$foo; Pretty [...]]]></description>
			<content:encoded><![CDATA[<p>Just a quick post for now.  Do you know how PHP&#8217;s symbol table works?  To put it in nutshell, symbols are stored in one place and values (also called ZVals) are stored in another.  Normally, this abstraction will mean nothing to you, but take the following sample code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$foo</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$bar</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$bar</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$foo</span><span style="color: #339933;">;</span></pre></div></div>

<p>Pretty basic circular reference, and one that might be pretty difficult to assign in a few other languages.  Now what?  Well, let&#8217;s take a look at another reference construct for a moment.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'foo'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'bar'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$y</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$x</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$z</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$y</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">,</span> <span style="color: #000088;">$y</span><span style="color: #339933;">,</span> <span style="color: #000088;">$z</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/*
string(3) &quot;foo&quot;
string(3) &quot;foo&quot;
string(3) &quot;foo&quot;
*/</span></pre></div></div>

<p>Pretty much what we expected.  Now, let&#8217;s throw a wrench into the mix and reassign <span style="font-family: 'Courier New', courier, mono;">$y</span> by reference to <span style="font-family: 'Courier New', courier, mono;">&#038;$b</span>, and then examine the results:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$y</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$b</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">,</span> <span style="color: #000088;">$y</span><span style="color: #339933;">,</span> <span style="color: #000088;">$z</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/*
string(3) &quot;foo&quot;
string(3) &quot;bar&quot;
string(3) &quot;foo&quot;
*/</span></pre></div></div>

<p>Only the value of <span style="font-family: 'Courier New', courier, mono;">$y</span> changed!  That is because PHP, when assigning a reference to a reference, always points at the same ZVal, instead of creating a reference chain; this is one significant way in which PHP References are <strong>NOT</strong> pointers &#8211; they&#8217;re never more than one layer deep.  Let&#8217;s go back to our original example and assign a value to one of those variables:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$foo</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$foo</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bar</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/*
int(3);
int(3);
*/</span></pre></div></div>

<p>Works like a charm!  This is because both references pointed at the same location in the ZVal table.  But what if we start over again, and reassign $foo by reference to something else?</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$foo</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$bar</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$bar</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$foo</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$baz</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'baz'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$foo</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$baz</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$foo</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bar</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/*
string(3) &quot;baz&quot;
NULL
*/</span></pre></div></div>

<p>If you&#8217;ve been following along, this should make perfect sense.  <span style="font-family: 'Courier New', courier, mono;">$foo</span> is created, and pointed at a ZVal location identified by <span style="font-family: 'Courier New', courier, mono;">$bar</span>; when <span style="font-family: 'Courier New', courier, mono;">$bar</span> is created, it points at the same place <span style="font-family: 'Courier New', courier, mono;">$foo</span> was pointed.  That location, of course, is null.  When <span style="font-family: 'Courier New', courier, mono;">$foo</span> is reassigned, the only thing that changes is to which ZVal <span style="font-family: 'Courier New', courier, mono;">$foo</span> points; if we had assigned a different value to <span style="font-family: 'Courier New', courier, mono;">$foo</span> first, then <span style="font-family: 'Courier New', courier, mono;">$bar</span> would still retain that value.</p>
<p>While we&#8217;re on the topic of ZVals, I&#8217;ll mention just one more thing.  PHP uses a lazy-copying (or, copy-on-write) mechanism, thanks to the ZVal table.  Consider the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$foo</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_repeat</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'x'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">100000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mem1</span> <span style="color: #339933;">=</span> <span style="color: #990000;">memory_get_usage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$bar1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$bar2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$bar3</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$bar4</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$bar5</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$bar6</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$foo</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mem2</span> <span style="color: #339933;">=</span> <span style="color: #990000;">memory_get_usage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$bar1</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;...&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$mem3</span> <span style="color: #339933;">=</span> <span style="color: #990000;">memory_get_usage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I leave the calls to <span style="font-family: 'Courier New', courier, mono;">memory_get_usage()</span> in place so that their effects will be more obvious.  If we dump those three values, we get 426040, 426408 and 526536, respectively.  In the second phase, as you can see, we only increased memory usage by 386 bytes (and that includes the memory required to store the memory that was used).  During the third phase, when a variable was altered, memory usage increased by 100128 bytes.  PHP uses about 24 bytes of memory to make an entry into the symbol table, and 80 more to create a null entry in the ZVal table.</p>
<p>So, the next time you think about passing a parameter you don&#8217;t intend to modify to a function by reference in order to save memory, or returning one for the same reason, don&#8217;t worry about it so much; it&#8217;s only 24 bytes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dereleased.com/2011/04/27/the-importance-of-zvals-and-circular-references/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Quirks &#8211; String manipulation by offset</title>
		<link>http://www.dereleased.com/2011/04/27/php-quirks-string-manipulation-by-offset/</link>
		<comments>http://www.dereleased.com/2011/04/27/php-quirks-string-manipulation-by-offset/#comments</comments>
		<pubDate>Thu, 28 Apr 2011 02:55:29 +0000</pubDate>
		<dc:creator>Clark</dc:creator>
				<category><![CDATA[PHP Development]]></category>
		<category><![CDATA[PHP Quirks]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.dereleased.com/?p=165</guid>
		<description><![CDATA[Just a quick update for a mild PHP Quirk/annoyance I have noticed recently while doing some manipulation of strings by character offset. Say you have a string, such as &#8216;abcde&#8217;; Now, suppose you want to check the value of the third character (at index 2). You might have done something like this: $str = 'abcde'; [...]]]></description>
			<content:encoded><![CDATA[<p>Just a quick update for a mild PHP Quirk/annoyance I have noticed recently while doing some manipulation of strings by character offset.</p>
<p>Say you have a string, such as &#8216;abcde&#8217;;  Now, suppose you want to check the value of the third character (at index 2).  You might have done something like this:</p>
<div class="wp_syntax">
<div class="code">
<pre class="php" style="font-family:monospace;"><span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'abcde'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">(</span><span style="color: #000088;">$str</span><span style="color: #009900;">{</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">}</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'c'</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
  <span style="color: #666666; font-style: italic;">// do something...</span>
<span style="color: #009900;">}</span></pre>
</div>
</div>
<p>And, of course, that&#8217;s all fine, well and dandy, it does what you expect and you can move on with your life.  In fact, if you&#8217;re in to micro-optimizations, that construct provides a great way to check a string for minimum length, and is, on average, 44% faster than using <span style="font-family: 'Courier New', courier, mono;">strlen()</span>.  However, you can use this same construct to change the value of the character at whatever string you&#8217;re working with.  It works roughly as expected, but with a few gotchas:
<div class="wp_syntax">
<div class="code">
<pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Gotcha #0 - Adding multiple characters to a single offset; shouldn't really be a gotcha
 */</span>
<span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'abc123'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$str</span><span style="color: #009900;">{</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">}</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'a'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// aac123</span>
<span style="color: #000088;">$str</span><span style="color: #009900;">{</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">}</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'123'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// aac113</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Gotcha #1 - Adding characters past the end of the string
 */</span>
<span style="color: #000088;">$str</span><span style="color: #009900;">{</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">}</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'c'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// aac113 c</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">ord</span><span style="color: #009900;">(</span><span style="color: #000088;">$str</span><span style="color: #009900;">{</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">}</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// prints '32', the space character</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Gotcha #2 - Adding characters to an empty string
 */</span>
<span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$str</span><span style="color: #009900;">{</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">}</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'a'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// array( 0 =&gt; 'a' )</span></pre>
</div>
</div>
<p>In the first case, we see that, rather than leave the &#8220;uninitialized&#8221; area between where we&#8217;ve defined characters as a null character, it has been silently converted to a space.  Arguably, this is so that an <span style="font-family: 'Courier New', courier, mono;">isset($str[6]);</span> check would not return false, but this is important to know if you expected the values of those spaces to remain at zero.</p>
<p>In the second case, we see PHP&#8217;s weak typing in place; since an empty string has no offsets to begin with, attempts to add characters results in silent conversion to an array.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dereleased.com/2011/04/27/php-quirks-string-manipulation-by-offset/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Let&#8217;s talk about your password model</title>
		<link>http://www.dereleased.com/2010/02/09/lets-talk-about-your-password-model/</link>
		<comments>http://www.dereleased.com/2010/02/09/lets-talk-about-your-password-model/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 17:02:50 +0000</pubDate>
		<dc:creator>Clark</dc:creator>
				<category><![CDATA[Information Security]]></category>
		<category><![CDATA[PHP Development]]></category>
		<category><![CDATA[The Internet]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[blowfish]]></category>
		<category><![CDATA[crypt]]></category>
		<category><![CDATA[md5]]></category>
		<category><![CDATA[nonce]]></category>
		<category><![CDATA[password]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[salt]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[sha1]]></category>
		<category><![CDATA[sha256]]></category>

		<guid isPermaLink="false">http://www.dereleased.com/?p=151</guid>
		<description><![CDATA[First off, let me just say that I am by no means an expert cryptographer; there are all sorts of wonderful, terrible things about hashes and block ciphers that I just don&#8217;t understand (I&#8217;d like to believe that it&#8217;s because I&#8217;ve not been exposed to them, whoever&#8217;s fault that is, and that if given a [...]]]></description>
			<content:encoded><![CDATA[<p>First off, let me just say that I am by no means an expert cryptographer; there are all sorts of wonderful, terrible things about hashes and block ciphers that I just don&#8217;t understand (I&#8217;d like to believe that it&#8217;s because I&#8217;ve not been exposed to them, whoever&#8217;s fault that is, and that if given a chance I would get it), but that&#8217;s also why I&#8217;m writing this &#8211; to give the opinion of someone who recognizes his own weakness, and how that translates to another&#8217;s strength.  Furthermore, this explanation gives a very simplistic view of web security that only examines one aspect of a secure system.  For loads more information about securing your web application, take a look at <a href="http://cookies.lcs.mit.edu/pubs/webauth:tr.pdf">&#8220;Dos and Don&#8217;ts of Client Authentication on the Web&#8221; <small>[PDF]</small></a> written by some very smart folks at M.I.T.</p>
<p>So, let&#8217;s start with a beginner&#8217;s introduction.  In the beginning, there were users, and users wanted to be able to log in because otherwise being a user was rather pointless indeed.  Thus, the password is born, and forevermore it becomes the goal of clever crackers and security experts alike.  The first problem someone encounters with passwords is how to store them, and that depends very much on a few key factors: Audience, Exposure, and Uniqueness.  If you are running a &#8220;homegrown&#8221; application (shout out to MecTracker) for use only inside the company, containing (in general) zero sensitive data, and you intend to pick user&#8217;s passwords for them (preventing the loss of a life password, itself a bad-yet-unavoidable practice), then why not just store them in plain text?  Certainly makes it easy to retrieve a password for someone without having to reset it (useful for someone away from their work machine with saved password who needs to log in).</p>
<p>Conversely, if you&#8217;re a bank, and you&#8217;re storing any of this in plain text, you will be razed to the ground by angry tech-savvy customers and auditors alike, hopefully BEFORE you get grandma and grandpa Jones to type in the password they use for everything else, too.  Hopefully, if you&#8217;re a bank, you&#8217;re using some crazy method I&#8217;m not about to describe here.</p>
<p>Then, there&#8217;s the middle ground.  I, for example, am not a bank (who would&#8217;ve guessed?  Can someone please notify my ex-girlfriend?), so my needs are much more middle-of-the-road, which is why I&#8217;ve settled for hashing.  When I started using PHP, I generally stuck to simple MD5 hashes; it was 10 years ago, and breaking MD5 seemed reasonably difficult.  Then I was told not to use MD5 because, at 128 bits, it was too weak, and I should be using SHA-1, which was 160 bits.  Then came the recommendation for SHA-256 (guess how many bits that one is!), and then whirlpool, and so on.  If you&#8217;re using a proper password strategy then you&#8217;ve been salting all along (I&#8217;ll admit I wasn&#8217;t in the old days, but you&#8217;ve got to be a beginner sometime), but if you haven&#8217;t, allow me to give you a word on salt.</p>
<p>&#8220;Salting&#8221; a password hash is the practice of taking a piece of input data, adding in an extra piece of information (called &#8220;salt&#8221;; see where this is going?), and hashing that, instead of just hashing the raw input.  In fact, with sites <a href="http://md5.rednoize.com/">that act like a search engine for MD5 and SHA-1 hashes</a>, not salting your input is, for general purpose storage, only one-degree of separation away from just storing the data in plain text.  Furthermore, good salt will be ever-changing (in this practice, the salt is also known as a &#8216;nonce&#8217;), and can safely be stored without obfuscation, as having included it means that a table not accounting for the nonce is useless, and a table that accounts for the nonce is only good against one of the passwords in your database.  Now you&#8217;ve just made an attack much more expensive, but that may not be as useful in reality as we&#8217;d like to believe.</p>
<p>MD5 and SHA-1 hashes can be calculated very, very quickly.  In fact, it&#8217;s generally more expensive to include some data about the current time (for use in salting/as a nonce) than it is to calculate the actual hash.  Here is some experimental code to prove my point:</p>
<div class="wp_syntax">
<div class="code">
<pre class="php" style="font-family:monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">(</span><span style="color: #0000ff;">'ITERATIONS'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$tt</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$th</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">(</span><span style="color: #000088;">$j</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$j</span> <span style="color: #339933;">&lt;</span> ITERATIONS<span style="color: #339933;">;</span> <span style="color: #339933;">++</span><span style="color: #000088;">$j</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
	<span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">(</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">for</span> <span style="color: #009900;">(</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">(</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">)</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$start</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span><span style="color: #000088;">$i</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
		<span style="color: #000088;">$k</span> <span style="color: #339933;">=</span> <span style="color: #990000;">md5</span><span style="color: #009900;">(</span><span style="color: #000088;">$i</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">}</span>
	<span style="color: #000088;">$tt</span> <span style="color: #339933;">+=</span> <span style="color: #009900;">(</span><span style="color: #990000;">microtime</span><span style="color: #009900;">(</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">)</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$start</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$th</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$i</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span>
&nbsp;
<span style="color: #990000;">var_dump</span><span style="color: #009900;">(</span><span style="color: #000088;">$tt</span> <span style="color: #339933;">/</span> ITERATIONS<span style="color: #339933;">,</span> <span style="color: #000088;">$th</span> <span style="color: #339933;">/</span> ITERATIONS<span style="color: #009900;">)</span><span style="color: #339933;">;</span></pre>
</div>
</div>
<p>Simply hashing the value of the counter averaged 320,000 hashes per second on my work machine, which is not very powerful, and is certainly not running this in a very optimized way.  By changing what is being hashed to the current time to the microsecond, the number of hashes per second is reduced to an average of about 150,000 &#8211; in short, the hash is NOT the expensive part of what&#8217;s going on here.  So, let&#8217;s say that, given a more optimized environment but a more expensive dictionary list to be hashed, that the average is 200,000 hashes per second, and the dictionary is about 50,000,000 common passwords.  Simple math tells you that generating a hash list for this will take about 250 seconds, or less than 5 minutes.  If it takes under 5 minutes to generate a table, and only a few seconds from there to query it, then even a database of 150,000 users can be fully cracked in just under a fortnight.</p>
<p>So how can this be combated?  Well, strong password guidelines are a good start, but if you&#8217;re relying on users to implement password security for you, you&#8217;re probably doing it very, very wrong.  I&#8217;d like to challenge one of the assumptions you&#8217;ve probably made that I&#8217;ve had to challenge recently, and that is the value of speed; speed is <em>bad</em>.  Think about it: using a hash method that can generate a table of fifty million values in under 5 minutes sounds great from a performance perspective, but who are you really helping?  Is your user going to notice that your hash method took under 1ms to calculate, or is this performance more likely to benefit someone trying to crack your passwords?  Who would be more hurt if your passwords took closer to 12ms to generate and verify, your users or your would-be attacker?</p>
<p>If you haven&#8217;t heard of it yet, may I introduce you to <a href="http://www.bletchleypark.net/cryptology/blowfish.html">Blowfish Encryption</a>.  Blowfish is designed to scale with Moore&#8217;s Law by allowing you, the programmer, to decide how long it takes to generate a hash.  This is done by allowing you to specify a number which will be interpreted as a log-base-2 of how many iterations the hashing sequence should take; this metadata is then stored as part of the salt, prepended to the hash, and can be verified by the same function that created it since hashes are of fixed length and will be truncated or padded accordingly.  By using a log-base-2 scale, every increment of that number (n) literally doubles the time required to calculate the hash, as it will have to undertake 2<sup>n</sup> iterations to generate the password.  From what I can gather, a number like 7 or 8 is a fair industry standard at this time, and on my work machine limits the hashes-per-second to around 86.6 and 43.3, respectively.</p>
<p>Now, performance is a factor in real world applications, so let&#8217;s pick a number like 2<sup>7</sup>, which as I said allows about 87 hashes per second.  At that rate, a single dictionary table (useful for only one user, since we are salting these passwords) takes about six and a half days to generate.  For that same database of 150,000 users, it would take over 2,733 <b>years</b> to crack.  Of course, computational power will get less expensive as time goes on, and the same number of operations can and will get faster, but with the blowfish algorithm you need only increment the log to double the computational cost, keeping the cracking of your database safely outside the realm of technical feasibility.</p>
<p>So how does one use the blowfish algorithm in PHP?  The <a href="http://php.net/manual/en/function.crypt.php" style="font-family: 'Courier New',courier,monospace;">crypt()</a> function is your friend!  However, the manual is not entirely clear on the implementation details of blowfish, as it does not include one key part (which caused me to tear my hear out a little bit, since, as a Windows user, I was unable to check the man pages for crypt(3)) in any great detail, and that is the log base.  When you generate the salt, you will need to prepend it with an instruction string that tells it what kind of hash to generate, and what parameters to use.  Furthermore, the salt is not sixteen characters, but sixteen BYTES, and the characters in your hash will be read as a BASE64 encoded string, which means that using characters not allowed in a base64 string will cause the function to revert back to whatever the default is on your system, probably STD_DES or MD5.</p>
<p>All of that information might have seemed a bit hazy, so I&#8217;ll include the timing example I used before modified to suit crypt/blowfish.  Note also that I am storing the microtime result on every iteration of the for-loop, as in order to give you worst-case scenarios on the cracker&#8217;s timetable, I had to give best-case timings on the hashing, and that means as few calls to microtime as possible.</p>
<div class="wp_syntax">
<div class="code">
<pre class="php" style="font-family:monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">(</span><span style="color: #0000ff;">'ITERATIONS'</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$tt</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$th</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">(</span><span style="color: #000088;">$j</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$j</span> <span style="color: #339933;">&lt;</span> ITERATIONS<span style="color: #339933;">;</span> <span style="color: #339933;">++</span><span style="color: #000088;">$j</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
	<span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">(</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">for</span> <span style="color: #009900;">(</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #009900;">(</span><span style="color: #000088;">$z</span> <span style="color: #339933;">=</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">(</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$start</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span><span style="color: #000088;">$i</span><span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
		<span style="color: #000088;">$k</span> <span style="color: #339933;">=</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">(</span><span style="color: #000088;">$i</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'$2a$07$'</span> <span style="color: #339933;">.</span> <span style="color: #009900;">(</span>string<span style="color: #009900;">)</span><span style="color: #000088;">$z</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">}</span>
	<span style="color: #000088;">$tt</span> <span style="color: #339933;">+=</span> <span style="color: #009900;">(</span><span style="color: #000088;">$z</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$start</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$th</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$i</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span>
&nbsp;
<span style="color: #990000;">var_dump</span><span style="color: #009900;">(</span><span style="color: #000088;">$tt</span> <span style="color: #339933;">/</span> ITERATIONS<span style="color: #339933;">,</span> <span style="color: #000088;">$th</span> <span style="color: #339933;">/</span> ITERATIONS<span style="color: #009900;">)</span><span style="color: #339933;">;</span></pre>
</div>
</div>
<p>Of paramount importance is the literal string prepended to the stored value.  The first four characters, <span style="font-family:'Courier New',courier,monospace;">$2a$</span>, simply instruct crypt to use the blowfish algorithm.  The next three, <span style="font-family:'Courier New',courier,monospace;">07$</span>, pass the number 7 as our log-base-2 argument, meaning the computation will run for 2<sup>7</sup> iterations.  After that, we concatenate our salt (values shorter than 22 characters will be padded in a predictable fashion, and longer than 22 will be truncated) to the argument string and send it off on its merry, 12ms way.</p>
<p>Do I think I&#8217;ve defeated all the clever crackers out there?  Certainly not.  However, I&#8217;m definitely in a better boat for having stood on the shoulders of giants and listened to people smarter than I am about security.  In fact, don&#8217;t listen to me, check out these links for more info:</p>
<p><a href="http://vwng.blogspot.com/2008/01/php-hash.html">(Victor) Xi Wang talks about salt, nonces and rainbow tables</a></p>
<p><a href="http://chargen.matasano.com/chargen/2007/9/7/enough-with-the-rainbow-tables-what-you-need-to-know-about-s.html">Matasano Security, LLC, talks about blowfish and why you shouldn&#8217;t design your own password protection scheme.</a></p>
<p><a href="http://www.bletchleypark.net/cryptology/blowfish.html">Linked earlier, explains blowfish encryption &#8211; very math/pseudocode heavy.</a></p>
<p><a href="http://php.net/manual/en/function.crypt.php">Also linked earlier, the PHP Manual Entry for Crypt()</a></p>
<p>Happy Hashing!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dereleased.com/2010/02/09/lets-talk-about-your-password-model/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Pour Some Syntactic Sugar On Me: &#8216;Unless&#8217; Keyword</title>
		<link>http://www.dereleased.com/2010/01/27/pour-some-syntactic-sugar-on-me-unless-keyword/</link>
		<comments>http://www.dereleased.com/2010/01/27/pour-some-syntactic-sugar-on-me-unless-keyword/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 19:49:06 +0000</pubDate>
		<dc:creator>Clark</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[control]]></category>
		<category><![CDATA[control structure]]></category>
		<category><![CDATA[if block]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[syntactic sugar]]></category>
		<category><![CDATA[syntax]]></category>
		<category><![CDATA[unless]]></category>

		<guid isPermaLink="false">http://www.dereleased.com/?p=145</guid>
		<description><![CDATA[Let&#8217;s face it, syntactic sugar can be a very attractive feature for a language (I consider Perl to be an extremely powerful language composed almost entirely of syntactic sugar), and I think it&#8217;s about time we all started demanding the &#8220;Unless&#8221; Keyword as a counterpart to the &#8220;If&#8221; Keyword. Let me give you a pretty [...]]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s face it, syntactic sugar can be a very attractive feature for a language (I consider Perl to be an extremely powerful language composed almost entirely of syntactic sugar), and I think it&#8217;s about time we all started demanding the &#8220;Unless&#8221; Keyword as a counterpart to the &#8220;If&#8221; Keyword.  Let me give you a pretty common example:</p>
<div class="wp_syntax">
<div class="code">
<pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">(</span>    <span style="color: #339933;">!</span><span style="color: #990000;">ctype_digit</span><span style="color: #009900;">(</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">[</span><span style="color: #0000ff;">'quantity'</span><span style="color: #009900;">]</span><span style="color: #009900;">)</span>
    <span style="color: #339933;">||</span>  <span style="color: #339933;">!</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">(</span><span style="color: #0000ff;">'/one of the billions of email address validators/'</span><span style="color: #339933;">,</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">[</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">]</span><span style="color: #009900;">)</span>
    <span style="color: #339933;">||</span>  <span style="color: #000088;">$_POST</span><span style="color: #009900;">[</span><span style="color: #0000ff;">'password1'</span><span style="color: #009900;">]</span> <span style="color: #339933;">!=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">[</span><span style="color: #0000ff;">'password2'</span><span style="color: #009900;">]</span>
    <span style="color: #339933;">||</span>  <span style="color: #339933;">!</span>my_validation_routine<span style="color: #009900;">(</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">[</span><span style="color: #0000ff;">'Im_Running_Out_Of_Examples'</span><span style="color: #009900;">]</span><span style="color: #009900;">)</span>
<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
    <span style="color: #990000;">die</span><span style="color: #009900;">(</span><span style="color: #0000ff;">'Bad Data'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span></pre>
</div>
</div>
<p>Quick-And-Dirty form validation.  Of course, one would normally want to check these separately so that meaningful error messages could be dumped, but let&#8217;s assume for a moment that this isn&#8217;t the case for this project.  I&#8217;m not saying this is a life or death matter.  I&#8217;m not saying the above code doesn&#8217;t work or anything like that.  What I am saying is that, from a readability/logical perspective, it would make sense to have an unless keyword to transform the above sequence from a bunch of or-not ideas into and-must-be ideas.  Example!</p>
<div class="wp_syntax">
<div class="code">
<pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">unless</span> <span style="color: #009900;">(</span>    <span style="color: #990000;">ctype_digit</span><span style="color: #009900;">(</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">[</span><span style="color: #0000ff;">'quantity'</span><span style="color: #009900;">]</span><span style="color: #009900;">)</span>
        <span style="color: #339933;">&amp;&amp;</span>  <span style="color: #990000;">preg_match</span><span style="color: #009900;">(</span><span style="color: #0000ff;">'/one of the billions of email address validators/'</span><span style="color: #339933;">,</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">[</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">]</span><span style="color: #009900;">)</span>
        <span style="color: #339933;">&amp;&amp;</span>  <span style="color: #000088;">$_POST</span><span style="color: #009900;">[</span><span style="color: #0000ff;">'password1'</span><span style="color: #009900;">]</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">[</span><span style="color: #0000ff;">'password2'</span><span style="color: #009900;">]</span>
        <span style="color: #339933;">&amp;&amp;</span>  my_validation_routine<span style="color: #009900;">(</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">[</span><span style="color: #0000ff;">'Im_Running_Out_Of_Examples'</span><span style="color: #009900;">]</span><span style="color: #009900;">)</span>
<span style="color: #009900;">)</span> <span style="color: #009900;">{</span>
    <span style="color: #990000;">die</span><span style="color: #009900;">(</span><span style="color: #0000ff;">'Bad Data'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span></pre>
</div>
</div>
<p>I realize some people don&#8217;t or won&#8217;t care about this, and that&#8217;s fine.  It&#8217;s not for them.  I like to think in code, and it would make code-thought to normal-though a bit more one-to-one if I could think in terms of &#8220;Unless a is valid and b is valid and c is valid return false&#8221; instead of &#8220;If a is not valid or b is not valid or c is not valid return false&#8221;.  C++ Programmers are lucky in this regard, as they are 1 macro away from having this keyword:</p>
<div class="wp_syntax">
<div class="code">
<pre class="c" style="font-family:monospace;"><span style="color: #339933;">#define unless(cond) if(!(cond))</span></pre>
</div>
</div>
<p>In conclusion, I know it doesn&#8217;t really change any thing, I know some people don&#8217;t care, but I want it.  I guess it&#8217;s time to start maintaining my own PHP distro&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dereleased.com/2010/01/27/pour-some-syntactic-sugar-on-me-unless-keyword/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

