Archive

Posts Tagged ‘Java’

Howto implement MySQL’s OLD_PASSWORD() in Java

July 23rd, 2010 makii Comments off

Like most Software Engineers I don’t have the luxury to start with an greenfield strategy. 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.

Recently I had to migrate Newsletter subscribers to a newly created subscription system which uses an Oracle Database (I would have preferred PostgreSQL) rather than a MySQL, as the old system does.

Programmers are lazy, and as expected the developers of the old system used the OLD_PASSWORD(str) function from the database available to hash the password entered by the user. This is a very convenient way to protect the users’ 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:

  • Send every user a new password. Bad, we don’t want to harass them with our technical issues.
  • Force them to set a new password when they log in the next time. See above
  • Try to find a solution to validate the password against the old password hash.

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 OLD_PASSWORD() function in C# at yourhelpcenter.de (attention, german) and together with my coworker Maurice we ported it to Java, resulting in this piece of code:

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 & 63) + add) * tmp) + (nr << 8);
            nr2 += (nr2 << 8) ^ nr;
            add += tmp;
        }

        result[0] = nr & ((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();
    }

I give no guarantee this will work in all cases. My IDE complains all over the place about possible integer overflows. The usage of Integer.toHexString()did not work either, as the resulting String is not padded up with zeroes.

Finally, some Unit Tests for the interested user:

    @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);
    }

Have fun with it!

Categories: Java, Technology, Web Tags: , , , ,

Java – The Movie

June 27th, 2010 makii Comments off

For all you out there who code java like me :-)

And everyone else…

via @pommesbude

Update:Youtube seemingly has killed the video, but the original version is still available here.

Microsoft’s “Apple Tax”. WTF?

April 12th, 2009 makii Comments off

After Microsoft’s ad starring Jerry Seinfeld and Bill Gates, which was the first response to the well-known Get a Mac commercials, was kind of a flop, they recently started the Laptop Hunters series. Watching it is kind of funny. Mostly, for my part, because of the reactions of the people when they’re presented with the money for their computer-to-be.

Apart from that, the Company From Redmond commissioned, or sponsored, as they call it, an analysis from Endpoint Technologies Associates, which should transparently describe the “Apple Tax”. At the first glance the form (filled out by hand, as it seems) looks pretty convincing, stating an overall saving of more than 3300 bucks when you buy a PC instead of a Mac. Is that really true?

Ina Fried from Cnet took a closer look at the findings and is not convinced. The report states no additional license fees for the Windows systems despite the OEM licenses included in the hardware price. Where do they get there Software from? Do they use OpenOffice? Do they use copies from their friends illegally? Why do they list the (totally optional) MobileMe account? This account on it’s own bills for about $750 in the five years. I know only one person who uses MobileMe and he will discontinue it.

Sure, Apple hardware is more expansive in comparison to PC hardware, that’s true. But as the Windows Experience Blog states correctly, it’s not only about cost, but about value. I’m an Apple user for about a half year now and I think I get a lot more value for my money than for my previous laptops, which were PC systems. In germany we have two words to describe this difference. The word which describes a possibly good deal is “günstig”, which translates well to cheap as in low priced. “billig” also means cheap, but more in a kind of this product’s kinda crappy way. A lot of PC hardware in the past was “billig”, and the present one often still is. The hardware of these systems does not fit together well, the cheap ones still have a ridiculously low resolution (I will never work willingly on anything below 1400×900 on 14″ or 15″ screens). The chassis stability of Macs might be reached by Thinkpads, but the Design I think is still leading, though it is quite simply.

To the software. I had installed Windows on my previous PC systems, and I even have a BootCamp installation with Windows XP on it on my Mac. I boot it two or three times a month to play games which unfortunately are not available for OSX (Assasins Creed and Fallout 3) and don’t work with Crossover Games properly. That’s all I use it for, and, hell, that’s all I need it for. The last five years I solely worked on Unix- and Linux- based systems. And I like it. Instead of clicking around endlessly in the explorer or the System Control I have a powerful shell (zsh or bash) and I can do everything on the fast lane. For the direct functions of the operating system the clicking point is also valid for OSX. But the simple gestures on the trackpad, or the Expose configuration is so addictive I tried to switch workspaces on my Linux workstation in the office by moving the mouse pointer to the upper right corner only two days after I bought my first Mac (yeah, I know. This might become a problem someday).

The important part for me is: Mac OSX, while shiny and colorful, has a Unix foundation, which is (IMO, even if not open source), a good thing. No ugly Cygwin necessary. There is MacPorts. There is Fink. Lots of stuff I need for everyday life. And the parts which come with OSX UI are most natural and intuitive.

To the point. I like Apple Hardware and Software, because

  • It looks kinda cool. This sure is one major point why Apple is so successful.
  • The hardware is sturdy.
  • The software fits my needs.
  • There is not that much malware, spyware and other bad stuff for OSX, if any.
  • There is a usable Unix environment underneath all that glamour.
  • I am not ridicuously accurate about free software licenses, though I like and respect free software in general.

