views:

3437

answers:

5

In a Java application stack with Spring & Hibernate (JPA) in the Data Access Layer, what are good methods of applying the password encryption (hopefully using annotations), and where can you find out more about getting it done (tutorial, etc)?

It's understood that I would use a JCA supported algorithm for encrypting the passwords, but I would prefer to not have to implement the wrapper logic if there is an easy way.

I was looking at Jasypt, and was a) wondering if that's a good option and how to do it and b) what else people are using for this. If anyone is using Jasypt or an alternative, details of your experience it would be great.

A: 

I just use something similar to SHA-256(username + ":" + password + ":" + salt) and store it in the database in a 64-character column called passwd.

Wikipedia says, relating to salts: "Salt data complicates dictionary attacks that use pre-encryption of dictionary entries: each bit of salt used doubles the amount of storage and computation required. ... For best security, the salt value is kept secret, separate from the password database. This provides an advantage when a database is stolen, but the salt is not."

So to authenticate, get user from database with supplied username, then generate the same hash using the password provided via their login attempt, and compare to that in the database. Also add in some rate limiting for login attempts (e.g., 5 per 5 minute period). If the user forgets their password, NEVER email them the password (as you won't have it stored), nor email them a new generated password, but email them a link to change that password with a change password key/nonce/salt in the URL that you can check against.

JeeBee
I edited my question - I wasn't looking for the algorithm, but for a standard (ie library implemented) way to use the JCA algorithms with a Spring/Hibernate stack.
stevedbrown
So you'd really want something like <code>@Password("SHA-256")</code> annotation on a field in your user account model, and for your framework to automatically handle validation in the background? At some point you start asking a framework to be too specific in terms of functionality, and sacrificing flexibility.
JeeBee
+1  A: 

MD5 or SHA-256 would be fine, although MD5 is crackable now.

Maybe I misunderstood the problem, but it should be just comparing the hashed passwords.

In hibernate, just store as a String. On the validation side, have a method like:

public validate(String user, String pass)
{
    if(getUser(user).getPass().equals(getHash(pass)))
        return true;
    return false;
}
Jesse
Same comment, I wasn't looking for an architecture or algorithm to use - just how people actually do it.
stevedbrown
Is this more what you're looking for?http://www.evolt.org/node/60122
Jesse
That describes what I'm doing now - I was wondering if there's a way to annotate hibernate properties or something to do this automatically. Seems cleaner.
stevedbrown
I've never come across anything like that. You could right your own AOP pointcut to do it based off an annotation, but that seems like overkill for this scenario.
Jesse
+2  A: 

Java has all of the required libraries already provided for you. Simply create a utility method that implements hashing with a salt as described at OWASP.

If you really don't want to own that code and don't mind an extra dependency, it seems that the Ki library (formerly JSecurity) has an implementation of what is described by OWASP.

It also looks like the JASYPT library you mentioned has a similar utility.

I realize that this answer doesn't mention Spring or Hibernate but I'm not clear how you are hoping to utilize them in this scenario.

laz
+1  A: 

You can use Jasypt with Hibernate to encrypt or hash your properties on the fly if thats what you're looking for. The actual algorithm for computing digests (hashes) is pretty simple using the JCE if you want to roll your own as well.

Kevin
I don't think the JASYPT with Hibernate approach is ideal for passwords since it is possible to decrypt the value. Passwords should be security with a one-way digest.
laz
JASYPT does support this - http://www.jasypt.org/howtoencryptuserpasswords.html.
stevedbrown
I saw that, but it doesn't seem that the Hibernate integration that they provide takes advantage of it. The Hibernate integration is only for encrypting/decrypting, not one-way digesting.
laz
A: 

The company for which I work is considering whether to use jasypt or not. We currently have an app using Hibernate that may be installed on one of four database flavors. If we go ahead and use it, I'll return and provide details on my findings.

Omniwombat