0

For protecting passwords, hash value of that password is stored in database. I am using this code for generating hash value of a string: http://www.exampledepot.com/egs/javax.crypto/GenMac.html

But i found that it generates random hash for the same string each time. So i am not able to use it for password validation.

How to make that code generate hash value based on the String only ? That is, if i input "iloveyou" it should generate the same hash value each and every time. Not a different one.

Vpp Man
  • 2,384
  • 8
  • 43
  • 74
  • In general, don't use MD5 (for anything: it is considered broken), and don't use a fast hash for password hashing, use a slow one. See [What makes a hash function good for password hashing?](http://crypto.stackexchange.com/questions/24/what-makes-a-hash-function-good-for-password-hashing) for details. And of course, don't include (non-reproducible) random data in your hash if you want it to be the same every time. – Paŭlo Ebermann Feb 11 '12 at 16:56
  • thanku for the link. I am new to JSP and in JSP there is no inbuilt function to easily produce the hash. But in PHP, there is md5(), shah1(), etc. So i am looking for jsp implementation of these. – Vpp Man Feb 11 '12 at 18:31
  • In JSPs you can use the whole Java library, and hash functions are available with `java.util.Digest`. Though you should look if you can use PBKDF-2 or bcrypt (with a sufficiently high work factor) instead of a simple fast hash function, for reasons mentioned in the answer linked before. – Paŭlo Ebermann Feb 11 '12 at 19:23
  • thanku. I am trying to use a fixed salt now. But i am not sure how to pass a string as the salt. Eg: `a1Rj*EE` as a salt. Reference: https://www.owasp.org/index.php/Hashing_Java Will this work: `byte[] bSalt = new byte[8];` `bSalt=base64ToByte("a1Rj*EE");`? Can i pass a long string to that function also ? – Vpp Man Feb 12 '12 at 05:12

2 Answers2

2

The code you reference is not simple MD5, it is HMAC-MD5, which includes a key. The first section of code, involving KeyGenerator is generating a random key every time you run it. That different random key is giving you a different HMAC result each time.

If you just want a simple MD5 hash, then you will need to use code for MD5 alone, without the HMAC part. You may also want to look into the use of cryptographic salt to protect password hashes, otherwise users with the same passwords will have the same hash. See Difference between Hashing a Password and Encrypting it for a discussion.

Community
  • 1
  • 1
rossum
  • 15,344
  • 1
  • 24
  • 38
  • thanku. I understand that hash is irreversible. That is why i am using it for passwords. So KeyGenerator is creating a different key each time? How to change the code so that i can do only MD5 with the same key for all the strings ? In PHP, what i do is call md5() passing the password. – Vpp Man Feb 11 '12 at 18:26
  • You are not currently doing a hash, you are doing an HMAC (http://en.wikipedia.org/wiki/HMAC). I'm afraid I do not know PHP, so I can't help you specifically with that. Look for PHP code for MD5, not for HMAC-MD5. They are different things, and give different results. – rossum Feb 11 '12 at 18:38
  • thanku. What I want to do is to store only the hash value of a password when user registers. Later when user logins, password entered by him is hashed and compared with the hash value stored in database. I found this crpto lib from here:http://stackoverflow.com/questions/9227550/library-with-hash-functions-jsp But the example code will generate a new key each time. So, how do i use a MD5 hash alone with a text key(or a default key) and to generate a hash, in JSP ? – Vpp Man Feb 11 '12 at 18:43
1

Several things here:

  1. In order to manage passwords, you should not simply use an MD5 hash. But the problem there is not as much the algorithm (MD5, which --the other users are right-- is weak) but the bit about "simply using". You should do more than just applying your hash function, whichever it is. You should apply a random salt in a specific way, and you should iterate your hash function.

  2. Precisely, that "random salt" will make your encryption infrastructure create a new different result each time you execute it for the same input, something that seems to be confusing you -- but do not worry, that's OK! And you will be perfectly able to use it to compare passwords.

...and you may ask... how can this be and how can I do it right? I suggest you read this article I wrote: http://www.jasypt.org/howtoencryptuserpasswords.html where everything is explained. And in fact, I would also suggest you using jasypt for encrypting your password. It's easy and performs all these operations out-of-the-box.

Regards.

Daniel Fernández
  • 7,335
  • 2
  • 30
  • 33
  • thanku. That tutorial is very easy to understand. I understand the use of fixed salt. But still i do not get the idea of using random salt. Because each time it will generate a new hash. So, how do we use it ? Store this random salt generated in an additional field for each record and use it during login ? ie. use the stored random salt (random for each record) to digest the password submitted by user during login ? Correct ? – Vpp Man Feb 12 '12 at 04:38
  • 1
    @VppMan Yes, you should have a per-user random salt, generated once at password storage time and stored together with the hash. – Paŭlo Ebermann Feb 12 '12 at 11:56