I have stopped looking into Microsoft’s operating systems since Windows 2000. As mentioned before I’ve run XP on my Samsung X20 and MacBook Pro for gaming only. This was good as I can tell you now. Having started at a new Job I need to speak with a funny Exchange Server from Redmond Industries which I could not get up and running with anything I tried with Evolution, so I continued using the pre-installed Vista. It looks good enough for now, but still is a plain continuation of the user interface concepts from Windows 95. I still have to use the embedded context menue to create a new folder. It still comes with 2 tons of funny software preinstalled which needs to be cleaned out before you can somehow work with the system. Every tool which should bring me some sane Virtual Desktop support is either broken somehow, or is totally cumbersome to use. I think this will be my next desktop wallpaper…

I wish I had a Mac.

But this would not help either, this mail setup does not work for Entourage either.

So I’m (again, for once) stuck with Windows and no light at the end of the tunnel. I think I’ll, if time allowes, play a bit more with an Ubuntu in some virtual box setup and try to get Mail access up and running with Linux. Till then I need to work around with the most sane environment on windows, which still is Cygwin, which offers a sane xterm, zsh/bash, screen and some other utilities which I never want to miss.

Comming back to the topic of this post: There sure is some kind of “Apple Tax” if you want to call it that. BUT I really really doubt that it is so high as described in the Microsoft-paid paper. My opinion is that users get more value for a higher price. Some of the added value is purely aestetic, another is less pain in the ass, as they don’t bloat up their registry with a ton of funny entries for installing software they simply want to try, viruses, malware, The Search For The Driver(tm) and other stuff. The whole software suit from Apple is much more integrated and more simple to use. Every user has to decide whether they think it’s worth the price for them. If (probably) crappy hardware with Windows works for them – who am I to judge?

Despite that, the Get a Mac ads from Apple are more conciliabe, meaning they acknowledge the strengths of the PC to some degree, even if in a comical way. Laptop Hunters is a more offensive and absolutistic approach to compare the two systems, telling the customers Apple is stealing money from them. That’s business, OK, but it’s totally unfair.

Categories: Java, Mac, Windows Tags: , ,

String literals

June 2nd, 2008 makii Comments off

When coding in Java, I usually try to avoid string literals in the code, especiall when they reference other software components like templates, which are filled with values in arbitrary places throughout the code. This is what I found in a project which we’re going to insource soon:

#-[ user@host ]- ~/work/project/trunk
#-[0]-> find . -type f -name "*.java" -exec grep -r "\"salutation2\"" {} \; | wc -l
24
#-[ host@host ]- ~/work/project/trunk
#-[0]-> kill -9 $SOFTWARE_SERVICE_PROVIDER

No comment.

Categories: Coding, Java Tags: ,

Review of Neil Gafters’ talk

March 24th, 2008 makii Comments off

Yes, it’s a little late, but so what?

About two weeks I visited a quite interesting talk by Dr. Neil Gafter, developer of Google Calendar, whom, in his spare time, proposes new language features for the Java Programming Language and Sun’s javac-Java compiler. The latest proposes BGGA closures, in conjunction with other nice features like control abstraction, function types and lots of other pretty useful things,is under heavy discussion.

At first I was unsure what to think about closures, lots of people compained about the syntax, and that noone ever will understand the code when here are some function types in it. But, honestly, can it get any more confusing than with generics? NEVER!!. During the talk I recognized the pattern, which clearly is derived from the code blocks I used to love in Smalltalk. Clearly there are some slight differences taking into account that Java is, contrary to Smalltalk, a strongly typed language.

Further than that, it gets really nice when using closures in conjunction with control abstraction: Often used example, which actually doesn’t make sense, as Java5 has a foreach loop built in, but let’s just say someone wants to implement a method like foreach, taking as parameter any type of Collection<T>, as well as a block of code, which itself takes as parameter one item of T. This method then can be imported statically by any class which has need of this particular foreach loop. This abstract functionality is, right now, hard-wired in the java compiler to the Java Collections framework from the runtime environment. *hick* Yes, exactly the way around it should NOT be.

Closures in conjunction with control abstraction have the power to enable APIs to implement their own control statements like, for example, a foreach-loop for Collections or Maps, performance logging, cleanup work, whatever you can imagine. Despite my reservations in the beginning, I can’t wait to see the BGGA proposal into Java7 or Java8. Hopefully we will see it!

Categories: Java, Technology Tags: , ,

Neil Gafter in Cologne

March 7th, 2008 makii Comments off

For everybody who doesn’t know yet: This monday Dr. Neil Gafter visits the JUGCologne and gives a talk about JDK 7 language features and Closures.

Surely he wants promote his BGGA closures proposal, which I personally don’t like this much. Function types is a powerful tool, and Java already has a weak kind of function types, namely inner anonymous classes. Of course they are quite inflexible in comparison to the function types suggested, but anyway, they are pretty unreadable, really.

Voices on weblogs.java.net rise and defend Dr. Gafters’ proposal by telling “we have IDEs which support syntax highlighting”, but hey what kind of argument is that, when my  boss tells me  he will, from time to time, check my code using a plain editor, as he doesn’t want to install eclipse and frankly, really has no need to. It pretty often gets mixed up when using generics.

We’ll see what he tells us next monday.

Categories: Technology Tags: