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

<channel>
	<title>BiteCode - Programming and Web Design Blog</title>
	<atom:link href="http://bitecode.co.uk/feed/" rel="self" type="application/rss+xml" />
	<link>http://bitecode.co.uk</link>
	<description></description>
	<pubDate>Tue, 15 Jul 2008 14:36:54 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	<language>en</language>
			<item>
		<title>My Views on Python</title>
		<link>http://bitecode.co.uk/2008/07/my-views-on-python/</link>
		<comments>http://bitecode.co.uk/2008/07/my-views-on-python/#comments</comments>
		<pubDate>Tue, 15 Jul 2008 14:36:54 +0000</pubDate>
		<dc:creator>bitecode</dc:creator>
		
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://bitecode.co.uk/?p=21</guid>
		<description><![CDATA[About a year ago, before I had been properly introduced to Python, I was of the opinion that back-end website programming should be done in PHP, and that applications should be coded in C++. I hadn&#8217;t at this stage had any kind of Computing Science or programming tuition, nor had I attempted much more than [...]<script type="text/javascript">SHARETHIS.addEntry({ title: "My Views on Python", url: "http://bitecode.co.uk/2008/07/my-views-on-python/" });</script>]]></description>
			<content:encoded><![CDATA[<p>About a year ago, before I had been properly introduced to Python, I was of the opinion that back-end website programming should be done in PHP, and that applications should be coded in C++. I hadn&#8217;t at this stage had any kind of Computing Science or programming tuition, nor had I attempted much more than ‘hobby’ programming. When I arrived at university to study Computer Science, I was told that Python was the language that we were going to be taught in. All I knew about Python was that it could be used for CGI programming, and that it had what I considered to be odd syntax. Anyhow, after it received the whole-hearted endorsement of my lecturers I decided that I wanted learn it properly and see what all the fuss was about. 10 months on I program almost exclusively in Python; using PHP, my former ‘main’ language, makes me feel awkward and limited. Here, I present the reasons why (at least for the jobs it is suited to) Python is now my language of choice.</p>
<h3>Syntax</h3>
<p>The ‘unconventional’ syntax was one of the main reasons I didn&#8217;t look at Python earlier. I was used to languages with C-style syntax (C, C++, PHP, Javascript, etc.). Now, I consider the syntax to be one of the things I regard most highly about Python. Readability is a big issue in computer programming, especially if you are releasing code into the Public domain. I have found Python code to be much more instantly understandable and readable than any other language I have used. The indentation-based code blocks ensure that programmers lay out their code in a readable fashion, and also looks cleaner than C-style languages, which are littered with braces. </p>
<p>The syntax being so simple and so close to natural language makes Python easier to learn for beginners; it&#8217;s perfect for teaching the basics of programming to people who have never programmed before. Python still allows you to saturate your code with parentheses and cryptic variable names if that&#8217;s what you really like to do, but the basic syntax is much cleaner and certainly no less powerful than that of other languages. I also like Python&#8217;s multi-paradigm nature - it may be used as a simple imperative language, as an object-oriented language, or as a functional language.</p>
<h3>Standard Library</h3>
<p>Python&#8217;s ‘batteries included’ philosophy means that it has a rich and powerful standard library. Everything from regular expressions, to web protocol handling (httplib/smtplib etc.), to cryptographic hashing, to database connectivity is included. If something is missing, there will probably already be a third-party library available, which can be found with a simple Google search.</p>
<h3>Speed</h3>
<p>This may seem like an odd one - I&#8217;ve mentioned before that Python is not particularly fast and thus not necessarily suited to computationally expensive tasks; I stick by those claims. However, if you compare Python&#8217;s speed to other languages in the same class (‘scripting’ languages such as PHP, Perl, Ruby, etc.), it actually does pretty well. Benchmarks from the <a href="http://shootout.alioth.debian.org/gp4">Computer Language Benchmark Game website</a> show that Python (just vanilla CPython, not PyPy or CPython+Psyco etc.) generally performs better than PHP and Perl, and knocks Ruby out of the water. Admittedly Lua seems to be faster than Python, but that&#8217;s only really used for game scripting. On top of this, Psyco, the fantastic JIT compiler I&#8217;ve mentioned in previous posts, can allegedly speed up Python by as much as 100x! If this still isn&#8217;t quick enough, functions may be coded in C and compiled and imported as a module, which brings me on to my final topic:</p>
<h3>Extensibility</h3>
<p>As well as having fantastic standard library, Python is hugely extensible. Extension modules may easily be hand coded in C or C++ to eliminate bottlenecks, or bindings for entire C/C++ libraries can easily be created with SWIG or ctypes. A fantastic example of one such library is PyOpenGL, a Python wrapper around OpenGL, created using ctypes. It allows all the really hard work to be done in C and by the GPU, while allowing the flexibility of Python to used. Python may also be easily embedded in C and C++ applications and used as a scripting language (e.g. 3D software such as Maya).</p>
<p>I don&#8217;t think of Python as a perfect language - it isn&#8217;t right for every job and I couldn&#8217;t see myself only ever using Python, but I find programming in it more satisfying, rewarding and productive than in any of the other languages I&#8217;ve used.</p>
<p><a href="http://sharethis.com/item?&wp=2.5.1&amp;publisher=b8e8e47b-e8f1-4814-bb9c-d1bf2397550a&amp;title=My+Views+on+Python&amp;url=http%3A%2F%2Fbitecode.co.uk%2F2008%2F07%2Fmy-views-on-python%2F">ShareThis</a></p>]]></content:encoded>
			<wfw:commentRss>http://bitecode.co.uk/2008/07/my-views-on-python/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Generating Content with Markov Chains</title>
		<link>http://bitecode.co.uk/2008/07/generating-content-with-markov-chains/</link>
		<comments>http://bitecode.co.uk/2008/07/generating-content-with-markov-chains/#comments</comments>
		<pubDate>Tue, 15 Jul 2008 10:29:45 +0000</pubDate>
		<dc:creator>bitecode</dc:creator>
		
		<category><![CDATA[Python]]></category>

		<category><![CDATA[Web Development]]></category>

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

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

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

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

		<guid isPermaLink="false">http://bitecode.co.uk/?p=16</guid>
		<description><![CDATA[Reading up on Markov Chains opened my eyes to a way of generating content automatically, based on a probabilistic model. The content generation program would be able to produce pieces of text of variable (user-defined) lengths, that were semi-comprehensible. The program would first go through a training phase, where it is given the document(s) that [...]<script type="text/javascript">SHARETHIS.addEntry({ title: "Generating Content with Markov Chains", url: "http://bitecode.co.uk/2008/07/generating-content-with-markov-chains/" });</script>]]></description>
			<content:encoded><![CDATA[<p>Reading up on Markov Chains opened my eyes to a way of generating content automatically, based on a probabilistic model. The content generation program would be able to produce pieces of text of variable (user-defined) lengths, that were semi-comprehensible. The program would first go through a training phase, where it is given the document(s) that it were to base its generated text around. For every word in the document(s) the program would record the following word.</p>
<p>When the text was to be generated, the program would pick a random starting word, then produce the following words by making a (weighted) random selection of the words that followed the current word in the training document(s) (based on the data recorded in the training phase). The resulting text wouldn&#8217;t make sense to a reader, but would appear at a first glance to make much more sense than just a collection of random words. One possible use for this kind of text would be for pages wishing to achieve a high PageRank in order to raise their advertising revenue (effectively spam pages that appear high up on Google - so don&#8217;t actually do it!).</p>
<p>I&#8217;m sure there are plenty of other implementations of this kind of program, but I&#8217;ve written my own basic one in Python. It&#8217;s not perfect, but it gets the job done.</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">collections</span> <span style="color: #ff7700;font-weight:bold;">import</span> defaultdict
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">random</span> <span style="color: #ff7700;font-weight:bold;">import</span> choice
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> TextGenerator<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>._data = defaultdict<span style="color: black;">&#40;</span><span style="color: #008000;">list</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> train<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #008000;">file</span><span style="color: black;">&#41;</span>:
        words = <span style="color: black;">&#91;</span><span style="color: #008000;">None</span>, <span style="color: #008000;">None</span><span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> line <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #008000;">file</span><span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">for</span> word <span style="color: #ff7700;font-weight:bold;">in</span> line.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
                words<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, words<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> = words<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>, word
                <span style="color: #ff7700;font-weight:bold;">if</span> words<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>:
                    <span style="color: #008000;">self</span>._data<span style="color: black;">&#91;</span>words<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span>words<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> gentext<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, num_words<span style="color: black;">&#41;</span>:
        text = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
        text.<span style="color: black;">append</span><span style="color: black;">&#40;</span>choice<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._data.<span style="color: black;">keys</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>.<span style="color: black;">title</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>text<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&lt;</span> num_words:
            <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>._data.<span style="color: black;">has_key</span><span style="color: black;">&#40;</span>text<span style="color: black;">&#91;</span><span style="color: #ff4500;">-1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>:
                text.<span style="color: black;">append</span><span style="color: black;">&#40;</span>choice<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._data<span style="color: black;">&#91;</span>text<span style="color: black;">&#91;</span><span style="color: #ff4500;">-1</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">else</span>:
                text.<span style="color: black;">append</span><span style="color: black;">&#40;</span>choice<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._data.<span style="color: black;">keys</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">' '</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>text<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">'.'</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    textgen = TextGenerator<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    textgen.<span style="color: black;">train</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'pandp.txt'</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> textgen.<span style="color: black;">gentext</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">100</span><span style="color: black;">&#41;</span></pre></div></div>

<p>The data I used for training the program was documents I found on <a href="http://www.gutenberg.org/">Project Gutenberg</a>; &#8216;pandp.txt&#8217; - the file mentioned in the code above, is Pride and Prejudice by Jane Austen, however, for use on the web you could train the program with a series of existing web pages on a particular topic. Here is an example paragraph produced by the program after being trained with Pride and Prejudice:</p>
<blockquote><p>Productive way! I have the room; but as well married, but had escaped her every painful than you can, you have any reply. You have been. &#8220;What did not ask for the most attentive and she and solemn, apologising if he had marked their brother Gardiner&#8217;s curiosity; and Wickham, we know as they lived, and her affability, that other, and by her mother&#8217;s reproach him as your cousin Lydia&#8217;s unguarded temper, that had been his amusement. But little to place my aunt and Lydia&#8217;s interruption, &#8220;that when we should happen to guide us. We both him desperate.&#8221; The horses drew.</p></blockquote>
<p>I extended this concept to generate English-sounding words (letters were generated to make up a word, rather than words being generated to make up a paragraph). In case you&#8217;re interested, the <a href='http://bitecode.co.uk/wp-content/uploads/2008/07/wordgenpy.txt'>here</a> is code.</p>
<p><a href="http://sharethis.com/item?&wp=2.5.1&amp;publisher=b8e8e47b-e8f1-4814-bb9c-d1bf2397550a&amp;title=Generating+Content+with+Markov+Chains&amp;url=http%3A%2F%2Fbitecode.co.uk%2F2008%2F07%2Fgenerating-content-with-markov-chains%2F">ShareThis</a></p>]]></content:encoded>
			<wfw:commentRss>http://bitecode.co.uk/2008/07/generating-content-with-markov-chains/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Edge Detection in Python</title>
		<link>http://bitecode.co.uk/2008/07/edge-detection-in-python/</link>
		<comments>http://bitecode.co.uk/2008/07/edge-detection-in-python/#comments</comments>
		<pubDate>Sun, 13 Jul 2008 13:08:50 +0000</pubDate>
		<dc:creator>bitecode</dc:creator>
		
		<category><![CDATA[Image Processing]]></category>

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

		<category><![CDATA[edge detection]]></category>

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

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

		<guid isPermaLink="false">http://bitecode.co.uk/?p=15</guid>
		<description><![CDATA[I&#8217;ve been reading up on digital image processing and wanted to have a go at implementing a Sobel edge detector. I&#8217;m fairly familiar with PIL, so I decided to implement the algorithm in Python. Python is not by any means and ideal language for digital image processing; it is far too slow for most things, [...]<script type="text/javascript">SHARETHIS.addEntry({ title: "Edge Detection in Python", url: "http://bitecode.co.uk/2008/07/edge-detection-in-python/" });</script>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been reading up on digital image processing and wanted to have a go at implementing a Sobel edge detector. I&#8217;m fairly familiar with <a title="Python Imaging Library" href="http://www.pythonware.com/products/pil">PIL</a>, so I decided to implement the algorithm in Python. Python is not by any means and ideal language for digital image processing; it is far too slow for most things, but for the time being the speed doesn&#8217;t matter. Anyway, here is the basic implementation:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">def</span> sobel<span style="color: black;">&#40;</span>img<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> img.<span style="color: black;">mode</span> <span style="color: #66cc66;">!</span>= <span style="color: #483d8b;">&quot;RGB&quot;</span>:
        img = img.<span style="color: black;">convert</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;RGB&quot;</span><span style="color: black;">&#41;</span>
    out_image = Image.<span style="color: #dc143c;">new</span><span style="color: black;">&#40;</span>img.<span style="color: black;">mode</span>, img.<span style="color: black;">size</span>, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span>
    imgdata = img.<span style="color: black;">load</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    outdata = out_image.<span style="color: black;">load</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    gx = <span style="color: black;">&#91;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">-1</span>, <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>, <span style="color: black;">&#91;</span><span style="color: #ff4500;">-2</span>, <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span>, <span style="color: black;">&#91;</span><span style="color: #ff4500;">-1</span>, <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span>
    gy = <span style="color: black;">&#91;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">-1</span>, <span style="color: #ff4500;">-2</span>, <span style="color: #ff4500;">-1</span><span style="color: black;">&#93;</span>, <span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, <span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">2</span>, <span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">for</span> row <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>, img.<span style="color: black;">size</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: #ff4500;">-1</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">for</span> col <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>, img.<span style="color: black;">size</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: #ff4500;">-1</span><span style="color: black;">&#41;</span>:
            pixel_gx = pixel_gy = <span style="color: #ff4500;">0</span>
            pxval = <span style="color: #008000;">sum</span><span style="color: black;">&#40;</span>imgdata<span style="color: black;">&#91;</span>row,col<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>/<span style="color: #ff4500;">3</span>
            <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">-1</span>, <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>:
                <span style="color: #ff7700;font-weight:bold;">for</span> j <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">-1</span>, <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>:
                    val = <span style="color: #008000;">sum</span><span style="color: black;">&#40;</span>imgdata<span style="color: black;">&#91;</span>row+i,col+j<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>/<span style="color: #ff4500;">3</span>
                    pixel_gx += gx<span style="color: black;">&#91;</span>i<span style="color: #ff4500;">+1</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>j<span style="color: #ff4500;">+1</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">*</span> val
                    pixel_gy += gy<span style="color: black;">&#91;</span>i<span style="color: #ff4500;">+1</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>j<span style="color: #ff4500;">+1</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">*</span> val
            newpixel = <span style="color: #dc143c;">math</span>.<span style="color: black;">sqrt</span><span style="color: black;">&#40;</span>pixel_gx <span style="color: #66cc66;">*</span> pixel_gx + pixel_gy <span style="color: #66cc66;">*</span> pixel_gy<span style="color: black;">&#41;</span>
            newpixel = <span style="color: #ff4500;">255</span> - <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>newpixel<span style="color: black;">&#41;</span>
            outdata<span style="color: black;">&#91;</span>row, col<span style="color: black;">&#93;</span> = <span style="color: black;">&#40;</span>newpixel, newpixel, newpixel<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> out_image</pre></div></div>

<p>The results are surprisingly good:<br />
<img src="http://img183.imageshack.us/img183/7865/lenasobelcj8.jpg" border="0" /></p>
<p><a href="http://sharethis.com/item?&wp=2.5.1&amp;publisher=b8e8e47b-e8f1-4814-bb9c-d1bf2397550a&amp;title=Edge+Detection+in+Python&amp;url=http%3A%2F%2Fbitecode.co.uk%2F2008%2F07%2Fedge-detection-in-python%2F">ShareThis</a></p>]]></content:encoded>
			<wfw:commentRss>http://bitecode.co.uk/2008/07/edge-detection-in-python/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Common Searching and Sorting Algorithms in Python</title>
		<link>http://bitecode.co.uk/2008/07/common-searching-and-sorting-algorithms-in-python/</link>
		<comments>http://bitecode.co.uk/2008/07/common-searching-and-sorting-algorithms-in-python/#comments</comments>
		<pubDate>Sun, 13 Jul 2008 09:31:37 +0000</pubDate>
		<dc:creator>bitecode</dc:creator>
		
		<category><![CDATA[Python]]></category>

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

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

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

		<guid isPermaLink="false">http://bitecode.co.uk/?p=14</guid>
		<description><![CDATA[I implemented some common searching and sorting algorithms in Python to improve my understanding of them in preparation for an algorithms exam. They may not be perfect implementations (unoptimised etc.) but they may help a Python-minded individual understand how they work&#8230;
Note: The selection sort and bubble sort algorithms take one argument, a list, and modify [...]<script type="text/javascript">SHARETHIS.addEntry({ title: "Common Searching and Sorting Algorithms in Python", url: "http://bitecode.co.uk/2008/07/common-searching-and-sorting-algorithms-in-python/" });</script>]]></description>
			<content:encoded><![CDATA[<p>I implemented some common searching and sorting algorithms in Python to improve my understanding of them in preparation for an algorithms exam. They may not be perfect implementations (unoptimised etc.) but they may help a Python-minded individual understand how they work&#8230;</p>
<p><strong>Note:</strong> The selection sort and bubble sort algorithms take one argument, a list, and modify it in place as it is a mutable type. The merge sort and quicksort algorithms sorted versions of the provided lists; they don&#8217;t modify the lists in place. The search algorithms take two arguments - a list (l), and an element to be found(k). The both return the index of the found element.</p>
<h3>Selection Sort</h3>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">def</span> selection_sort<span style="color: black;">&#40;</span>l<span style="color: black;">&#41;</span>:
    n = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>l<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span>n<span style="color: #ff4500;">-1</span><span style="color: black;">&#41;</span>:
        mini = i
        <span style="color: #ff7700;font-weight:bold;">for</span> j <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span>i<span style="color: #ff4500;">+1</span>, n<span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">if</span> l<span style="color: black;">&#91;</span>j<span style="color: black;">&#93;</span> <span style="color: #66cc66;">&lt;</span> l<span style="color: black;">&#91;</span>mini<span style="color: black;">&#93;</span>:
                mini = j
        l<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span>, l<span style="color: black;">&#91;</span>mini<span style="color: black;">&#93;</span> = l<span style="color: black;">&#91;</span>mini<span style="color: black;">&#93;</span>, l<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span></pre></div></div>

<h3>Bubble Sort</h3>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">def</span> bubble_sort<span style="color: black;">&#40;</span>l<span style="color: black;">&#41;</span>:
    n = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>l<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span>n<span style="color: #ff4500;">-1</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">for</span> j <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span>n<span style="color: #ff4500;">-1</span>-i<span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">if</span> l<span style="color: black;">&#91;</span>j<span style="color: black;">&#93;</span> <span style="color: #66cc66;">&gt;</span> l<span style="color: black;">&#91;</span>j<span style="color: #ff4500;">+1</span><span style="color: black;">&#93;</span>:
                l<span style="color: black;">&#91;</span>j<span style="color: black;">&#93;</span>, l<span style="color: black;">&#91;</span>j<span style="color: #ff4500;">+1</span><span style="color: black;">&#93;</span> = l<span style="color: black;">&#91;</span>j<span style="color: #ff4500;">+1</span><span style="color: black;">&#93;</span>, l<span style="color: black;">&#91;</span>j<span style="color: black;">&#93;</span></pre></div></div>

<h3>Merge Sort</h3>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">def</span> merge_sort<span style="color: black;">&#40;</span>l<span style="color: black;">&#41;</span>:
    n = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>l<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> n <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">1</span>:
        mid = n/<span style="color: #ff4500;">2</span>
        l1 = merge_sort<span style="color: black;">&#40;</span>l<span style="color: black;">&#91;</span>:mid<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        l2 = merge_sort<span style="color: black;">&#40;</span>l<span style="color: black;">&#91;</span>mid:<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> merge<span style="color: black;">&#40;</span>l1, l2<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> l
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> merge<span style="color: black;">&#40;</span>l1, l2<span style="color: black;">&#41;</span>:
    result = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
    p = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>l1<span style="color: black;">&#41;</span>
    q = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>l2<span style="color: black;">&#41;</span>
    i = j = <span style="color: #ff4500;">0</span>
    <span style="color: #ff7700;font-weight:bold;">while</span> i <span style="color: #66cc66;">&lt;</span> p <span style="color: #ff7700;font-weight:bold;">and</span> j <span style="color: #66cc66;">&lt;</span> q:
        <span style="color: #ff7700;font-weight:bold;">if</span> l1<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> <span style="color: #66cc66;">&lt;</span>= l2<span style="color: black;">&#91;</span>j<span style="color: black;">&#93;</span>:
            result.<span style="color: black;">append</span><span style="color: black;">&#40;</span>l1<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
            i += <span style="color: #ff4500;">1</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            result.<span style="color: black;">append</span><span style="color: black;">&#40;</span>l2<span style="color: black;">&#91;</span>j<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
            j += <span style="color: #ff4500;">1</span>
    result.<span style="color: black;">extend</span><span style="color: black;">&#40;</span>l1<span style="color: black;">&#91;</span>i:<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
    result.<span style="color: black;">extend</span><span style="color: black;">&#40;</span>l2<span style="color: black;">&#91;</span>j:<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> result</pre></div></div>

<h3>QuickSort</h3>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">def</span> quick_sort<span style="color: black;">&#40;</span>l<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> l == <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        p = l<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
        left = quick_sort<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>x <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> l<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>:<span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">if</span> x <span style="color: #66cc66;">&lt;</span> p<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        right = quick_sort<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>x <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> l<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>:<span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">if</span> x <span style="color: #66cc66;">&gt;</span>= p<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> left + <span style="color: black;">&#91;</span>p<span style="color: black;">&#93;</span> + right</pre></div></div>

<h3>Sequential Search</h3>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">def</span> sequential_search<span style="color: black;">&#40;</span>l, k<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>l<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> l<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> == k:
            <span style="color: #ff7700;font-weight:bold;">return</span> i</pre></div></div>

<h3>Binary Search</h3>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">def</span> binary_search<span style="color: black;">&#40;</span>a, k<span style="color: black;">&#41;</span>:
    count = <span style="color: #ff4500;">0</span>
    l, r = <span style="color: #ff4500;">0</span>, <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>a<span style="color: black;">&#41;</span><span style="color: #ff4500;">-1</span>
    <span style="color: #ff7700;font-weight:bold;">while</span> l <span style="color: #66cc66;">&lt;</span>= r:
        count += <span style="color: #ff4500;">1</span>
        mid = <span style="color: black;">&#40;</span>l+r<span style="color: black;">&#41;</span>/<span style="color: #ff4500;">2</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> k == a<span style="color: black;">&#91;</span>mid<span style="color: black;">&#93;</span>:
            <span style="color: #ff7700;font-weight:bold;">print</span> count
            <span style="color: #ff7700;font-weight:bold;">return</span> mid
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            <span style="color: #ff7700;font-weight:bold;">if</span> k <span style="color: #66cc66;">&lt;</span> a<span style="color: black;">&#91;</span>mid<span style="color: black;">&#93;</span>:
                r = mid
            <span style="color: #ff7700;font-weight:bold;">else</span>:
                l = mid<span style="color: #ff4500;">+1</span></pre></div></div>

<p><a href="http://sharethis.com/item?&wp=2.5.1&amp;publisher=b8e8e47b-e8f1-4814-bb9c-d1bf2397550a&amp;title=Common+Searching+and+Sorting+Algorithms+in+Python&amp;url=http%3A%2F%2Fbitecode.co.uk%2F2008%2F07%2Fcommon-searching-and-sorting-algorithms-in-python%2F">ShareThis</a></p>]]></content:encoded>
			<wfw:commentRss>http://bitecode.co.uk/2008/07/common-searching-and-sorting-algorithms-in-python/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Web Security and Encryption - Missing a Middle Ground?</title>
		<link>http://bitecode.co.uk/2008/07/web-security-and-encryption-missing-a-middle-ground/</link>
		<comments>http://bitecode.co.uk/2008/07/web-security-and-encryption-missing-a-middle-ground/#comments</comments>
		<pubDate>Fri, 11 Jul 2008 23:40:17 +0000</pubDate>
		<dc:creator>bitecode</dc:creator>
		
		<category><![CDATA[Security]]></category>

		<category><![CDATA[Web Development]]></category>

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

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

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

		<guid isPermaLink="false">http://bitecode.co.uk/?p=12</guid>
		<description><![CDATA[It occurs to me that online security is generally either very thorough or non-existent. If a website needs to be secure it uses SSL. If security is not utterly essential, there is often no security used at all - everything is transmitted in plain text. What about the sites that have login forms, sites that [...]<script type="text/javascript">SHARETHIS.addEntry({ title: "Web Security and Encryption - Missing a Middle Ground?", url: "http://bitecode.co.uk/2008/07/web-security-and-encryption-missing-a-middle-ground/" });</script>]]></description>
			<content:encoded><![CDATA[<p>It occurs to me that online security is generally either very thorough or non-existent. If a website needs to be secure it uses SSL. If security is not utterly essential, there is often no security used at all - everything is transmitted in plain text. What about the sites that have login forms, sites that take some potentially confidential information from clients, but can&#8217;t afford a full SSL certificate.</p>
<p>SSL provides extremely strong encryption and uses a very effective protocol to help with authorization. This protocol makes use of a Certificate Authority - the CA issues the certificate that the server uses in the protocol, and is trusted by the client. It is possible to generate your own certificates, but if a certificate is encountered that has not been issued by a trusted CA a popup message is displayed in the browser window that warns users that the site may not be secure. This feature is vital as it warns users not to enter confidential information (such as credit card details) on a site that is not absolutely secure. </p>
<p>There are, however, sites where absolute security isn&#8217;t necessary. SSL certificates cost money. To give you some idea, VeriSign - one of the largest (if not the largest) CAs charges £599 + VAT for their mid-range SSL certificate (Secure Site Pro - 128bit encryption). To be fair, sites such as RapidSSL offer cheaper certificates, but for hobby sites or sites on very limited budgets any price is too much. More and more sites these days require you to sign in to access their full content - I&#8217;d bet that most people re-use passwords accross different sites (including for their web-based e-mail?). If this is the case, logging in to a site that doesn&#8217;t use SSL could mean that a user&#8217;s password is sent across the internet in plain text, which makes it completely vunerable to eavesdropping attacks. If this same password provides access to an e-mail account, the user&#8217;s identity could potentially be stolen (an e-mail account can be used to gain access to other sites using the “I&#8217;ve forgotten my password” forms).</p>
<p>To me it seems absurd that there isn&#8217;t already a mid-level security system, one that uses regular HTTP, that is free but not as secure as SSL. Even a system that didn&#8217;t bother encrypting pages - that only encrypted form data would be useful. For the time being, I will present the way that I prevent passwords from being sent in plaintext in login forms. (Note: this is not completely secure by any means, and only works with JavaScript-enabled browsers).</p>
<p>This example demonstrates the steps for a login page being displayed and the user logging in using their username and password. A password stored on a server shouldn&#8217;t be plaintext, for this example we will assume that an MD5 hash of the password is stored.</p>
<ol>
<li>The server generates a random salt and embeds it in some JavaScript code on the page.</li>
<li>The user fills in the form with their username and password.</li>
<li>When the form is submitted, some JavaScript code catches the event and pauses the submission.</li>
<li>The JavaScript code produces the MD5 hash of the user&#8217;s password. It then concatenates the hash with the salt provided by the server. An MD5 hash is then produced of the result and submitted in place of the password.</li>
<li>The server identifies the user by their session and recalls the salt from a session variable. It also produces an MD5 hash of the concatenation of the password hash that is retrieved from the database and the randomly generated salt. If the result of this matches the user&#8217;s input, their password is valid.</li>
</ol>
<p>As I stated before, this is not entirely secure - session cookies can still be stolen, most of the transmission is still plaintext, but it does protect the user&#8217;s password from man-in-the-middle and eavesdropping attacks.</p>
<p><a href="http://sharethis.com/item?&wp=2.5.1&amp;publisher=b8e8e47b-e8f1-4814-bb9c-d1bf2397550a&amp;title=Web+Security+and+Encryption+-+Missing+a+Middle+Ground%3F&amp;url=http%3A%2F%2Fbitecode.co.uk%2F2008%2F07%2Fweb-security-and-encryption-missing-a-middle-ground%2F">ShareThis</a></p>]]></content:encoded>
			<wfw:commentRss>http://bitecode.co.uk/2008/07/web-security-and-encryption-missing-a-middle-ground/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Python + Psyco vs Java vs C++ vs C</title>
		<link>http://bitecode.co.uk/2008/07/python-psyco-vs-java-vs-c-vs-c/</link>
		<comments>http://bitecode.co.uk/2008/07/python-psyco-vs-java-vs-c-vs-c/#comments</comments>
		<pubDate>Fri, 11 Jul 2008 17:36:17 +0000</pubDate>
		<dc:creator>bitecode</dc:creator>
		
		<category><![CDATA[C/C++]]></category>

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

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

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

		<category><![CDATA[c++]]></category>

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

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

		<guid isPermaLink="false">http://bitecode.co.uk/?p=11</guid>
		<description><![CDATA[In my last post, I demonstrated how Psyco can be used to speed up Python code using the example of a brute-force password cracker. To see how the speed of the Python-Psyco combination compared to the speed of other languages I ported to code to Java, C++ and C. In each language I wrote the [...]<script type="text/javascript">SHARETHIS.addEntry({ title: "Python + Psyco vs Java vs C++ vs C", url: "http://bitecode.co.uk/2008/07/python-psyco-vs-java-vs-c-vs-c/" });</script>]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://bitecode.co.uk/2008/07/password-cracking-in-python-with-psyco/">last post</a>, I demonstrated how Psyco can be used to speed up Python code using the example of a brute-force password cracker. To see how the speed of the Python-Psyco combination compared to the speed of other languages I ported to code to Java, C++ and C. In each language I wrote the code as if it were a regular program written in that language (i.e. I didn&#8217;t try to optimize the code) - in C++ I used C++ STL Strings, in Java I used the StringBuilder class and in C I used <code>char</code> arrays.</p>
<p>The timings for the Python+Psyco code are as follows (see last post for full details):</p>
<pre>Password | Time taken
zzzz     | 0m0.134s
zzzzz    | 0m3.486s
zzzzzz   | 2m8.056s</pre>
<p>Here is the Java code (I&#8217;m not a Java programmer at all - I just know the basics so I apologise if the code is poorly written):</p>

<div class="wp_syntax"><div class="code"><pre class="java"><span style="color: #000000; font-weight: bold;">class</span> BrutePass <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> count <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span> <span style="color: #003399;">String</span> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> args.<span style="color: #006633;">length</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Usage: java BrutePass &lt;password&gt;&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #003399;">String</span> pwd <span style="color: #339933;">=</span> args<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">int</span> maxlen <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
&nbsp;
        StringBuilder p <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StringBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">int</span> w<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> w<span style="color: #339933;">&lt;</span>maxlen<span style="color: #339933;">;</span> w<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> crack<span style="color: #009900;">&#40;</span> w<span style="color: #cc66cc;">+1</span>, <span style="color: #cc66cc;">0</span>, p, pwd <span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span>
                <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Found password &quot;</span> <span style="color: #339933;">+</span> p.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
             <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; with &quot;</span> <span style="color: #339933;">+</span> count <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; attempts.&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> crack<span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">int</span> w, <span style="color: #000066; font-weight: bold;">int</span> pos, StringBuilder p, 
        <span style="color: #003399;">String</span> pwd <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #003399;">String</span> chars <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;abcdefghijklmnopqrstuvwxyz&quot;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>chars.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            p.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span> chars.<span style="color: #006633;">charAt</span><span style="color: #009900;">&#40;</span> i <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> pos <span style="color: #339933;">==</span> w<span style="color: #cc66cc;">-1</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                count<span style="color: #339933;">++;</span>
                <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> pwd.<span style="color: #006633;">compareTo</span><span style="color: #009900;">&#40;</span> p.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span>
                    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">int</span> ret <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> pos <span style="color: #339933;">&lt;</span> w<span style="color: #cc66cc;">-1</span> <span style="color: #009900;">&#41;</span>
                ret <span style="color: #339933;">=</span> crack<span style="color: #009900;">&#40;</span> w, pos<span style="color: #cc66cc;">+1</span>, p, pwd <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> ret <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span>
                <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
            p.<span style="color: #006633;">delete</span><span style="color: #009900;">&#40;</span> p.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span>, p.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here are the timings for the java program:</p>
<pre>Password | Time taken
zzzz     | 0m0.176s
zzzzz    | 0m1.334s
zzzzzz   | 0m31.369s</pre>
<p>Much faster than the Psyco+Python version. The reason that the four-character password appears to take longer to crack in Java than in Python is that the JVM takes a while to get going. Now for the C++ code:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp"><span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #339900;">#include &lt;string&gt;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std;
&nbsp;
<span style="color: #0000ff;">int</span> crack<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> w, <span style="color: #0000ff;">int</span> pos, string<span style="color: #000040;">&amp;</span> p, string<span style="color: #000040;">&amp;</span> pass <span style="color: #008000;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> argc, <span style="color: #0000ff;">char</span> <span style="color: #000040;">**</span>argv <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> argc <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">2</span> <span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Usage: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> argv<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; &lt;password&gt;&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl;
        <span style="color: #0000ff;">return</span><span style="color: #008000;">&#40;</span> <span style="color: #0000dd;">0</span> <span style="color: #008000;">&#41;</span>;
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">int</span> maxlen <span style="color: #000080;">=</span> <span style="color: #0000dd;">10</span>;
    <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> argc <span style="color: #000080;">&gt;</span> <span style="color: #0000dd;">2</span> <span style="color: #008000;">&#41;</span>
        maxlen <span style="color: #000080;">=</span> <span style="color: #0000dd;">atoi</span><span style="color: #008000;">&#40;</span> argv<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#41;</span>;
&nbsp;
    string pwd<span style="color: #008000;">&#40;</span> argv<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#41;</span>;
    string p;
&nbsp;
    <span style="color: #0000ff;">int</span> count;
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span>; i<span style="color: #000080;">&lt;</span>maxlen; i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
        <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> count <span style="color: #000080;">=</span> crack<span style="color: #008000;">&#40;</span> i<span style="color: #0000dd;">+1</span>, <span style="color: #0000dd;">0</span>, p, pwd <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span>
            break;
&nbsp;
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Found password &quot;</span> <span style="color: #000080;">&lt;&lt;</span> p <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; with &quot;</span> <span style="color: #000080;">&lt;&lt;</span> count <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; attempts.&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl;
&nbsp;
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span>;
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> crack<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> w, <span style="color: #0000ff;">int</span> pos, string<span style="color: #000040;">&amp;</span> p, string<span style="color: #000040;">&amp;</span> pass <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">int</span> count <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span>;
    string chars<span style="color: #008000;">&#40;</span> <span style="color: #FF0000;">&quot;abcdefghijklmnopqrstuvwxyz&quot;</span> <span style="color: #008000;">&#41;</span>;
&nbsp;
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span>; i<span style="color: #000080;">&lt;</span>chars.<span style="color: #00eeff;">length</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>; i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        p.<span style="color: #00eeff;">push_back</span><span style="color: #008000;">&#40;</span> chars<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#41;</span>;
&nbsp;
        <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> pos <span style="color: #000080;">==</span> w<span style="color: #0000dd;">-1</span> <span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> p <span style="color: #000080;">==</span> pass <span style="color: #008000;">&#41;</span>
                <span style="color: #0000ff;">return</span><span style="color: #008000;">&#40;</span> count <span style="color: #008000;">&#41;</span>;
            count<span style="color: #000040;">++</span>;
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0000ff;">int</span> ret <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span>;
        <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> pos <span style="color: #000080;">&lt;</span> w<span style="color: #0000dd;">-1</span> <span style="color: #008000;">&#41;</span>
            ret <span style="color: #000080;">=</span> crack<span style="color: #008000;">&#40;</span> w, pos<span style="color: #0000dd;">+1</span>, p, pass <span style="color: #008000;">&#41;</span>;
&nbsp;
        <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> ret <span style="color: #008000;">&#41;</span>
            <span style="color: #0000ff;">return</span><span style="color: #008000;">&#40;</span> count <span style="color: #008000;">&#41;</span>;
&nbsp;
        p.<span style="color: #00eeff;">erase</span><span style="color: #008000;">&#40;</span> p.<span style="color: #00eeff;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #0000dd;">-1</span> <span style="color: #008000;">&#41;</span>;
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0000ff;">return</span><span style="color: #008000;">&#40;</span> <span style="color: #0000dd;">0</span> <span style="color: #008000;">&#41;</span>;
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>And the timings:</p>
<pre>Password | Time taken
zzzz     | 0m0.036s
zzzzz    | 0m0.730s
zzzzzz   | 0m18.999s</pre>
<p>I&#8217;d heard that Java is in many cases faster than C++; clearly this is not one of those cases. Now the C version:</p>

<div class="wp_syntax"><div class="code"><pre class="c"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;stdlib.h&gt;</span>
<span style="color: #339933;">#include &lt;string.h&gt;</span>
&nbsp;
<span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> crack<span style="color: #009900;">&#40;</span> <span style="color: #993333;">int</span> w, <span style="color: #993333;">int</span> pos, <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>p, <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>pass <span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span> <span style="color: #993333;">int</span> argc, <span style="color: #993333;">char</span> <span style="color: #339933;">**</span>argv <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> maxlen <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span>, i;
    <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> count;
    <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>pwd, <span style="color: #339933;">*</span>p;
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> argc &lt; <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span> <span style="color: #ff0000;">&quot;Usage: %s &lt;password&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, argv<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span>;
        <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> argc &gt; <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&#41;</span>
        maxlen <span style="color: #339933;">=</span> atoi<span style="color: #009900;">&#40;</span> argv<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span>;
&nbsp;
    pwd <span style="color: #339933;">=</span> argv<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span>;
    p <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>calloc<span style="color: #009900;">&#40;</span> maxlen <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span>, <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>;
&nbsp;
    <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span> i<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span>; i&lt;maxlen; i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> count <span style="color: #339933;">=</span> crack<span style="color: #009900;">&#40;</span> i<span style="color: #cc66cc;">+1</span>, <span style="color: #cc66cc;">0</span>, p, pwd <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">break</span>;
&nbsp;
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span> <span style="color: #ff0000;">&quot;Found password %s with %lu attempts.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, p, count <span style="color: #009900;">&#41;</span>;
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> crack<span style="color: #009900;">&#40;</span> <span style="color: #993333;">int</span> w, <span style="color: #993333;">int</span> pos, <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>p, <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>pass <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">static</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> count <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span>;
    <span style="color: #993333;">int</span> i, ret;
    <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>chars <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;abcdefghijklmnopqrstuvwxyz&quot;</span>;
&nbsp;
    <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span> i<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span>; i&lt;strlen<span style="color: #009900;">&#40;</span>chars<span style="color: #009900;">&#41;</span>; i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        p<span style="color: #009900;">&#91;</span>strlen<span style="color: #009900;">&#40;</span>p<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> chars<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>;
&nbsp;
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> pos <span style="color: #339933;">==</span> w<span style="color: #cc66cc;">-1</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            count<span style="color: #339933;">++</span>;
            <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span>strcmp<span style="color: #009900;">&#40;</span> p, pass <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
                <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span> count <span style="color: #009900;">&#41;</span>;
        <span style="color: #009900;">&#125;</span>
&nbsp;
        ret <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span>;
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> pos &lt; w<span style="color: #cc66cc;">-1</span> <span style="color: #009900;">&#41;</span>
            ret <span style="color: #339933;">=</span> crack<span style="color: #009900;">&#40;</span> w, pos<span style="color: #cc66cc;">+1</span>, p, pass <span style="color: #009900;">&#41;</span>;
&nbsp;
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> ret <span style="color: #009900;">&#41;</span>
            <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span> count <span style="color: #009900;">&#41;</span>;
&nbsp;
        p<span style="color: #009900;">&#91;</span>strlen<span style="color: #009900;">&#40;</span>p<span style="color: #009900;">&#41;</span><span style="color: #cc66cc;">-1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span>;
    <span style="color: #009900;">&#125;</span>    
    <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And the timings:</p>
<pre>Password | Time taken
zzzz     | 0m0.022s
zzzzz    | 0m0.381s
zzzzzz   | 0m9.663s</pre>
<p>That&#8217;s pretty much twice the speed of the C++ version! This shows how much overhead there is for using STL Strings rather than C strings.</p>
<p>Although Psyco provided a fantastic speed boost, it has not eliminated the need to know C as some people have claimed - the native C version of the program performed 13.25x faster than the Psyco version.</p>
<p><a href="http://sharethis.com/item?&wp=2.5.1&amp;publisher=b8e8e47b-e8f1-4814-bb9c-d1bf2397550a&amp;title=Python+%2B+Psyco+vs+Java+vs+C%2B%2B+vs+C&amp;url=http%3A%2F%2Fbitecode.co.uk%2F2008%2F07%2Fpython-psyco-vs-java-vs-c-vs-c%2F">ShareThis</a></p>]]></content:encoded>
			<wfw:commentRss>http://bitecode.co.uk/2008/07/python-psyco-vs-java-vs-c-vs-c/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Password Cracking in Python with Psyco</title>
		<link>http://bitecode.co.uk/2008/07/password-cracking-in-python-with-psyco/</link>
		<comments>http://bitecode.co.uk/2008/07/password-cracking-in-python-with-psyco/#comments</comments>
		<pubDate>Thu, 03 Jul 2008 16:19:14 +0000</pubDate>
		<dc:creator>bitecode</dc:creator>
		
		<category><![CDATA[Python]]></category>

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

		<category><![CDATA[brute-force]]></category>

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

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

		<guid isPermaLink="false">http://bitecode.co.uk/?p=9</guid>
		<description><![CDATA[A friend of mine recently implemented a brute-force password cracker in Python. I, having never looked into this kind of thing before, became curious about the implementation details. He showed me his code and it was surprisingly simple - just one recursive function and a small loop that called the function. It wasn&#8217;t actually a [...]<script type="text/javascript">SHARETHIS.addEntry({ title: "Password Cracking in Python with Psyco", url: "http://bitecode.co.uk/2008/07/password-cracking-in-python-with-psyco/" });</script>]]></description>
			<content:encoded><![CDATA[<p>A friend of mine recently implemented a brute-force password cracker in Python. I, having never looked into this kind of thing before, became curious about the implementation details. He showed me his code and it was surprisingly simple - just one recursive function and a small loop that called the function. It wasn&#8217;t actually a proper password cracker - you supplied a word and it would generate all possible words (by combining letters from a list) until the supplied word was matched. As it stands, it is useless but it wouldn&#8217;t be hard to make it produce SHA1 or MD5 hash of each word it generated - this would allow it to break hashed passwords.</p>
<p>The main problem with the code was that it was extremely slow. I wanted to see how much faster it would run if <a title="Psyco" href="http://psyco.sourceforge.net/">Psyco</a> (a JIT complier for Python) was used. I wrote my own implementation of the original code and took a few timings:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
&nbsp;
chars = <span style="color: #483d8b;">'abcdefghijklmnopqrstuvwxyz0123456789'</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> crack<span style="color: black;">&#40;</span>w, pos, p, <span style="color: #dc143c;">pwd</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">for</span> c <span style="color: #ff7700;font-weight:bold;">in</span> chars:
        cr = <span style="color: #008000;">None</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> pos <span style="color: #66cc66;">&lt;</span> w:
            cr = crack<span style="color: black;">&#40;</span>w, pos<span style="color: #ff4500;">+1</span>, p+c, <span style="color: #dc143c;">pwd</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> cr:
            <span style="color: #ff7700;font-weight:bold;">return</span> cr
        <span style="color: #ff7700;font-weight:bold;">if</span> p+c == <span style="color: #dc143c;">pwd</span>:
            <span style="color: #ff7700;font-weight:bold;">return</span> p+c
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        <span style="color: #dc143c;">pwd</span> = <span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">except</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Usage: %s &lt;password&gt;'</span> <span style="color: #66cc66;">%</span> <span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
        <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
    p = <span style="color: #008000;">None</span>
    w = <span style="color: #ff4500;">1</span>
    max_len = <span style="color: #ff4500;">10</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #ff7700;font-weight:bold;">not</span> p <span style="color: #ff7700;font-weight:bold;">and</span> w <span style="color: #66cc66;">&lt;</span> max_len:
        p = crack<span style="color: black;">&#40;</span>w, <span style="color: #ff4500;">0</span>, <span style="color: #483d8b;">''</span>, <span style="color: #dc143c;">pwd</span><span style="color: black;">&#41;</span>
        w += <span style="color: #ff4500;">1</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Found'</span>, p</pre></div></div>

<p>Here are the results of the timings for 4, 5 and 6 character passwords (all timings taken on a 2.4GHz Macbook Pro w/ 2GB RAM):</p>
<pre>$ time ./python_brutepass zzzz
Found zzzz

real	0m0.567s
user	0m0.542s
sys	0m0.013s
$ time ./python_brutepass zzzzz
Found zzzzz

real	0m18.779s
user	0m18.632s
sys	0m0.046s
$ time ./python_brutepass zzzzzz
Found zzzzzz

real	11m9.131s
user	11m4.323s
sys	0m1.190s</pre>
<p>11 minutes to match a 6 character word (which is only lowercase characters and numbers)?! Once you&#8217;d added in a hashing step, I imagine this would take a great deal longer still.</p>
<p>I added in a couple of lines at the top of the code:</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> psyco
psyco.<span style="color: black;">full</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>and took the timings again:</p>
<pre>$ time ./python_brutepass zzzz
Found zzzz

real	0m0.134s
user	0m0.112s
sys	0m0.014s
$ time ./python_brutepass zzzzz
Found zzzzz

real	0m3.486s
user	0m3.435s
sys	0m0.022s
$ time ./python_brutepass zzzzzz
Found zzzzzz

real	2m8.056s
user	2m6.994s
sys	0m0.254s</pre>
<p>With Psyco enabled it ran ~5x quicker - not bad for an extra two lines of code.</p>
<p>Anyway, these examples show that such computationally expensive operations aren&#8217;t really possible in Python. To find a 5 character password, the maximum number of operations needed (assuming no hashing is done) is ~62193781. I worked this out as follows. If <em>n</em> is the length of the password (5), and <em>l</em> is the number of letters being used (36 in this case for lowercase letters + numbers from 0 to 9), the total number of possible permutations with repitition for each length of word is l<sup>n</sup>, but as the length of the word would not be known, the total number of permuatations with repitition is the sum of l<sup>k</sup> for k=0 to n. This is actually a geometric progression and thus can be calculated like this:<br /><img style="" title="Equation" src="http://bitecode.co.uk/wp-content/uploads/2008/07/brutepass_eqn.png" alt="" width="179" height="65" /></p>
<p><a href="http://sharethis.com/item?&wp=2.5.1&amp;publisher=b8e8e47b-e8f1-4814-bb9c-d1bf2397550a&amp;title=Password+Cracking+in+Python+with+Psyco&amp;url=http%3A%2F%2Fbitecode.co.uk%2F2008%2F07%2Fpassword-cracking-in-python-with-psyco%2F">ShareThis</a></p>]]></content:encoded>
			<wfw:commentRss>http://bitecode.co.uk/2008/07/password-cracking-in-python-with-psyco/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Binary Representation of Numbers in Python</title>
		<link>http://bitecode.co.uk/2008/07/binary-representation-of-numbers-in-python/</link>
		<comments>http://bitecode.co.uk/2008/07/binary-representation-of-numbers-in-python/#comments</comments>
		<pubDate>Thu, 03 Jul 2008 00:55:34 +0000</pubDate>
		<dc:creator>bitecode</dc:creator>
		
		<category><![CDATA[Python]]></category>

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

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

		<guid isPermaLink="false">http://bitecode.co.uk/?p=8</guid>
		<description><![CDATA[Over the last few months I&#8217;ve found myself playing with binary numbers a great deal - for investigating various algorithms, for working out bit masks, for understanding bit-twiddling operations (such as using xor for clearing a register in assembly language), etc. At the moment, I&#8217;m predominantly a Python user (I love the clean syntax, the [...]<script type="text/javascript">SHARETHIS.addEntry({ title: "Binary Representation of Numbers in Python", url: "http://bitecode.co.uk/2008/07/binary-representation-of-numbers-in-python/" });</script>]]></description>
			<content:encoded><![CDATA[<p>Over the last few months I&#8217;ve found myself playing with binary numbers a great deal - for investigating various algorithms, for working out bit masks, for understanding bit-twiddling operations (such as using xor for clearing a register in assembly language), etc. At the moment, I&#8217;m predominantly a Python user (I love the clean syntax, the power and speed, and the compatibility with C), but I&#8217;ve found no easy way of representing numbers in binary form in Python (or indeed in any other language).</p>
<p>Clearly the ability to represent numbers in binary form would have little practical use in a programming language, but for purely academic reasons I decided to write a very basic binary class in Python. At the moment it only supports positive integers (its just plain base 2, not two&#8217;s complement), and is limited in its range of features, but I have found it to useful on occasion (mostly when using Python in the command line mode).</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">class</span> Bin<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, num<span style="color: black;">&#41;</span>:
        num = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>num<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#91;</span>d <span style="color: #ff7700;font-weight:bold;">for</span> d <span style="color: #ff7700;font-weight:bold;">in</span> num <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>d<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>:
            <span style="color: #ff7700;font-weight:bold;">raise</span><span style="color: black;">&#40;</span><span style="color: #008000;">TypeError</span>, <span style="color: #483d8b;">'not a binary number'</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.__num = <span style="color: #008000;">self</span>.__bin_str_to_int<span style="color: black;">&#40;</span>num<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> __bin_str_to_int<span style="color: black;">&#40;</span>num<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">sum</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">**</span>n <span style="color: #ff7700;font-weight:bold;">for</span> n <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>num<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>num<span style="color: black;">&#91;</span>-n<span style="color: #ff4500;">-1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> __get_bin_str<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        binstr = <span style="color: #483d8b;">''</span>
        n = <span style="color: #008000;">self</span>.__num
        <span style="color: #ff7700;font-weight:bold;">while</span> n <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">0</span>:
            binstr = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>n <span style="color: #66cc66;">%</span> <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span> + binstr
            n /= <span style="color: #ff4500;">2</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> binstr
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__int__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.__num
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__repr__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.__bin_str<span style="color: black;">&#41;</span>
&nbsp;
    __bin_str = <span style="color: #008000;">property</span><span style="color: black;">&#40;</span>fget=__get_bin_str<span style="color: black;">&#41;</span></pre></div></div>

<p>Here is an example of its basic usage:</p>
<pre>>>> from bintype import Bin
>>> x = Bin(11001100)
>>> x
11001100
>>> int(x)
204
</pre>
<p>In it&#8217;s current state it is of little use, but with some simple operator overloading (__add__, __lshift__, etc.) and support for negative numbers using two&#8217;s complement it could provide a useful way of testing basic operations on binary integers.</p>
<p><a href="http://sharethis.com/item?&wp=2.5.1&amp;publisher=b8e8e47b-e8f1-4814-bb9c-d1bf2397550a&amp;title=Binary+Representation+of+Numbers+in+Python&amp;url=http%3A%2F%2Fbitecode.co.uk%2F2008%2F07%2Fbinary-representation-of-numbers-in-python%2F">ShareThis</a></p>]]></content:encoded>
			<wfw:commentRss>http://bitecode.co.uk/2008/07/binary-representation-of-numbers-in-python/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
