I am using Hibernate / Java to store an object in a database. The object has a password field, which is a String . When registering a user in my application, I use a password using SHA-1 (I admit that this is a little weak). This creates byte [] , which is then converted to String using new String(byte[] arr); Whenever I want to log in, I simply retrieve the hashed password from the database (like String ) and compare it with the password of the login password at login using hashedPasswordFromDatabase.equals(SHA1_HASH(inputPassword));
This worked fine on my development system (Windows 7, JDK 1.6.0_23 / JDK 1.7, MySQL 5.5, Tomcat 6.0.26), but after deploying it to our server (running JDK 1.6 on Linux), the equals method never evaluates to TRUE even for equal passwords. I quickly set up a new development system (Ubuntu 12.04, MySQL 5.5, JDK 1.7.0_03, Tomcat 7.0.22), and it also does not work.
I know the possible encoding problems mentioned in the Java API documentation for the String class, as well as in several places here in SO. I tried a couple of the encodings suggested in this forum (e.g. Base64, Latin-1), and I ended up with a UnsupportedEncodingException . I think I better avoid string conversion. So, how do I create my database so that the entity class generated by Hibernate includes byte [] for the password field instead of String ?
Sayo oladeji
source share