
<?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>Research and Destroy &#187; mysql</title>
	<atom:link href="http://research-and-destroy.de/blog/tag/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://research-and-destroy.de/blog</link>
	<description>... using advanced technology</description>
	<lastBuildDate>Sun, 18 Dec 2011 22:38:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Howto implement MySQL&#8217;s OLD_PASSWORD() in Java</title>
		<link>http://research-and-destroy.de/blog/2010/07/23/howto-implement-mysqls-old_password-in-java/</link>
		<comments>http://research-and-destroy.de/blog/2010/07/23/howto-implement-mysqls-old_password-in-java/#comments</comments>
		<pubDate>Fri, 23 Jul 2010 19:30:12 +0000</pubDate>
		<dc:creator>makii</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[hash]]></category>
		<category><![CDATA[legacy]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[OLD_PASSWORD]]></category>

		<guid isPermaLink="false">http://research-and-destroy.de/blog/?p=200</guid>
		<description><![CDATA[Like most Software Engineers I don't have the luxury to [...]]]></description>
			<content:encoded><![CDATA[<p>Like most Software Engineers I don&#8217;t have the luxury to start with an <em>greenfield strategy</em>. Most times we know a good and viable solution to a problem, but cannot implement it due to restrictions which come with legacy systems and stuff out of our control. </p>
<p>Recently I had to migrate Newsletter subscribers to a newly created subscription system which uses an <a href="http://www.oracle.com/us/products/database/index.html" title="Oracle Databases">Oracle Database</a> (I would have preferred <a href="http://www.postgresql.org/" title="PostgreSQL">PostgreSQL</a>) rather than a <a href="http://www.mysql.com/" title="MySQL database">MySQL</a>, as the old system does.</p>
<p>Programmers are lazy, and as expected the developers of the old system used the <code><a href="http://dev.mysql.com/doc/refman/5.1/en/encryption-functions.html#function_old-password" title="OLD_PASSWORD in the MySQL Reference Manual">OLD_PASSWORD(str)</a></code> function from the database available to hash the password entered by the user. This is a very convenient way to protect the users&#8217; login credentials, but rather bad when migrating to another system which has different or none implementations of this functionality. So what do we do? The options are:</p>
<ul>
<li>Send every user a new password. Bad, we don&#8217;t want to harass them with our technical issues.</li>
<li>Force them to set a new password when they log in the next time. <em>See above</em></li>
<li>Try to find a solution to validate the password against the old password hash.</li>
</ul>
<p>As MySQL is widely used and a lot of data migration happens to and from, someone must have been run in this issue already, and most likely there is a solution to this problem in the net. And there is. I found a reimplementation of the <code>OLD_PASSWORD()</code> function in <em>C#</em> at <a href="http://www.yourhelpcenter.de/2009/06/mysql-alten-md5-hash-in-c-berechnen-16-stellig/" title="C# implementation of OLD_PASSWORD()">yourhelpcenter.de</a> (attention, german) and together with my coworker Maurice we ported it to Java, resulting in this piece of code:</p>
<p><code>
<pre>public static String mysqlOldPassword(byte[] password) {
        int[] result = new int[2];
        int nr = 1345345333;
        int add = 7;
        int nr2 = 0x12345671;
        int tmp;

        int i;
        for (i = 0; i < password.length; i++) {
            if (password[i] == ' ' || password[i] == '\t')
                continue;

            tmp = (int) password[i];
            nr ^= (((nr &#038; 63) + add) * tmp) + (nr << 8);
            nr2 += (nr2 << 8) ^ nr;
            add += tmp;
        }

        result[0] = nr &#038; ((1 << 31) - 1);
        int val = ((1 << 31) - 1);
        result[1] = nr2 & val;
        String hash = String.format("%08x%08x",result[0],result[1]);
        return hash.toLowerCase();
    }</pre>
<p></code></p>
<p>I give no guarantee this will work in all cases. My IDE complains all over the place about possible integer overflows. The usage of <code>Integer.toHexString()</code>did not work either, as the resulting String is not padded up with zeroes.</p>
<p>Finally, some Unit Tests for the interested user: </p>
<p>
<pre>    @Test
    public void testOldPassword() throws Exception {
        final String expected = "0414ac6137ee1adc";
        byte[] bytes = "fooo".getBytes("UTF8");
        String foo = mysqlOldPassword(bytes);
        assertEquals(expected, foo);
    }

    @Test
    public void testOldPassword2() throws Exception {
        final String expected = "3fa0dce62ba931b5";
        byte[] bytes = "hastewas".getBytes("UTF8");
        String foo = mysqlOldPassword(bytes);
        assertEquals(expected, foo);
    }</pre>
</p>
<p>Have fun with it!</p>
<p class="wp-flattr-button"></p>]]></content:encoded>
			<wfw:commentRss>http://research-and-destroy.de/blog/2010/07/23/howto-implement-mysqls-old_password-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

