<?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>Jason Mooberry &#187; PHP</title>
	<atom:link href="http://blog.jasonmooberry.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.jasonmooberry.com</link>
	<description>Stuff I did for you.</description>
	<lastBuildDate>Sat, 29 Oct 2011 23:41:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.2</generator>
		<item>
		<title>Constant time string comparison</title>
		<link>http://blog.jasonmooberry.com/2010/10/constant-time-string-comparison/</link>
		<comments>http://blog.jasonmooberry.com/2010/10/constant-time-string-comparison/#comments</comments>
		<pubDate>Sat, 23 Oct 2010 00:59:55 +0000</pubDate>
		<dc:creator>Jason Mooberry</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[bitwise operators]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://blog.jasonmooberry.com/?p=119</guid>
		<description><![CDATA[I recently spotted @fabpot&#8216;s tweet about his Symfony2 commit to prevent timing attacks. It&#8217;s a pretty simple change. Basically it just makes every password string comparison run through every character no matter if the match fails on any character. I don&#8217;t use bitwise operators much so it took a sec to spot how it works. [...]]]></description>
			<content:encoded><![CDATA[<p>I recently spotted <a href="http://twitter.com/#!/fabpot">@fabpot</a>&#8216;s tweet about his <a href="http://github.com/fabpot/symfony/commit/0749038e73b0def26abfefa5cc40f30683c7b460">Symfony2 commit</a> to prevent <a href="http://codahale.com/a-lesson-in-timing-attacks/">timing attacks</a>.  It&#8217;s a pretty simple change.  Basically it just makes every password string comparison run through every character no matter if the match fails on any character.  I don&#8217;t use bitwise operators much so it took a sec to spot how it works. </p>
<p>The comparison code is:</p>
<pre class="brush: php">
$result = 0;
for ($i = 0; $i < strlen($password1); $i++) {
  $result |= ord($password1[$i]) ^ ord($password2[$i]);
}
return 0 === $result;
</pre>
<p>The key components are:</p>
<pre class="brush: php">
ord()  // translate a character into it's ASCII value (an 8 bit integer)
^       // bitwise xor
|=     // short for left assign value bitwise or'd with new value
</pre>
<p>Just for clarity, bitwise xor evaluates to 1 for 1/0 or 0/1 or 0/0 but evaluates to 0 for 1/1.  So when comparing two integers by xor'ing the bits a perfect match will evaluate to 0.  Anything but an exact match will evaluate to 1.  </p>
<pre class="brush: plain">
01110000 xor
01110000
00000000  // match

01110011 xor
01110010
00000011  // nope
</pre>
<p>This means that if we xor each integer value of each character and or the produced values together it will evaluate to 0 when the two strings match.  And this is always accomplished in string length time.</p>
<pre class="brush: plain">
00000000  or
00000000  or
...
00000000
00000000  // match
</pre>
<p>As an aside, bitwise operators can do some neat stuff.  This one is a solution that my Pascal instructor in college mentioned as a fast cheap way to swap screen buffers.  In this example we're just swapping variable values without an intermediate.</p>
<pre class="brush: php">
$a = 'a';
$b = 'b';

$a ^= $b;
$b ^= $a;
$a ^= $b;

echo $a;  // b
echo $b;  // a
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.jasonmooberry.com/2010/10/constant-time-string-comparison/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PHP cast as array</title>
		<link>http://blog.jasonmooberry.com/2010/07/php-cast-as-array-and-why-returning-null-is-useful/</link>
		<comments>http://blog.jasonmooberry.com/2010/07/php-cast-as-array-and-why-returning-null-is-useful/#comments</comments>
		<pubDate>Tue, 27 Jul 2010 23:19:04 +0000</pubDate>
		<dc:creator>Jason Mooberry</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[casting]]></category>
		<category><![CDATA[shenanigans]]></category>

		<guid isPermaLink="false">http://blog.jasonmooberry.com/?p=68</guid>
		<description><![CDATA[PHP is full of shenanigans.  If you know them you can code more efficiently.  If you don&#8217;t it can make for a mess.  One of the lesser used, but useful tricks is casting as array. If you&#8217;re not familiar with casting, check the docs:  http://php.net/manual/en/language.types.type-juggling.php Here&#8217;s a quick breakdown of what happens when you cast [...]]]></description>
			<content:encoded><![CDATA[<p>PHP is full of shenanigans.  If you know them you can code more efficiently.  If you don&#8217;t it can make for a mess.  One of the lesser used, but useful tricks is casting as array.</p>
<p>If you&#8217;re not familiar with casting, check the docs:  <a href="http://php.net/manual/en/language.types.type-juggling.php">http://php.net/manual/en/language.types.type-juggling.php</a></p>
<p>Here&#8217;s a quick breakdown of what happens when you cast to array:</p>
<pre class="brush: php">
php > var_dump((array)false);
array(1) {
  [0]=>
  bool(false)
}
php > var_dump((array)null);
array(0) {
}
php > var_dump((array)0);
array(1) {
  [0]=>
  int(0)
}
php > var_dump((array)"");
array(1) {
  [0]=>
  string(0) ""
}
php > var_dump((array)array());
array(0) {
}
php > var_dump((array)new StdClass);
array(0) {
}
php > class TestIterator implements IteratorAggregate {
php >   public function getIterator() {
php >     return new ArrayIterator(array(1,2,3));
php >   }
php > }
php > $a = new TestIterator;
php > var_dump((array)$a);
array(0) {
}
php > foreach ($a as $num) { echo $num,"\n"; }
1
2
3
</pre>
<p>The interesting bits here are that null, empty array and empty object all cast to empty array.  Even an Iterator.  Since the Iterator object is empty is casts to an empty array, even though iterating through it will produce 3 elements.  Any other value casts to an array with a single element that is the value.  </p>
<p>There&#8217;s a couple use cases I&#8217;d like to highlight from this list.  Flexibility in signature params, and easier iteration:</p>
<pre class="brush: php">
function string_replace($string,$elements) {
  foreach((array)$elements as $element) {
    $pos = strpos($string,'?');
    if ($pos !== false) {
      $string = substr_replace($string,$element,$pos,1);
    }
  }
  return $string;
}
</pre>
<p>This is just a simple function for replacing question marks in a string with supplied string(s).  By casting $elements as an array we can take a single param or an array of params without checking for it in our code.  We can also passively return the string untouched if null params are supplied. </p>
<pre class="brush: php">
php > echo string_replace('the name is ?','jason');
the name is jason
php > echo string_replace('? ? ? at swingers',array('in','the','evening'));
in the evening at swingers
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.jasonmooberry.com/2010/07/php-cast-as-array-and-why-returning-null-is-useful/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.055 seconds -->

