Wednesday, July 16, 2008
watch out - md5 sum bug
It was a rude shock for me.
This was the piece of code that I had,
String unencryptedValue= "test"; MessageDigest m= MessageDigest.getInstance("MD5"); m.update(unencryptedValue.getBytes(),0,unencryptedValue.length());
String encryptedValue = new BigInteger(1,m.digest()).toString(16);
Then it took me some time to realize that the BigInteger casting was causing the havoc, and at the same time waruna gave me a link to the following blog http://jdwyah.blogspot.com/2006/08/java-md5-password-hash.html, which has the solution for the above problem,
its just a matter of introducing another method which would pad the leading zero's to the md5 value to make up the required length.
the fixed code looked something like below,
String unencryptedValue= "test"; MessageDigest m= MessageDigest.getInstance("MD5"); m.update(unencryptedValue.getBytes(),0,unencryptedValue.length());
String encryptedValue = new BigInteger(1,m.digest()).toString(16);
encryptedValue = fixLeadingZeroBug(encryptedValue, 32, '0');
public String fixLeadingZeroBug(String s, int length, char pad) { StringBuffer buffer = new StringBuffer(s); while (buffer.length() <> buffer.insert(0, pad); } return buffer.toString(); }
It was just now a matter of fixing the bug and instead I learnt an important lesson to be a mindful of these leading zero's when ever an integer is used in place of having it as a String for intermediate processing. But unfortunately I had to learn it the hard way. :(
Wednesday, July 9, 2008
Factory Method
I had a big surprise at the presentation when I suddenly found that the presentation that i was making was being recorded on video. Probably I should be more careful when i make my next one as I leaving evidence of all the lies I say at the presentation.... haha
Composite pattern
And then came the question of the day! Harsha presented the audience and myself with a question to try.
He simply asked us to use the composite pattern to solve a search problem????
"The question"
Suppose you have a gui like follows,
It was a nice question to make us think about the composite pattern and its practical application.
This in turn led to an interesting discussion on topic.
The solution to this, as explained by romith, was to have the AND and the OR as the nodes and the criteria like the Name , City etc as the leaves!
Initially I thought that the Name, City and NIC could be made into nodes and have the AND and OR as the leaves.It was quiet rightly pointed out by Harsha that it would work , but was not the correct answer, as it was not a scalable solution and having it the other way around would provide with an extensible and a scalable solution.
I ended this presentation with a lot of self satisfaction as it was a presentation that I left with a clear picture about the topic than the idea that I had at the start of it! But ironically I was the presenter.. haha poor audiance!
Saturday, June 21, 2008
Abstract Factory Pattern
A wait the next design pattern slides!
Thursday, June 19, 2008
Google , Yahoo and Microsoft does it matter to me?
The history behind this present situation is very interesting and plenty has been written about it and analyzed about these three companies, Google, Yahoo and Msft. Msft has been the forefront of providing user friendly but proprietary OS and its chairman has been topping the worlds richest list since Msft became a household name in the early part of 90's.
Yahoo, had its humble beginnings as ones hobby went onto become mother of all Internet based companies established itself in the forefront of Internet marketing, till the emergence of a start up in the name of Google, who fought a rough battle with Yahoo and incidentally dethroned it from its established position to become the leader. The tactic of having contextual marketing and along with it a more powerful search algorithm against a business that was already established.
Being just another observer of this battle for some time I have my own thoughts on the matter. I had always been mesmerised by the rise of the Google and happy to see that it is a good example of how simplicity can succeed even in the very dynamic business environment, specially its simplicity in interfaces and the meteoric rise it has gone through from its humble beginnings had always been things that I had been interested about. Any how personally does not like the idea of yahoo being brought under its wings, if we are to have a sustainable Internet market probably we need a healthy competition and not a monopoly. Only in that case will be the users like us will be befits from the occasional spills they make in the event of cut throat competition. I can remember the way the web mail account size suddenly started to burst and come to age! That happened for no other reason than competition.
But on the other hand yahoo falling pray to Msft is worse than that! So probably from a users perspective I would love to see the mooted partnership between yahoo and Google might be the best for us, as its obvious that yahoo is not in a position to convince its investors unless it comes up with some sort of a viable strategy to generate sufficient cash in the fore see able future.
Thursday, June 5, 2008
Intelligent Ant

You might have heard about swarm intelligence where a group of ants acting as in concert exhibit intelligent behavior. But when these ants are considered individually they do not normally exhibit any intelligent behavior. But the other day I was in for a strange surprise when the hsenid ant individually exhibited intelligent behavior, when it was placed in captivity and harassed by tying objects on its sensors, it reacted correctly to suit its hostile environmental condition.
The news reaching us from sources related to the ant says that the ant is planning a great escape from its abductor and might end up in one of the other tables nearby, where it can have its eye open!
Wednesday, April 30, 2008
Playing around with memory tables
So I thought of testing it and I did the following,
initially I set the following in my.cnf
[mysqld]
max_heap_table_size = 8K
then I tried the following,
alter table City2 engine=memory;
ERROR 1114 (HY000): The table '#sql-2bc1_1' is full
and as soon as I checked
show create table City2;
City2 | CREATE TABLE `City2` (
`ID` int(11) NOT NULL auto_increment,
`Name` char(35) NOT NULL default '',
`CountryCode` char(3) NOT NULL default '',
`District` char(20) NOT NULL default '',
`Population` int(11) NOT NULL default '0',
PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=4080 DEFAULT CHARSET=latin1
It was clear that no changes has taken place and the alter table has completely failed and the table continue to be MyISAM,
After that just to confirm it I did the following,
set global max_heap_table_size=2*1024*1024;
alter table City2 engine=memory;
City2 | CREATE TABLE `City2` (
`ID` int(11) NOT NULL auto_increment,
`Name` char(35) NOT NULL default '',
`CountryCode` char(3) NOT NULL default '',
`District` char(20) NOT NULL default '',
`Population` int(11) NOT NULL default '0',
PRIMARY KEY (`ID`)
) ENGINE=MEMORY AUTO_INCREMENT=4080 DEFAULT CHARSET=latin1
So finally I know for sure that there will be no data truncation when you issue an alter table statement to convert to Memory. Hopefully this will be useful to the guys who are playing around with memory tables these days :)
PS: I tested this on mysql 5.0.27-max-log