17

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.

2
  • I've always stored the hash of the password rather than the actual password. That way you can still verify it, but you don't have any of the responsibility of storing it.
    – Jesse
    Jul 2, 2009 at 15:45
  • 1
    Right, that's exactly what I want to do - just trying to find a library that will help with the hashing + querying over an ORM layer. Jul 2, 2009 at 15:46

6 Answers 6

7

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 Shiro 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.

3
  • Where you trying to encrypt the database password in the hibernate config file? How did you end up doing it? I am having issues with hibernate 5. Aug 8, 2018 at 0:50
  • @pitchblack408, this is for obscuring passwords in the application data, such as for the users of the system, not the password used to open the database connection.
    – laz
    Aug 9, 2018 at 18:05
  • Thanks, I see that now after reading all the documentation Aug 9, 2018 at 18:07
5

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.

4
  • 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
    Jul 2, 2009 at 17:12
  • JASYPT does support this - jasypt.org/howtoencryptuserpasswords.html. Jul 2, 2009 at 17:57
  • 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
    Jul 2, 2009 at 18:22
  • I looked into this, and laz is right -- the hibernate encryption is not digest-based, and so not suitable for passwords. From the documentation, it looks like the StrongPasswordGenerator of Jasypt will work. Mar 31, 2011 at 5:11
2

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;
}
4
  • 1
    Same comment, I wasn't looking for an architecture or algorithm to use - just how people actually do it. Jul 2, 2009 at 16:21
  • Is this more what you're looking for? evolt.org/node/60122
    – Jesse
    Jul 2, 2009 at 16:33
  • 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. Jul 2, 2009 at 17:28
  • 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
    Jul 2, 2009 at 17:39
2

There doesn't seem to be a Hibernate specific way to do it with Jasypt, but you can set up a password encryptor in Spring:

  <!-- 
   Set up string digester here so we can configure it for more pools if it's a problem... 
  -->
  <bean id="stringDigester" class="org.jasypt.digest.PooledStringDigester">
    <!-- same settings as StrongPasswordGenerator -->
    <property name="poolSize" value="2"/>
    <property name="algorithm" value="SHA-256"/>
    <property name="iterations" value="100000"/>
    <property name="saltGenerator">
      <bean class="org.jasypt.salt.RandomSaltGenerator"/>
    </property>
    <property name="saltSizeBytes" value="16"/>
  </bean>

  <!-- ...and then we only have to deal with the passwordEncryptor interface in code. -->
  <bean id="passwordEncryptor" class="com.myproject.util.StringPasswordEncryptor">
    <property name="stringDigester" ref="stringDigester"/>
  </bean>

After that, you call context.getBean("passwordEncryptor") to get the encryptor, and then call either encryptPassword() or checkPassword().

2

If you are using Spring in your application, then you can also use Spring Security,which provides you with several password encoders, i.e. ShaPasswordEncoder You can find it on StackOverflow

0

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.

2
  • 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. Jul 2, 2009 at 16:02
  • 1
    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
    Jul 2, 2009 at 21:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.