<?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>KevBurnsJr - Blog</title>
	<atom:link href="http://blog.kevburnsjr.com/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.kevburnsjr.com</link>
	<description>Web Application Engineer</description>
	<lastBuildDate>Fri, 25 Nov 2011 21:15:21 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Asynchronous RSA key generation in Javascript</title>
		<link>http://blog.kevburnsjr.com/javascript-asynchronous-rsa-key-generation</link>
		<comments>http://blog.kevburnsjr.com/javascript-asynchronous-rsa-key-generation#comments</comments>
		<pubDate>Fri, 25 Nov 2011 20:56:14 +0000</pubDate>
		<dc:creator>KevBurnsJr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.kevburnsjr.com/?p=1860</guid>
		<description><![CDATA[Source on GitHub &#8211; https://github.com/KevBurnsJr/rsasync
&#8211;
Generating RSA keys is a characteristically CPU intensive operation. This presents problems when operating on weak devices (such as the iPhone 3G) in environments under strict computation restrictions (such as safari mobile&#8217;s 10 second javascript execution timeout).
What is needed is an RSA key generation library that operates asynchronously in order to [...]]]></description>
			<content:encoded><![CDATA[<p>Source on GitHub &#8211; <a href="https://github.com/KevBurnsJr/rsasync">https://github.com/KevBurnsJr/rsasync</a><br />
&#8211;</p>
<p>Generating RSA keys is a characteristically CPU intensive operation. This presents problems when operating on weak devices (such as the iPhone 3G) in environments under strict computation restrictions (such as safari mobile&#8217;s 10 second javascript execution timeout).</p>
<p>What is needed is an RSA key generation library that operates asynchronously in order to chug through the 2+ minutes of computation time required to generate a 512 RSA key on a weak device without bumping against the computation restrictions enforced by the safari mobile execution environment.</p>
<p>Some nearly suitable libraries do exist, but all of them fall short in some fashion.</p>
<ul>
<li style="list-style: disc">
Probably the closest is this <strong style="color:white">asynchronous keygen</strong> from Atsushi Oka<br />
<a href="http://ats.oka.nu/titaniumcore/js/crypto/readme.txt">http://ats.oka.nu/titaniumcore/js/crypto/readme.txt</a><br />
However, the interface for Ats Oka&#8217;s library is not simple and the architecture of the code leaves something to be desired.</p>
</li>
<li style="list-style: disc">
<strong style="color:white">Cryptico</strong> is another library featuring RSA key generation which is also touted as a sort of all-in-one solution<br />
<a href="http://code.google.com/p/cryptico/">http://code.google.com/p/cryptico/</a><br />
However, this library really just glues together a bunch of already-available libraries and packages them as a unit.</p>
</li>
<li style="list-style: disc">
<strong style="color:white">jsbn</strong> is the underlying RSA key generator packaged with Cryptico and is available on its own<br />
<a href="http://www-cs-students.stanford.edu/~tjw/jsbn/">http://www-cs-students.stanford.edu/~tjw/jsbn/</a><br />
This library has a fairly simple interface and is relatively fast and compact meeting most of my requirements.<br />
However, <span style="color:#BD48B3;font-style:italic;">this library doesn&#8217;t do asynchronous key generation</span>.  </p>
<p>But <span style="color:#66CC66;font-style:italic;">we can fix that</span>.
</li>
</ul>
<p>jsbn RSA keygen times out after 11 seconds on the iPhone 3G for even a 256 bit key but with a little fenangling and a lot of setTimeouts, we can get it to handle a key of virtually any size for which the user has the patience to wait.</p>
<p>Here&#8217;s an example of the new async interface:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">key <span class="sy0">=</span> <span class="kw2">new</span> RSAKey<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
key.<span class="me1">generateAsync</span><span class="br0">&#40;</span><span class="nu0">512</span><span class="sy0">,</span> <span class="st0">&quot;03&quot;</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
    <span class="kw2">var</span> pubKey <span class="sy0">=</span> hex2b64<span class="br0">&#40;</span>key.<span class="me1">n</span>.<span class="me1">toString</span><span class="br0">&#40;</span><span class="nu0">16</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="kw3">alert</span><span class="br0">&#40;</span>pubKey<span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>

<p>This was a great exercise in how to turn synchronous javascript into asynchronous javascript. Taking procedural code and breaking those for loops into recursive functions was a mind bender but once I figured out how it generally ought to work, each function became easier to port. </p>
<p>Originally I did it all inline, but later I ripped it all out into a separate file which extends Tom Wu&#8217;s jsbn.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="co1">// Copyright (c) 2011  Kevin M Burns Jr.</span>
<span class="co1">// All Rights Reserved.</span>
<span class="co1">// See &quot;LICENSE&quot; for details.</span>
<span class="co1">//</span>
<span class="co1">// Extension to jsbn which adds facilities for asynchronous RSA key generation</span>
<span class="co1">// Primarily created to avoid execution timeout on mobile devices</span>
<span class="co1">//</span>
<span class="co1">// http://www-cs-students.stanford.edu/~tjw/jsbn/</span>
<span class="co1">//</span>
<span class="co1">// ---</span>
&nbsp;
<span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
&nbsp;
<span class="co1">// Generate a new random private key B bits long, using public expt E</span>
<span class="kw2">var</span> RSAGenerateAsync <span class="sy0">=</span> <span class="kw2">function</span> <span class="br0">&#40;</span>B<span class="sy0">,</span> E<span class="sy0">,</span> callback<span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="co1">//var rng = new SeededRandom();</span>
    <span class="kw2">var</span> rng <span class="sy0">=</span> <span class="kw2">new</span> SecureRandom<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="kw2">var</span> qs <span class="sy0">=</span> B <span class="sy0">&gt;&gt;</span> <span class="nu0">1</span><span class="sy0">;</span>
    <span class="kw1">this</span>.<span class="me1">e</span> <span class="sy0">=</span> parseInt<span class="br0">&#40;</span>E<span class="sy0">,</span> <span class="nu0">16</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="kw2">var</span> ee <span class="sy0">=</span> <span class="kw2">new</span> BigInteger<span class="br0">&#40;</span>E<span class="sy0">,</span> <span class="nu0">16</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="kw2">var</span> rsa <span class="sy0">=</span> <span class="kw1">this</span><span class="sy0">;</span>
    <span class="co1">// These functions have non-descript names because they were originally for(;;) loops.</span>
    <span class="co1">// I don't know about cryptography to give them better names than loop1-4.</span>
    <span class="kw2">var</span> loop1 <span class="sy0">=</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="kw2">var</span> loop4 <span class="sy0">=</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            <span class="kw1">if</span> <span class="br0">&#40;</span>rsa.<span class="me1">p</span>.<span class="me1">compareTo</span><span class="br0">&#40;</span>rsa.<span class="me1">q</span><span class="br0">&#41;</span> <span class="sy0">&lt;=</span> <span class="nu0">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                <span class="kw2">var</span> t <span class="sy0">=</span> rsa.<span class="me1">p</span><span class="sy0">;</span>
                rsa.<span class="me1">p</span> <span class="sy0">=</span> rsa.<span class="me1">q</span><span class="sy0">;</span>
                rsa.<span class="me1">q</span> <span class="sy0">=</span> t<span class="sy0">;</span>
            <span class="br0">&#125;</span>
            <span class="kw2">var</span> p1 <span class="sy0">=</span> rsa.<span class="me1">p</span>.<span class="me1">subtract</span><span class="br0">&#40;</span>BigInteger.<span class="me1">ONE</span><span class="br0">&#41;</span><span class="sy0">;</span>
            <span class="kw2">var</span> q1 <span class="sy0">=</span> rsa.<span class="me1">q</span>.<span class="me1">subtract</span><span class="br0">&#40;</span>BigInteger.<span class="me1">ONE</span><span class="br0">&#41;</span><span class="sy0">;</span>
            <span class="kw2">var</span> phi <span class="sy0">=</span> p1.<span class="me1">multiply</span><span class="br0">&#40;</span>q1<span class="br0">&#41;</span><span class="sy0">;</span>
            <span class="kw1">if</span> <span class="br0">&#40;</span>phi.<span class="me1">gcd</span><span class="br0">&#40;</span>ee<span class="br0">&#41;</span>.<span class="me1">compareTo</span><span class="br0">&#40;</span>BigInteger.<span class="me1">ONE</span><span class="br0">&#41;</span> <span class="sy0">==</span> <span class="nu0">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                rsa.<span class="me1">n</span> <span class="sy0">=</span> rsa.<span class="me1">p</span>.<span class="me1">multiply</span><span class="br0">&#40;</span>rsa.<span class="me1">q</span><span class="br0">&#41;</span><span class="sy0">;</span>
                rsa.<span class="me1">d</span> <span class="sy0">=</span> ee.<span class="me1">modInverse</span><span class="br0">&#40;</span>phi<span class="br0">&#41;</span><span class="sy0">;</span>
                rsa.<span class="me1">dmp1</span> <span class="sy0">=</span> rsa.<span class="me1">d</span>.<span class="me1">mod</span><span class="br0">&#40;</span>p1<span class="br0">&#41;</span><span class="sy0">;</span>
                rsa.<span class="me1">dmq1</span> <span class="sy0">=</span> rsa.<span class="me1">d</span>.<span class="me1">mod</span><span class="br0">&#40;</span>q1<span class="br0">&#41;</span><span class="sy0">;</span>
                rsa.<span class="me1">coeff</span> <span class="sy0">=</span> rsa.<span class="me1">q</span>.<span class="me1">modInverse</span><span class="br0">&#40;</span>rsa.<span class="me1">p</span><span class="br0">&#41;</span><span class="sy0">;</span>
                setTimeout<span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>callback<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#125;</span><span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// escape</span>
            <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span>
                setTimeout<span class="br0">&#40;</span>loop1<span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span>
            <span class="br0">&#125;</span>
        <span class="br0">&#125;</span><span class="sy0">;</span>
        <span class="kw2">var</span> loop3 <span class="sy0">=</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            rsa.<span class="me1">q</span> <span class="sy0">=</span> nbi<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
            rsa.<span class="me1">q</span>.<span class="me1">fromNumberAsync</span><span class="br0">&#40;</span>qs<span class="sy0">,</span> <span class="nu0">1</span><span class="sy0">,</span> rng<span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
                rsa.<span class="me1">q</span>.<span class="me1">subtract</span><span class="br0">&#40;</span>BigInteger.<span class="me1">ONE</span><span class="br0">&#41;</span>.<span class="me1">gcda</span><span class="br0">&#40;</span>ee<span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span>r<span class="br0">&#41;</span><span class="br0">&#123;</span>
                    <span class="kw1">if</span> <span class="br0">&#40;</span>r.<span class="me1">compareTo</span><span class="br0">&#40;</span>BigInteger.<span class="me1">ONE</span><span class="br0">&#41;</span> <span class="sy0">==</span> <span class="nu0">0</span> <span class="sy0">&amp;&amp;</span> rsa.<span class="me1">q</span>.<span class="me1">isProbablePrime</span><span class="br0">&#40;</span><span class="nu0">10</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                        setTimeout<span class="br0">&#40;</span>loop4<span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span>
                    <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span>
                        setTimeout<span class="br0">&#40;</span>loop3<span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span>
                    <span class="br0">&#125;</span>
                <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
            <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="br0">&#125;</span><span class="sy0">;</span>
        <span class="kw2">var</span> loop2 <span class="sy0">=</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            rsa.<span class="me1">p</span> <span class="sy0">=</span> nbi<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
            rsa.<span class="me1">p</span>.<span class="me1">fromNumberAsync</span><span class="br0">&#40;</span>B <span class="sy0">-</span> qs<span class="sy0">,</span> <span class="nu0">1</span><span class="sy0">,</span> rng<span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
                rsa.<span class="me1">p</span>.<span class="me1">subtract</span><span class="br0">&#40;</span>BigInteger.<span class="me1">ONE</span><span class="br0">&#41;</span>.<span class="me1">gcda</span><span class="br0">&#40;</span>ee<span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span>r<span class="br0">&#41;</span><span class="br0">&#123;</span>
                    <span class="kw1">if</span> <span class="br0">&#40;</span>r.<span class="me1">compareTo</span><span class="br0">&#40;</span>BigInteger.<span class="me1">ONE</span><span class="br0">&#41;</span> <span class="sy0">==</span> <span class="nu0">0</span> <span class="sy0">&amp;&amp;</span> rsa.<span class="me1">p</span>.<span class="me1">isProbablePrime</span><span class="br0">&#40;</span><span class="nu0">10</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                        setTimeout<span class="br0">&#40;</span>loop3<span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span>
                    <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span>
                        setTimeout<span class="br0">&#40;</span>loop2<span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span>
                    <span class="br0">&#125;</span>
                <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
            <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="br0">&#125;</span><span class="sy0">;</span>
        setTimeout<span class="br0">&#40;</span>loop2<span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="br0">&#125;</span><span class="sy0">;</span>
    setTimeout<span class="br0">&#40;</span>loop1<span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span><span class="sy0">;</span>
RSAKey.<span class="me1">prototype</span>.<span class="me1">generateAsync</span> <span class="sy0">=</span> RSAGenerateAsync<span class="sy0">;</span>
&nbsp;
<span class="co1">// Public API method</span>
<span class="kw2">var</span> bnGCDAsync <span class="sy0">=</span> <span class="kw2">function</span> <span class="br0">&#40;</span>a<span class="sy0">,</span> callback<span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw2">var</span> x <span class="sy0">=</span> <span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">s</span> <span class="sy0">&lt;</span> <span class="nu0">0</span><span class="br0">&#41;</span> <span class="sy0">?</span> <span class="kw1">this</span>.<span class="me1">negate</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">:</span> <span class="kw1">this</span>.<span class="me1">clone</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="kw2">var</span> y <span class="sy0">=</span> <span class="br0">&#40;</span>a.<span class="me1">s</span> <span class="sy0">&lt;</span> <span class="nu0">0</span><span class="br0">&#41;</span> <span class="sy0">?</span> a.<span class="me1">negate</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">:</span> a.<span class="me1">clone</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="kw1">if</span> <span class="br0">&#40;</span>x.<span class="me1">compareTo</span><span class="br0">&#40;</span>y<span class="br0">&#41;</span> <span class="sy0">&lt;</span> <span class="nu0">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="kw2">var</span> t <span class="sy0">=</span> x<span class="sy0">;</span>
        x <span class="sy0">=</span> y<span class="sy0">;</span>
        y <span class="sy0">=</span> t<span class="sy0">;</span>
    <span class="br0">&#125;</span>
    <span class="kw2">var</span> i <span class="sy0">=</span> x.<span class="me1">getLowestSetBit</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">,</span>
        g <span class="sy0">=</span> y.<span class="me1">getLowestSetBit</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="kw1">if</span> <span class="br0">&#40;</span>g <span class="sy0">&lt;</span> <span class="nu0">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        callback<span class="br0">&#40;</span>x<span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="kw1">return</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
    <span class="kw1">if</span> <span class="br0">&#40;</span>i <span class="sy0">&lt;</span> g<span class="br0">&#41;</span> g <span class="sy0">=</span> i<span class="sy0">;</span>
    <span class="kw1">if</span> <span class="br0">&#40;</span>g <span class="sy0">&gt;</span> <span class="nu0">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        x.<span class="me1">rShiftTo</span><span class="br0">&#40;</span>g<span class="sy0">,</span> x<span class="br0">&#41;</span><span class="sy0">;</span>
        y.<span class="me1">rShiftTo</span><span class="br0">&#40;</span>g<span class="sy0">,</span> y<span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
    <span class="co1">// Workhorse of the algorithm, gets called 200 - 800 times per 512 bit keygen.</span>
    <span class="kw2">var</span> gcda1 <span class="sy0">=</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="kw1">if</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>i <span class="sy0">=</span> x.<span class="me1">getLowestSetBit</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="sy0">&gt;</span> <span class="nu0">0</span><span class="br0">&#41;</span><span class="br0">&#123;</span> x.<span class="me1">rShiftTo</span><span class="br0">&#40;</span>i<span class="sy0">,</span> x<span class="br0">&#41;</span><span class="sy0">;</span> <span class="br0">&#125;</span>
        <span class="kw1">if</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>i <span class="sy0">=</span> y.<span class="me1">getLowestSetBit</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="sy0">&gt;</span> <span class="nu0">0</span><span class="br0">&#41;</span><span class="br0">&#123;</span> y.<span class="me1">rShiftTo</span><span class="br0">&#40;</span>i<span class="sy0">,</span> y<span class="br0">&#41;</span><span class="sy0">;</span> <span class="br0">&#125;</span>
        <span class="kw1">if</span> <span class="br0">&#40;</span>x.<span class="me1">compareTo</span><span class="br0">&#40;</span>y<span class="br0">&#41;</span> <span class="sy0">&gt;=</span> <span class="nu0">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            x.<span class="me1">subTo</span><span class="br0">&#40;</span>y<span class="sy0">,</span> x<span class="br0">&#41;</span><span class="sy0">;</span>
            x.<span class="me1">rShiftTo</span><span class="br0">&#40;</span><span class="nu0">1</span><span class="sy0">,</span> x<span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span>
            y.<span class="me1">subTo</span><span class="br0">&#40;</span>x<span class="sy0">,</span> y<span class="br0">&#41;</span><span class="sy0">;</span>
            y.<span class="me1">rShiftTo</span><span class="br0">&#40;</span><span class="nu0">1</span><span class="sy0">,</span> y<span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="br0">&#125;</span>
        <span class="kw1">if</span><span class="br0">&#40;</span><span class="sy0">!</span><span class="br0">&#40;</span>x.<span class="me1">signum</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">&gt;</span> <span class="nu0">0</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            <span class="kw1">if</span> <span class="br0">&#40;</span>g <span class="sy0">&gt;</span> <span class="nu0">0</span><span class="br0">&#41;</span> y.<span class="me1">lShiftTo</span><span class="br0">&#40;</span>g<span class="sy0">,</span> y<span class="br0">&#41;</span><span class="sy0">;</span>
            setTimeout<span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>callback<span class="br0">&#40;</span>y<span class="br0">&#41;</span><span class="br0">&#125;</span><span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// escape</span>
        <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span>
            setTimeout<span class="br0">&#40;</span>gcda1<span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="br0">&#125;</span>
    <span class="br0">&#125;</span><span class="sy0">;</span>
    setTimeout<span class="br0">&#40;</span>gcda1<span class="sy0">,</span><span class="nu0">10</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span><span class="sy0">;</span>
BigInteger.<span class="me1">prototype</span>.<span class="me1">gcda</span> <span class="sy0">=</span> bnGCDAsync<span class="sy0">;</span>
&nbsp;
<span class="co1">// (protected) alternate constructor</span>
<span class="kw2">var</span> bnpFromNumberAsync <span class="sy0">=</span> <span class="kw2">function</span> <span class="br0">&#40;</span>a<span class="sy0">,</span>b<span class="sy0">,</span>c<span class="sy0">,</span>callback<span class="br0">&#41;</span> <span class="br0">&#123;</span>
  <span class="kw1">if</span><span class="br0">&#40;</span><span class="st0">&quot;number&quot;</span> <span class="sy0">==</span> <span class="kw1">typeof</span> b<span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw1">if</span><span class="br0">&#40;</span>a <span class="sy0">&lt;</span> <span class="nu0">2</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="kw1">this</span>.<span class="me1">fromInt</span><span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span>
      <span class="kw1">this</span>.<span class="me1">fromNumber</span><span class="br0">&#40;</span>a<span class="sy0">,</span>c<span class="br0">&#41;</span><span class="sy0">;</span>
      <span class="kw1">if</span><span class="br0">&#40;</span><span class="sy0">!</span><span class="kw1">this</span>.<span class="me1">testBit</span><span class="br0">&#40;</span>a<span class="sy0">-</span><span class="nu0">1</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
        <span class="kw1">this</span>.<span class="me1">bitwiseTo</span><span class="br0">&#40;</span>BigInteger.<span class="me1">ONE</span>.<span class="me1">shiftLeft</span><span class="br0">&#40;</span>a<span class="sy0">-</span><span class="nu0">1</span><span class="br0">&#41;</span><span class="sy0">,</span>op_or<span class="sy0">,</span><span class="kw1">this</span><span class="br0">&#41;</span><span class="sy0">;</span>
      <span class="br0">&#125;</span>
      <span class="kw1">if</span><span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">isEven</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="kw1">this</span>.<span class="me1">dAddOffset</span><span class="br0">&#40;</span><span class="nu0">1</span><span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span>
      <span class="br0">&#125;</span>
      <span class="kw2">var</span> bnp <span class="sy0">=</span> <span class="kw1">this</span><span class="sy0">;</span>
      <span class="kw2">var</span> bnpfn1 <span class="sy0">=</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
        bnp.<span class="me1">dAddOffset</span><span class="br0">&#40;</span><span class="nu0">2</span><span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="kw1">if</span><span class="br0">&#40;</span>bnp.<span class="me1">bitLength</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">&gt;</span> a<span class="br0">&#41;</span> bnp.<span class="me1">subTo</span><span class="br0">&#40;</span>BigInteger.<span class="me1">ONE</span>.<span class="me1">shiftLeft</span><span class="br0">&#40;</span>a<span class="sy0">-</span><span class="nu0">1</span><span class="br0">&#41;</span><span class="sy0">,</span>bnp<span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="kw1">if</span><span class="br0">&#40;</span>bnp.<span class="me1">isProbablePrime</span><span class="br0">&#40;</span>b<span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            setTimeout<span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>callback<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#125;</span><span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// escape</span>
        <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span>
            setTimeout<span class="br0">&#40;</span>bnpfn1<span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="br0">&#125;</span>
      <span class="br0">&#125;</span><span class="sy0">;</span>
      setTimeout<span class="br0">&#40;</span>bnpfn1<span class="sy0">,</span><span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
  <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span>
    <span class="kw2">var</span> x <span class="sy0">=</span> <span class="kw2">new</span> Array<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">,</span> t <span class="sy0">=</span> a<span class="sy0">&amp;</span><span class="nu0">7</span><span class="sy0">;</span>
    x.<span class="me1">length</span> <span class="sy0">=</span> <span class="br0">&#40;</span>a<span class="sy0">&gt;&gt;</span><span class="nu0">3</span><span class="br0">&#41;</span><span class="sy0">+</span><span class="nu0">1</span><span class="sy0">;</span>
    b.<span class="me1">nextBytes</span><span class="br0">&#40;</span>x<span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="kw1">if</span><span class="br0">&#40;</span>t <span class="sy0">&gt;</span> <span class="nu0">0</span><span class="br0">&#41;</span> x<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> <span class="sy0">&amp;=</span> <span class="br0">&#40;</span><span class="br0">&#40;</span><span class="nu0">1</span><span class="sy0">&lt;&lt;</span>t<span class="br0">&#41;</span><span class="sy0">-</span><span class="nu0">1</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="kw1">else</span> x<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span>
    <span class="kw1">this</span>.<span class="me1">fromString</span><span class="br0">&#40;</span>x<span class="sy0">,</span><span class="nu0">256</span><span class="br0">&#41;</span><span class="sy0">;</span>
  <span class="br0">&#125;</span>
<span class="br0">&#125;</span><span class="sy0">;</span>
BigInteger.<span class="me1">prototype</span>.<span class="me1">fromNumberAsync</span> <span class="sy0">=</span> bnpFromNumberAsync<span class="sy0">;</span>
&nbsp;
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>

<p>I&#8217;m also adding a few functions for sleeping a public or private RSA key object to a simple JSON Transport Object (for storage) and waking it again.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
&nbsp;
<span class="co1">// Cast to private Transport Object</span>
<span class="kw2">var</span> RSAPrivTPO <span class="sy0">=</span> <span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw1">return</span> <span class="br0">&#123;</span>
        <span class="st0">'n'</span>     <span class="sy0">:</span> hex2b64<span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">n</span>.<span class="me1">toString</span><span class="br0">&#40;</span><span class="nu0">16</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">,</span>
        <span class="st0">'e'</span>     <span class="sy0">:</span> hex2b64<span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">e</span>.<span class="me1">toString</span><span class="br0">&#40;</span><span class="nu0">16</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">,</span>
        <span class="st0">'d'</span>     <span class="sy0">:</span> hex2b64<span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">d</span>.<span class="me1">toString</span><span class="br0">&#40;</span><span class="nu0">16</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">,</span>
        <span class="st0">'p'</span>     <span class="sy0">:</span> hex2b64<span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">p</span>.<span class="me1">toString</span><span class="br0">&#40;</span><span class="nu0">16</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">,</span>
        <span class="st0">'q'</span>     <span class="sy0">:</span> hex2b64<span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">q</span>.<span class="me1">toString</span><span class="br0">&#40;</span><span class="nu0">16</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">,</span>
        <span class="st0">'dmp1'</span>  <span class="sy0">:</span> hex2b64<span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">dmp1</span>.<span class="me1">toString</span><span class="br0">&#40;</span><span class="nu0">16</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">,</span>
        <span class="st0">'dmq1'</span>  <span class="sy0">:</span> hex2b64<span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">dmq1</span>.<span class="me1">toString</span><span class="br0">&#40;</span><span class="nu0">16</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">,</span>
        <span class="st0">'coeff'</span> <span class="sy0">:</span> hex2b64<span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">coeff</span>.<span class="me1">toString</span><span class="br0">&#40;</span><span class="nu0">16</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
    <span class="br0">&#125;</span>
<span class="br0">&#125;</span>
RSAKey.<span class="me1">prototype</span>.<span class="me1">privTPO</span> <span class="sy0">=</span> RSAPrivTPO<span class="sy0">;</span>
&nbsp;
<span class="co1">// Hydrate from private Transport Object</span>
<span class="kw2">var</span> RSAFromPrivTPO <span class="sy0">=</span> <span class="kw2">function</span> <span class="br0">&#40;</span>tpo<span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw1">this</span>.<span class="me1">setPrivateEx</span><span class="br0">&#40;</span>
        b64tohex<span class="br0">&#40;</span>tpo.<span class="me1">n</span><span class="br0">&#41;</span><span class="sy0">,</span> 
        b64tohex<span class="br0">&#40;</span>tpo.<span class="me1">e</span><span class="br0">&#41;</span><span class="sy0">,</span> 
        b64tohex<span class="br0">&#40;</span>tpo.<span class="me1">d</span><span class="br0">&#41;</span><span class="sy0">,</span> 
        b64tohex<span class="br0">&#40;</span>tpo.<span class="me1">p</span><span class="br0">&#41;</span><span class="sy0">,</span> 
        b64tohex<span class="br0">&#40;</span>tpo.<span class="me1">q</span><span class="br0">&#41;</span><span class="sy0">,</span> 
        b64tohex<span class="br0">&#40;</span>tpo.<span class="me1">dmp1</span><span class="br0">&#41;</span><span class="sy0">,</span> 
        b64tohex<span class="br0">&#40;</span>tpo.<span class="me1">dmq1</span><span class="br0">&#41;</span><span class="sy0">,</span> 
        b64tohex<span class="br0">&#40;</span>tpo.<span class="me1">coeff</span><span class="br0">&#41;</span>
    <span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="kw1">return</span> <span class="kw1">this</span><span class="sy0">;</span>
<span class="br0">&#125;</span><span class="sy0">;</span>
RSAKey.<span class="me1">prototype</span>.<span class="me1">fromPrivTPO</span> <span class="sy0">=</span> RSAFromPrivTPO<span class="sy0">;</span>
&nbsp;
<span class="co1">// Cast to public Transport Object</span>
<span class="kw2">var</span> RSAPubTPO <span class="sy0">=</span> <span class="kw2">function</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw1">return</span> <span class="br0">&#123;</span>
        <span class="st0">'n'</span> <span class="sy0">:</span> hex2b64<span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">n</span>.<span class="me1">toString</span><span class="br0">&#40;</span><span class="nu0">16</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">,</span>
        <span class="st0">'e'</span> <span class="sy0">:</span> hex2b64<span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">e</span>.<span class="me1">toString</span><span class="br0">&#40;</span><span class="nu0">16</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
    <span class="br0">&#125;</span>
<span class="br0">&#125;</span><span class="sy0">;</span>
RSAKey.<span class="me1">prototype</span>.<span class="me1">pubTPO</span> <span class="sy0">=</span> RSAPubTPO<span class="sy0">;</span>
&nbsp;
<span class="co1">// Hydrate from public Transport Object</span>
<span class="kw2">var</span> RSAFromPubTPO <span class="sy0">=</span> <span class="kw2">function</span> <span class="br0">&#40;</span>tpo<span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw1">this</span>.<span class="me1">setPublic</span><span class="br0">&#40;</span>
        b64tohex<span class="br0">&#40;</span>tpo.<span class="me1">n</span><span class="br0">&#41;</span><span class="sy0">,</span> 
        b64tohex<span class="br0">&#40;</span>tpo.<span class="me1">e</span><span class="br0">&#41;</span>
    <span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="kw1">return</span> <span class="kw1">this</span><span class="sy0">;</span>
<span class="br0">&#125;</span><span class="sy0">;</span>
RSAKey.<span class="me1">prototype</span>.<span class="me1">fromPubTPO</span> <span class="sy0">=</span> RSAFromPubTPO<span class="sy0">;</span>
&nbsp;
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>

<p>Source on GitHub &#8211; <a href="https://github.com/KevBurnsJr/rsasync">https://github.com/KevBurnsJr/rsasync</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.kevburnsjr.com/javascript-asynchronous-rsa-key-generation/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Domain specific media types and REST</title>
		<link>http://blog.kevburnsjr.com/domain-specific-media-types-and-rest</link>
		<comments>http://blog.kevburnsjr.com/domain-specific-media-types-and-rest#comments</comments>
		<pubDate>Sun, 07 Aug 2011 22:01:38 +0000</pubDate>
		<dc:creator>KevBurnsJr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.kevburnsjr.com/?p=1836</guid>
		<description><![CDATA[Below is a thread posted to rest-discuss. I&#8217;ve reposted my response here for easy linking and reading.
http://tech.groups.yahoo.com/group/rest-discuss/message/17650
Daniel Roussel posts:

Hi,
I&#8217;ve been reading a lot about how to do &#8220;proper&#8221; REST this week and the more I read, the more I&#8217;m lost, especially the HATEOAS part I fear.
First, to give some context, the company I work for [...]]]></description>
			<content:encoded><![CDATA[<p>Below is a thread posted to rest-discuss. I&#8217;ve reposted my response here for easy linking and reading.<br />
<a href="http://tech.groups.yahoo.com/group/rest-discuss/message/17650">http://tech.groups.yahoo.com/group/rest-discuss/message/17650</a></p>
<p>Daniel Roussel posts:</p>
<div style="white-space: normal; padding: 1em; background: #000; margin: 0 0 1em;">
Hi,</p>
<p>I&#8217;ve been reading a lot about how to do &#8220;proper&#8221; REST this week and the more I read, the more I&#8217;m lost, especially the HATEOAS part I fear.</p>
<p>First, to give some context, the company I work for develops mobile applications for clients. Most of the time, they want to get an iPhone native application, an Android application and a traditional Web based Application to cover the other mobile phones out there.</p>
<p>The way we are currently doing things is the good old (bad?) RPC over HTTP way. We define a bunch of URI which are coded inside the different apps, we exchange data as JSON, etc. This week, trying to do things in a better way, I&#8217;ve begin a more serious study of REST and how to do it properly.</p>
<p>What I really can&#8217;t wrap my head around is how, technically, have HATEOAS in a native application? I mean, when building a native application, I have tables to display lists, buttons to do some things, etc. My understanding is that all those should be displayed based on the data (hypermedia) received from the server. Is that right?</p>
<p>A concrete example would be a hotel room rental service. The person would open the application and have fields to enter the from/to dates. It would then tap a &#8220;Get Available Rooms&#8221;. The app would call the server and get back a list of rooms along with prices and other details. From there the person could select one room and rent it. </p>
<p>The RPC way of coding this is obvious to me but I have no idea how I&#8217;d do that in a proper REST way! What bugs me is that every way I look at it, the client application would still be tightly coupled to the service. I understand how I would only need to GET the http://rent-a-room.com URI hardcoded and then in the response I would have the http://rent-a-room.com/available-rooms URI given. But&#8230; My application would expect each &#8220;call&#8221; to return some pre-defined data and &#8220;rel&#8221;, those can&#8217;t appear out of the blue?! </p>
<p>I guess what I&#8217;m trying to say is that both the business process and the data exchanged must be known to my client application at the moment of coding it, and those can&#8217;t change without breaking existing clients. But reading about REST, every is talking about loose coupling and not breaking clients&#8230; I just don&#8217;t see it.</p>
<p>What am I missing?</p>
<p>Thanks a lot and sorry if it is a stupid question!
</p></div>
<p>Daniel, Here are 3 ways you might use self-descriptive messages in your API</p>
<p><span style="color: #ccc;">1) Create many domain specific media types (one for each view)</span></p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Content-Type: application/rent-a-room+xml</pre></div></div>

<p><span style="color: #ccc;">2) Create one domain specific media type</span></p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Content-Type: application/vnd.hotels.com+xml</pre></div></div>

<p><span style="color: #ccc;">3) Create zero domain specific media types</span></p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Content-Type: application/json
Link: &lt;/schema/rent-a-room&gt;; rel=&quot;describedBy&quot;</pre></div></div>

<p>All three of these approaches could be seen as satisfying the self-descriptive messages constraint.</p>
<p>If you create many DSMs (domain specific media types), your application might bind the media type to the view class via some sort of client-side configuration.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&quot;application/rent-a-room+xml&quot;   =&gt;   RentARoomView</pre></div></div>

<p>If you create one DSM, your media type might specify the semantics by which a representation specifies details about itself which could be used in rendering the representation in a GUI.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">{&quot;_type&quot;: &quot;rent-a-room&quot;, ... }</pre></div></div>

<p>&#8230; which you might then bind to a view &#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&quot;rent-a-room&quot;   =&gt;   RentARoomView</pre></div></div>

<p>If you create zero DSMs, your application might bind the value of the describedBy link header to a view in the gui.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&quot;/schema/rent-a-room&quot;   =&gt;   RentARoomView</pre></div></div>

<p>&#8211;</p>
<p>An alternative approach would be to create one DSM with a richer semantics which would effectively allow you to compose the interface from the server side using code-on-demand and/or more granular views</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">{&quot;_links&quot;:[
    {&quot;rel&quot;:&quot;view&quot;,&quot;type&quot;:&quot;text/javascript&quot;,&quot;href&quot;:&quot;/views/RentARoomView.js&quot;}
    {&quot;rel&quot;:&quot;commentable&quot;,&quot;type&quot;:&quot;text/javascript&quot;,&quot;href&quot;:&quot;/attributes/commentable.js&quot;}
]}</pre></div></div>

<p>This Code-on-demand approach would take greatest advantage of the constraints of REST to create a highly evolvable service by never binding anything directly to a view class within the application. Instead, your application would become a user agent, parsing representations and fetching additional computational resources as necessary to render the view.</p>
<p>Code-on-demand may be significantly less feasible if your client is written in object C, but perhaps it&#8217;s something to think about. The embedded links might not be javascript or CSS, but perhaps some other language used for GUI composition, such as XUL or a simple DSL. </p>
<p>Finally I&#8217;m sure it goes without saying that whatever way you wind up rendering a representation for a view, the UI would contain links which you would click to navigate to new screens which are built using the data and metadata from the representation of the resource identified by the link.</p>
<p>And there you have a few takes on creating an engine of application state with self-descriptive messages and code-on-demand.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.kevburnsjr.com/domain-specific-media-types-and-rest/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Three Line Ternaries</title>
		<link>http://blog.kevburnsjr.com/three-line-ternaries</link>
		<comments>http://blog.kevburnsjr.com/three-line-ternaries#comments</comments>
		<pubDate>Fri, 27 May 2011 00:59:13 +0000</pubDate>
		<dc:creator>KevBurnsJr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.kevburnsjr.com/?p=1750</guid>
		<description><![CDATA[These little buggers are so much more readable than their single-line brethren:

$yourmom&#91;'fat_joke'&#93; = $yourmom&#91;'dinner'&#93; instanceof Elephant
	? &#34;so fat she ate Dumbo&#34; 
	: &#34;so skinny Africa sends her food&#34;;

]]></description>
			<content:encoded><![CDATA[<p>These little buggers are so much more readable than their single-line brethren:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span class="re0">$yourmom</span><span class="br0">&#91;</span><span class="st_h">'fat_joke'</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="re0">$yourmom</span><span class="br0">&#91;</span><span class="st_h">'dinner'</span><span class="br0">&#93;</span> instanceof Elephant
	? <span class="st0">&quot;so fat she ate Dumbo&quot;</span> 
	<span class="sy0">:</span> <span class="st0">&quot;so skinny Africa sends her food&quot;</span><span class="sy0">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.kevburnsjr.com/three-line-ternaries/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Anonymous Functions in Mustache</title>
		<link>http://blog.kevburnsjr.com/anonymous-functions-in-mustache</link>
		<comments>http://blog.kevburnsjr.com/anonymous-functions-in-mustache#comments</comments>
		<pubDate>Wed, 25 May 2011 16:44:48 +0000</pubDate>
		<dc:creator>KevBurnsJr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.kevburnsjr.com/?p=1742</guid>
		<description><![CDATA[When using Anonymous Functions in Mustache, it&#8217;s important to note this line from the spec:
https://github.com/mustache/spec/blob/master/specs/~lambdas.yml#L82

Lambdas used for sections should receive the raw section string.

This means that the inputs to your lambda will not be pre-rendered. The output of the lambda will be rendered.
So I had a lambda that looked like this&#8230;

function&#40;$timestamp&#41; &#123;
	return str_replace&#40;&#34;T&#34;, &#34; &#34;, [...]]]></description>
			<content:encoded><![CDATA[<p>When using Anonymous Functions in Mustache, it&#8217;s important to note this line from the spec:<br />
<a href="https://github.com/mustache/spec/blob/master/specs/~lambdas.yml#L82">https://github.com/mustache/spec/blob/master/specs/~lambdas.yml#L82</a></p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Lambdas used for sections should receive the raw section string.</pre></div></div>

<p>This means that the inputs to your lambda will not be pre-rendered. The output of the lambda will be rendered.</p>
<p>So I had a lambda that looked like this&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span class="kw2">function</span><span class="br0">&#40;</span><span class="re0">$timestamp</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
	<span class="kw1">return</span> <span class="kw3">str_replace</span><span class="br0">&#40;</span><span class="st0">&quot;T&quot;</span><span class="sy0">,</span> <span class="st0">&quot; &quot;</span><span class="sy0">,</span> <span class="re0">$timestamp</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div>

<p>But It was giving jibberish.<br />
<a href="https://github.com/bobthecow">bobthecow</a> (author of Mustache.php) helped me realize that I needed to render the inputs myself&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span class="kw2">function</span><span class="br0">&#40;</span><span class="re0">$timestamp</span><span class="br0">&#41;</span> use<span class="br0">&#40;</span><span class="re0">$view</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
	<span class="kw1">return</span> <span class="kw3">str_replace</span><span class="br0">&#40;</span><span class="st0">&quot;T&quot;</span><span class="sy0">,</span> <span class="st0">&quot; &quot;</span><span class="sy0">,</span> <span class="re0">$view</span><span class="sy0">-&gt;</span><span class="me1">render</span><span class="br0">&#40;</span><span class="re0">$timestamp</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div>

<p>The lesson is this:<br />
The next time I run into a problem, I&#8217;ll be sure to read the tests. </p>
<p>Thanks, Justin!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.kevburnsjr.com/anonymous-functions-in-mustache/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Cloud is not a silver bullet.</title>
		<link>http://blog.kevburnsjr.com/the-cloud-is-not-a-silver-bullet</link>
		<comments>http://blog.kevburnsjr.com/the-cloud-is-not-a-silver-bullet#comments</comments>
		<pubDate>Fri, 01 Apr 2011 17:29:24 +0000</pubDate>
		<dc:creator>KevBurnsJr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.kevburnsjr.com/?p=1730</guid>
		<description><![CDATA[Many decisions are made at the outset of a brand new product/company. Recently I participated in an all-hands meeting to kick off development of a company&#8217;s first product (CEO guy, Product guy, Engineering guy [me]). We talked about the product and the architeture and came around to the infrastructure. It was decided that we would [...]]]></description>
			<content:encoded><![CDATA[<p>Many decisions are made at the outset of a brand new product/company. Recently I participated in an all-hands meeting to kick off development of a company&#8217;s first product (CEO guy, Product guy, Engineering guy [me]). We talked about the product and the architeture and came around to the infrastructure. It was decided that we would use AWS for its handy scalability.</p>
<p>During the conversation, the product guy (who has some tech chops) said something that caught me off guard. His quip was something like: <span style="color: white">&#8220;Cloud is the way to go. We&#8217;re definitely going to do everything in the cloud.&#8221;</span></p>
<p>Below is my lengthy response.</p>
<blockquote><p>Hi Chris,</p>
<p>	You mentioned something in that first meeting which I&#8217;ve been wanting to elaborate on and partially refute.</p>
<p>	I&#8217;d like to caution that The Cloud is not a silver bullet.<br />
	While AWS may be an excellent solution for the immediate future, there may come a point where moving to a datacenter proves to be more reliable and cost-effective.</p>
<p>	There are a number of properties of the infrastructure involved in cloud computing which guarantee that some cloud resources will never match the performance and reliability of bare metal.</p>
<p>	See for instance the recent Reddit outage in which AWS EBS volumes became unavailable for several hours, resulting in serious data problems.<br />
	<a href="http://blog.reddit.com/2011/03/why-reddit-was-down-for-6-of-last-24.html">http://blog.reddit.com/2011/03/why-reddit-was-down-for-6-of-last-24.html</a></p>
<p>	In AWS, the hard drives (EBS) are connected to the machines (EC2) over a shared 1Gb/s ethernet connection.<br />
	In a real server, the hard drives are connected to the machine over a dedicated 6Gb/s SATA cable.</p>
<p>	There are some ways to help mitigate these bottlenecks<br />
	<a href="http://victortrac.com/EC2_Ephemeral_Disks_vs_EBS_Volumes">http://victortrac.com/EC2_Ephemeral_Disks_vs_EBS_Volumes</a></p>
<p>	1) you can set up multiple EBS volumes in a RAID configuration to increase throughput<br />
	2) you can load balance traffic across multiple availability zones to help mitigate network congestion and availability issues like those experienced by Reddit</p>
<p>	But also keep in mind that there will come a point at which renting a cabinet (42U) in a datacenter will become far more cost effective than running a server farm in the cloud.<br />
	A full cabinet with 15Amps of power and a 100Mb/s connection runs $500/mo with a signed 3 year lease.<br />
	A quarter cabinet (7U) with 2Amps of power and a 100Mb/s connection runs $200/mo with a signed 3 year lease.</p>
<p>	So if we took one of these $200 PE 1850s I have sitting here and plugged it in to a quarter cabinet at Hurricane Electric, we would already have more processing power and it would save us $40/mo over our current us-west m1.large instance.</p>
<p>	But there are 2 advantages to the cloud:<br />
	1) If we need more capacity, we can provision it immediately without having to provision hardeware<br />
	2) If we need capacity in a different location, we can easily spin up a cluster in Japan or Europe or the East Coast without having to physically be there.</p>
<p>	In the long term I hope we&#8217;ll come to a hybrid approach where we can<br />
	- leverage the best of real-world datacenters (for backups and heavy number crunching)<br />
	- and leverage the cloud for all its advantages (for geo dispersion, fault tolerance and early stage ramp-up)</p>
<p>	Remember the words of Devops Borat<br />
	<a href="http://twitter.com/DEVOPS_BORAT/status/40816140632596480">http://twitter.com/DEVOPS_BORAT/status/40816140632596480</a></p>
<p>	&#8220;Formation of hybrid cloud: 1) roll in datacenter 2) SHIT! roll to cloud 3) SHIT! roll half back to datacenter.&#8221;</p>
<p>- Kev</p></blockquote>
<p>Thanks, Chris for suggesting I post this to the blog.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.kevburnsjr.com/the-cloud-is-not-a-silver-bullet/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP Fatal Error 500</title>
		<link>http://blog.kevburnsjr.com/php-fatal-error-500</link>
		<comments>http://blog.kevburnsjr.com/php-fatal-error-500#comments</comments>
		<pubDate>Sat, 19 Mar 2011 15:16:53 +0000</pubDate>
		<dc:creator>KevBurnsJr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.kevburnsjr.com/?p=1722</guid>
		<description><![CDATA[How to force PHP to return an empty 500 response when it encounters a fatal error:
Add this to the very beginning and very end of your bootstrap file

register_shutdown_function&#40;function&#40;&#41;&#123;	
	if&#40;!defined&#40;'REQUEST_SUCCEEDED'&#41;&#41; &#123;
		header&#40;&#34;HTTP/1.1 500 Internal Server Error&#34;&#41;;
		if&#40;getenv&#40;'APP_ENV'&#41; != 'dev'&#41; &#123;
			ob_clean&#40;&#41;;
		&#125;
	&#125;
&#125;&#41;;
&#160;
// ...
// ... your whole application ...
// ... from top to bottom ...
// ...
&#160;
define&#40;'REQUEST_SUCCEEDED', true&#41;;

]]></description>
			<content:encoded><![CDATA[<p>How to force PHP to return an empty 500 response when it encounters a fatal error:<br />
Add this to the very beginning and very end of your bootstrap file</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span class="kw3">register_shutdown_function</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>	
	<span class="kw1">if</span><span class="br0">&#40;</span><span class="sy0">!</span><span class="kw3">defined</span><span class="br0">&#40;</span><span class="st_h">'REQUEST_SUCCEEDED'</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
		<span class="kw3">header</span><span class="br0">&#40;</span><span class="st0">&quot;HTTP/1.1 500 Internal Server Error&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
		<span class="kw1">if</span><span class="br0">&#40;</span><span class="kw3">getenv</span><span class="br0">&#40;</span><span class="st_h">'APP_ENV'</span><span class="br0">&#41;</span> <span class="sy0">!=</span> <span class="st_h">'dev'</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
			<span class="kw3">ob_clean</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
		<span class="br0">&#125;</span>
	<span class="br0">&#125;</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="co1">// ...</span>
<span class="co1">// ... your whole application ...</span>
<span class="co1">// ... from top to bottom ...</span>
<span class="co1">// ...</span>
&nbsp;
<span class="kw3">define</span><span class="br0">&#40;</span><span class="st_h">'REQUEST_SUCCEEDED'</span><span class="sy0">,</span> <span class="kw2">true</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.kevburnsjr.com/php-fatal-error-500/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to prevent a leak like Tumblr&#8217;s</title>
		<link>http://blog.kevburnsjr.com/how-to-prevent-a-leak-like-tumblrs</link>
		<comments>http://blog.kevburnsjr.com/how-to-prevent-a-leak-like-tumblrs#comments</comments>
		<pubDate>Sat, 19 Mar 2011 13:43:37 +0000</pubDate>
		<dc:creator>KevBurnsJr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.kevburnsjr.com/?p=1690</guid>
		<description><![CDATA[Tumblr took a bit of a tumble today.
http://news.ycombinator.com/item?id=2343330
One of Tumblr&#8217;s engineers (presumably) deployed a file to production which contained a critical flaw.
The first character of the file was replaced with an `i` instead of a `]]></description>
			<content:encoded><![CDATA[<p>Tumblr took a bit of a tumble today.<br />
<a href="http://news.ycombinator.com/item?id=2343330">http://news.ycombinator.com/item?id=2343330</a></p>
<p>One of Tumblr&#8217;s engineers (presumably) deployed a file to production which contained a critical flaw.<br />
The first character of the file was replaced with an `i` instead of a `<`.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">i?php
    <span class="kw1">require_once</span><span class="br0">&#40;</span><span class="st_h">'chorus/Utils.php'</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="kw1">require_once</span><span class="br0">&#40;</span><span class="st_h">'chorus/Kestrel.php'</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="kw1">require_once</span><span class="br0">&#40;</span><span class="st_h">'chorus/DataService.php'</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="kw1">require_once</span><span class="br0">&#40;</span><span class="st_h">'chorus/Shard.php'</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
    Database<span class="sy0">::</span><span class="me2">set_defaults</span><span class="br0">&#40;</span><span class="kw3">array</span><span class="br0">&#40;</span>
        <span class="st_h">'user'</span>     <span class="sy0">=&gt;</span> <span class="st_h">'tumblr3'</span><span class="sy0">,</span>
        <span class="st_h">'password'</span> <span class="sy0">=&gt;</span> <span class="st_h">'m3MpH1C0Koh39AQD83TFhsBPlOM1Rx9eW55Z8YWStbgTmcgQWJvFt4'</span><span class="sy0">,</span>
        <span class="st_h">'database'</span> <span class="sy0">=&gt;</span> <span class="st_h">'tumblr3'</span><span class="sy0">,</span>
    <span class="co1">//    'write_lock_tables' =&gt; '*',</span>
        <span class="st_h">'extended_log'</span> <span class="sy0">=&gt;</span> <span class="br0">&#40;</span>idate<span class="br0">&#40;</span><span class="st_h">'G'</span><span class="br0">&#41;</span> <span class="sy0">==</span> <span class="nu0">17</span> <span class="sy0">&amp;&amp;</span> <span class="kw3">intval</span><span class="br0">&#40;</span>idate<span class="br0">&#40;</span><span class="st_h">'i'</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="sy0">==</span> <span class="nu0">56</span> <span class="sy0">&amp;&amp;</span> <span class="kw3">trim</span><span class="br0">&#40;</span>`hostname`<span class="br0">&#41;</span> <span class="sy0">==</span> <span class="st_h">'web10.tumblr.com'</span><span class="br0">&#41;</span>
    <span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
    <span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw2">__FILE__</span> <span class="sy0">==</span> <span class="st_h">'/var/www/apps/tumblr/config/config.php'</span> <span class="sy0">||</span> <span class="kw2">__FILE__</span> <span class="sy0">==</span> <span class="st_h">'/data/tumblr/config/config.php'</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="kw3">define</span><span class="br0">&#40;</span><span class="st_h">'ENVIRONMENT'</span><span class="sy0">,</span>      <span class="st_h">'production'</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="kw1">if</span> <span class="br0">&#40;</span><span class="sy0">!</span> <span class="kw3">defined</span><span class="br0">&#40;</span><span class="st_h">'DEFAULT_DATABASE'</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="kw3">define</span><span class="br0">&#40;</span><span class="st_h">'DEFAULT_DATABASE'</span><span class="sy0">,</span> <span class="st_h">'primary'</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="kw3">define</span><span class="br0">&#40;</span><span class="st_h">'S3_BUCKET'</span><span class="sy0">,</span>        <span class="st_h">'data.tumblr.com'</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="kw3">define</span><span class="br0">&#40;</span><span class="st_h">'ENABLE_PANTHER'</span><span class="sy0">,</span>   <span class="kw2">true</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="kw3">define</span><span class="br0">&#40;</span><span class="st_h">'ENABLE_MEDIA_CDN'</span><span class="sy0">,</span> <span class="kw2">true</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="kw3">define</span><span class="br0">&#40;</span><span class="st_h">'ASSETS_URL'</span><span class="sy0">,</span>       <span class="br0">&#40;</span>ENABLE_MEDIA_CDN <span class="sy0">&amp;&amp;</span> <span class="sy0">!</span> <span class="br0">&#40;</span><span class="kw3">isset</span><span class="br0">&#40;</span><span class="re0">$_SERVER</span><span class="br0">&#91;</span><span class="st_h">'HTTPS'</span><span class="br0">&#93;</span><span class="br0">&#41;</span> <span class="sy0">&amp;&amp;</span> <span class="re0">$_SERVER</span><span class="br0">&#91;</span><span class="st_h">'HTTPS'</span><span class="br0">&#93;</span><span class="br0">&#41;</span> ? <span class="st_h">'http://assets.tumblr.com'</span> <span class="sy0">:</span> <span class="st_h">''</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="kw3">define</span><span class="br0">&#40;</span><span class="st_h">'MEMCACHE_HOST'</span><span class="sy0">,</span>         <span class="st_h">'10.252.0.68'</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="kw3">define</span><span class="br0">&#40;</span><span class="st_h">'MEMCACHE_VERSION_HOST'</span><span class="sy0">,</span> <span class="st_h">'10.252.0.67'</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="kw3">define</span><span class="br0">&#40;</span><span class="st_h">'VALIDATION_FAILURE_LOG'</span><span class="sy0">,</span> BASE_PATH <span class="sy0">.</span> <span class="st_h">'/validate.log'</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="co2"># &lt;snip&gt;</span></pre></div></div>

<p>Yes, that is tumblr&#8217;s production database password.</p>
<p>Full source <a href="http://pastebin.com/aPQJUh1Q">here</a></p>
<p>Once this error was introduced to production, people viewing any page would see a dump of the first 749 lines of the config file along with some PHP errors.  THEN, GoogleBot came along and indexed the whole mess which is why you can still see it in <a href="http://www.google.com/search?q=site%3Atumblr.com+m3MpH1C0Koh39AQD83TFhsBPlOM1Rx9eW55Z8YWStbgTmcgQWJvFt4">Google&#8217;s search results</a></p>
<h3 style="color: #fff;">Learning from the mistakes of others</h3>
<p>How can we keep this from happening to us?</p>
<p>First of all, go to any project on your local dev environment, replace the first `<` with `i` in any included file in your project (hint config.php) and see what happens. Chances are you'll see exactly the same thing that happened to Tumblr.</p>
<p>The only solution I see to this is pre-commit syntax checking for committed PHP files.  </p>
<p>Here's <a href="http://www.phpntips.com/svn-pre-commit-to-check-php-syntax-and-codesniffer-2010-08/">a tutorial for php syntax checking in SVN</a> and here&#8217;s <a href="https://github.com/phpbb/phpbb3/blob/develop-olympus/git-tools/hooks/pre-commit">a pre-commit hook script for php syntax checking in GIT</a>.</p>
<p>Basically the way it works is that if you ever commit a PHP file which contains a syntax error, your commit will be blocked and you will have to amend it before you are allowed to commit it to the repository. If Tumblr had done this, they might never have leaked their config file.</p>
<p><span style="color:white">[UDPATE]</span> Apparently both PHP&#8217;s built-in Syntax Checking and the PEAR package PHP_CodeSniffer won&#8217;t pick up on errors such as this. Searching for a valid solution now&#8230;</p>
<p><span style="color:white">[UDPATE]</span> The best solution I&#8217;ve seen so far is to always return 500 responses to clients with a generic error message in production, thus preventing errors like this from bubbling up at the Apache level.</p>
<p><span style="color:white">[UDPATE]</span> Turns out PHP returns 200 when it encounters a fatal error. Inconceivable.</p>
<p><span style="color:white">[UDPATE]</span> Here&#8217;s how to force PHP to return a 500 when it encounters a fatal error. Add this to a prepend file or make it the first 5 lines of your bootstrap file.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span class="kw2">function</span> die_with_honor<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
	<span class="kw3">header</span><span class="br0">&#40;</span><span class="st0">&quot;HTTP/1.1 500 Internal Server Error&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
	<span class="kw3">ob_clean</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span>
<span class="kw3">register_shutdown_function</span><span class="br0">&#40;</span><span class="st_h">'die_with_honor'</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.kevburnsjr.com/how-to-prevent-a-leak-like-tumblrs/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>A letter from momtrepreneur</title>
		<link>http://blog.kevburnsjr.com/a-letter-from-momtrepreneur</link>
		<comments>http://blog.kevburnsjr.com/a-letter-from-momtrepreneur#comments</comments>
		<pubDate>Mon, 28 Feb 2011 14:49:36 +0000</pubDate>
		<dc:creator>KevBurnsJr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.kevburnsjr.com/?p=1612</guid>
		<description><![CDATA[Hi, its me again ;-).
I was thinking about what you said about focusing on your stuff in the US.  I was driving along the road to Supetar today, passing the olive orchards and thinking of you.
I was thinking about how one&#8217;s 20s are the time for learning and exploring, honing skills and interests, finding [...]]]></description>
			<content:encoded><![CDATA[<blockquote style="margin-left: 0; font-size: 1.2em;"><p>Hi, its me again ;-).</p>
<p>I was thinking about what you said about focusing on your stuff in the US.  I was driving along the road to Supetar today, passing the olive orchards and thinking of you.</p>
<p>I was thinking about how one&#8217;s 20s are the time for learning and exploring, honing skills and interests, finding out what you like and don&#8217;t like, learning what gives you satisfaction and a sense of purpose &#8211; for your professional and personal life.  One&#8217;s 30s are an important time to focus what you have learned and build the foundation for life.  I think that means choosing how you want to work and what you want to do as well as building long lasting professional, personal and family relationships.   </p>
<p>In my 20s I worked in a lot of areas and learned as much as I could technically as well as about business, constantly challenging myself to try new things until I found what I was best at and what gave me the most sense of satisfaction and purpose.  It took a long time ;-).  I must have taken every type of class offered at school, just for the enjoyment of it.  Once I discovered the computer industry, there was an endless supply of things to learn, which I had never dreamed of.  I hadn&#8217;t even seen a computer ever when I started my first computer business.  I was stunned by the possibility of it when I went to my first computer club meetings where they were talking about the Apple I computer and Local Area Networks (ethernet to be specific).  </p>
<p>I also learned after having you that I wanted to have as much control and flexibility over my time so I could focus on you children and the family.  This is why I went into consulting.  I felt I could have rewarding work, would learn a lot from working with many companies, but also not be overwhelmed by the demands of a job where someone else decided when and where.  I think I built a very good foundation in my 30s for this and was able in my 40s to have even more flexibility and further develop my business capabilities.  Of course, I would have liked to have spent even more time at home because once I focused on business, the demands from clients made it increasingly difficult to manage my time.  The conundrum of balancing work, getting new business and family life was much harder than I thought it would be.</p>
<p>The first time I tried consulting on my own (right after I had Deirdre), I was unsuccessful.  I didn&#8217;t want to go back to work with the two of you at home.  I was 30.  But it turned out I didn&#8217;t have the contacts or ability to get clients, so I didn&#8217;t have much work &#8211; we had invented email on the PC and Macintosh, but nobody really used it.  And the web had not been commercialized, so the only people that used it were universities and research companies like SRI.  </p>
<p>I was really lucky that a friend of mine who worked at Apple called me and asked if I wanted to work on a project for Apple because one of the consultants that was working on it quit right in the middle and had not done any of the work.  It turns out he didn&#8217;t know what to do.  So, I said yes and wound up starting a consulting firm with the 3 people who were working on the project together. . . so many serendipitous events, which really shaped my life.  I think what I learned from it was that in order to get to a point where I could do what I wanted, I had to have some foundations put into place.  About 5 years later, I was able to start my own firm, quite successfully &#8211; I had learned how to run a consulting business and I had the network to get work.</p>
<p>We haven&#8217;t spoken in a long time about what you want and what you are thinking about, I am very interested in your thoughts and feelings about what you are doing.</p>
<p>Lots of love,</p>
<p>Mom</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://blog.kevburnsjr.com/a-letter-from-momtrepreneur/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mixing it up</title>
		<link>http://blog.kevburnsjr.com/mixing-it-up</link>
		<comments>http://blog.kevburnsjr.com/mixing-it-up#comments</comments>
		<pubDate>Sun, 27 Feb 2011 23:51:03 +0000</pubDate>
		<dc:creator>KevBurnsJr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.kevburnsjr.com/?p=1618</guid>
		<description><![CDATA[I have 6 books on my nightstand, each on a different topic:


Lean Thinking
process engineering


Metaphors We Live By
role of language in psychology


Just Enough Software Architecture
software architecture


Drive
psychology of motivation


Web Operations
operations


Leading Geeks
management


Programming Erlang
programming


This gives me the opportunity to jump around to follow whatever impulse is fueling me at any given time.  If I&#8217;m feeling burned after working [...]]]></description>
			<content:encoded><![CDATA[<p>I have 6 books on my nightstand, each on a different topic:</p>
<table>
<tr>
<td><a href="http://www.amazon.com/gp/product/0743249275?ie=UTF8&#038;tag=kevbcom-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0743249275">Lean Thinking</a><img src="http://www.assoc-amazon.com/e/ir?t=kevbcom-20&#038;l=as2&#038;o=1&#038;a=0743249275" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /></td>
<td>process engineering</td>
</tr>
<tr>
<td><a href="http://www.amazon.com/gp/product/0226468011?ie=UTF8&#038;tag=kevbcom-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0226468011">Metaphors We Live By</a><img src="http://www.assoc-amazon.com/e/ir?t=kevbcom-20&#038;l=as2&#038;o=1&#038;a=0226468011" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important; display: inline;" /></td>
<td>role of language in psychology</td>
</tr>
<tr>
<td><a href="http://www.amazon.com/gp/product/0984618104?ie=UTF8&#038;tag=kevbcom-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0984618104">Just Enough Software Architecture</a><img src="http://www.assoc-amazon.com/e/ir?t=kevbcom-20&#038;l=as2&#038;o=1&#038;a=0984618104" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /></td>
<td>software architecture</td>
</tr>
<tr>
<td><a href="http://www.amazon.com/gp/product/1594488843?ie=UTF8&#038;tag=kevbcom-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=1594488843">Drive</a><img src="http://www.assoc-amazon.com/e/ir?t=kevbcom-20&#038;l=as2&#038;o=1&#038;a=1594488843" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /></td>
<td>psychology of motivation</td>
</tr>
<tr>
<td><a href="http://www.amazon.com/gp/product/1449377440?ie=UTF8&#038;tag=kevbcom-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=1449377440">Web Operations</a><img src="http://www.assoc-amazon.com/e/ir?t=kevbcom-20&#038;l=as2&#038;o=1&#038;a=1449377440" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /></td>
<td>operations</td>
</tr>
<tr>
<td><a href="http://www.amazon.com/gp/product/0787961485?ie=UTF8&#038;tag=kevbcom-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0787961485">Leading Geeks</a><img src="http://www.assoc-amazon.com/e/ir?t=kevbcom-20&#038;l=as2&#038;o=1&#038;a=0787961485" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /></td>
<td>management</td>
</tr>
<tr>
<td><a href="http://www.amazon.com/gp/product/193435600X?ie=UTF8&#038;tag=kevbcom-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=193435600X">Programming Erlang</a><img src="http://www.assoc-amazon.com/e/ir?t=kevbcom-20&#038;l=as2&#038;o=1&#038;a=193435600X" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /></td>
<td>programming</td>
</tr>
</table>
<p>This gives me the opportunity to jump around to follow whatever impulse is fueling me at any given time.  If I&#8217;m feeling burned after working around some bogus proprietary API, I will jump into a book on Software Architecture to remind me that there is still a path toward sanity. If I&#8217;ve been teching out for days and feel like I&#8217;ve lost the forest for trees, I&#8217;ll pick up a book on psychology to remind me that software does still have the potential to assist human beings in a large number of not-necessarily-obvious ways. If I&#8217;ve spent all day writing emails and haven&#8217;t had time to touch a piece of code, I&#8217;ll jump into a book on process or management to give me new ideas on how to talk less and do more. If I&#8217;m feeling adventurous, I&#8217;ll pick up a book on a new language and type along for a few hours to advance my understanding of the universe.</p>
<p>And I do the same thing with projects:</p>
<table>
<tr>
<td><a href="http://teambo.hackyhack.net">Teambo</a></td>
<td>A generic ticket tracking application</td>
</tr>
<tr>
<td><a href="http://torophp.net">ToroPHP</a></td>
<td>A PHP Framework</td>
</tr>
<tr>
<td><a href="http://ripple-php.hackyhack.net/test/">ripple-php</a></td>
<td>A Riak ODM for PHP (ported from Ruby)</td>
</tr>
<tr>
<td><a href="http://riak-php-client_protobuf.hackyhack.net/unit_tests_protobuf.php">riak-php-client</a></td>
<td>Riak&#8217;s PHP client which I&#8217;m extending to include support for Protocol Buffers</td>
</tr>
</table>
<p>If I&#8217;m burnt on churning out HTML and CSS for days, I&#8217;ll step back and jump into something esoteric like adding a new transport protocol to an open source database client. If I&#8217;ve been crunching on a hard CS problem and start lacking steam, I&#8217;ll jump over to an application I&#8217;ve got on the back burner and knock out a few quick features to get the gears turning again. If I&#8217;m losing sleep over a cool new use case I&#8217;ve been turning over, I&#8217;ll flip on the lights and crank out a few test cases to put my ideas on paper as fodder for tomorrow&#8217;s session.</p>
<p>This sort of autonomy is crucial to remaining happy and productive.</p>
<p><span style="color: #fff; font-size: 1.7em; font-weight: normal;">Plant a thousand projects and let them flourish as they may.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.kevburnsjr.com/mixing-it-up/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pretty JSON : Pipe to pj</title>
		<link>http://blog.kevburnsjr.com/pretty-json-pipe-to-pj</link>
		<comments>http://blog.kevburnsjr.com/pretty-json-pipe-to-pj#comments</comments>
		<pubDate>Fri, 25 Feb 2011 02:28:10 +0000</pubDate>
		<dc:creator>KevBurnsJr</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.kevburnsjr.com/?p=1563</guid>
		<description><![CDATA[&#160;&#160;&#160;&#160;google image search for&#160;&#160;&#160;&#160;pipe and pjs

If you wind up doing a lot of curl from the command line (like you will using Riak),  add this line to the bottom of ~/.bashrc

# ...
alias pj='python -mjson.tool'

Now when you&#8217;re curling, you can just pipe curl output to pj:

$ curl -s http://localhost:8098/riak/stats &#124; pj

And this &#8230;

{&#34;props&#34;:{&#34;name&#34;:&#34;stats&#34;,&#34;n_val&#34;:3,&#34;allow_mult&#34;:false,
&#34;last_write_wins&#34;:false,&#34;precommit&#34;:[],&#34;postcommit&#34;:[]
,&#34;chash_keyfun&#34;:{&#34;mod&#34;:&#34;riak_core_util&#34;,&#34;fun&#34;:&#34;chash_s
td_keyfun&#34;},&#34;linkfun&#34;:{&#34;mod&#34;:&#34;riak_kv_wm_link_walker&#34;,
&#34;fun&#34;:&#34;mapreduce_linkfun&#34;},&#34;old_vclock&#34;:86400,&#34;young_v
clock&#34;:20,&#34;big_vclock&#34;:50,&#34;small_vclock&#34;:10,&#34;r&#34;:&#34;quoru
m&#34;,&#34;w&#34;:&#34;quorum&#34;,&#34;dw&#34;:&#34;quorum&#34;,&#34;rw&#34;:&#34;quorum&#34;}}

Becomes this &#8230;

{
 [...]]]></description>
			<content:encoded><![CDATA[<p style="float: right; padding: 0 25px; text-align: center; color: #444;"><img src="http://d.kevburnsjr.com/hef.jpg" /><br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;google image search for<br/>&nbsp;&nbsp;&nbsp;&nbsp;pipe and pjs</p>
<div style="float: left; width: 470px;">
<p>If you wind up doing a lot of curl from the command line (like you will using <a href="http://wiki.basho.com/">Riak</a>),  add this line to the bottom of <span style="color: #fff; font-weight: bold;">~/.bashrc</span></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span class="co0"># ...</span>
<span class="kw3">alias</span> <span class="re2">pj</span>=<span class="st_h">'python -mjson.tool'</span></pre></div></div>

<p>Now when you&#8217;re curling, you can just pipe curl output to pj:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ curl <span class="re5">-s</span> http:<span class="sy0">//</span>localhost:<span class="nu0">8098</span><span class="sy0">/</span>riak<span class="sy0">/</span>stats <span class="sy0">|</span> pj</pre></div></div>

<p>And this &#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">{&quot;props&quot;:{&quot;name&quot;:&quot;stats&quot;,&quot;n_val&quot;:3,&quot;allow_mult&quot;:false,
&quot;last_write_wins&quot;:false,&quot;precommit&quot;:[],&quot;postcommit&quot;:[]
,&quot;chash_keyfun&quot;:{&quot;mod&quot;:&quot;riak_core_util&quot;,&quot;fun&quot;:&quot;chash_s
td_keyfun&quot;},&quot;linkfun&quot;:{&quot;mod&quot;:&quot;riak_kv_wm_link_walker&quot;,
&quot;fun&quot;:&quot;mapreduce_linkfun&quot;},&quot;old_vclock&quot;:86400,&quot;young_v
clock&quot;:20,&quot;big_vclock&quot;:50,&quot;small_vclock&quot;:10,&quot;r&quot;:&quot;quoru
m&quot;,&quot;w&quot;:&quot;quorum&quot;,&quot;dw&quot;:&quot;quorum&quot;,&quot;rw&quot;:&quot;quorum&quot;}}</pre></div></div>

<p>Becomes this &#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">{
    &quot;props&quot;: {
        &quot;allow_mult&quot;: false,
        &quot;big_vclock&quot;: 50,
        &quot;chash_keyfun&quot;: {
            &quot;fun&quot;: &quot;chash_std_keyfun&quot;,
            &quot;mod&quot;: &quot;riak_core_util&quot;
        },
        &quot;dw&quot;: &quot;quorum&quot;,
        &quot;last_write_wins&quot;: false,
        &quot;linkfun&quot;: {
            &quot;fun&quot;: &quot;mapreduce_linkfun&quot;,
            &quot;mod&quot;: &quot;riak_kv_wm_link_walker&quot;
        },
        &quot;n_val&quot;: 3,
        &quot;name&quot;: &quot;stats&quot;,
        &quot;old_vclock&quot;: 86400,
        &quot;postcommit&quot;: [],
        &quot;precommit&quot;: [],
        &quot;r&quot;: &quot;quorum&quot;,
        &quot;rw&quot;: &quot;quorum&quot;,
        &quot;small_vclock&quot;: 10,
        &quot;w&quot;: &quot;quorum&quot;,
        &quot;young_vclock&quot;: 20
    }
}</pre></div></div>

</div>
<div style="clear:both;"></div>
]]></content:encoded>
			<wfw:commentRss>http://blog.kevburnsjr.com/pretty-json-pipe-to-pj/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

