views:

269

answers:

2

Grails 1.1 onwards has 'encodeAsMD5' available on Strings -- is there a way to provide a salt for the function?

Typical usage:

${myString.encodeAsMD5()}

Another option would be to use the Apache DigestUtils class.

I'm not using this to do password hashing -- instead, I'm using it for a verification mechanism to determine when a request was created and how much time elapsed when the response was received.

To start out, I encrypt the date/time of when a request was initially created and pass it over to the client. Later when the client sends some data back, it includes the original hash value which is then used in determining how much time has passed.

+2  A: 

Are you wanting to have a salt because you're trying to do password hashing? If so, please consider jBCrypt, a secure, really easy-to-use, password hashing library. You can probably easily hook it in as a Groovy string metamethod. :-P

And if you're not doing password hashing, I'm not sure where a salt would come in; please feel free to elaborate in that case. :-)


Example of how to use HMAC to do keyed hashing (I used HmacMD5 in my example, but you can use HmacSHA1, HmacSHA256, HmacSHA384, or HmacSHA512 also):

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public byte[] hmacMD5(byte[] key, byte[] data) {
    Mac mac = Mac.getInstance("HmacMD5");
    mac.init(new SecretKeySpec(key, "HmacMD5"));
    mac.update(data);
    return mac.doFinal();
}

I'm sorry that this is in Java and not Groovy, but hopefully it'll be easy enough for you to adapt. :-) Anyway, in this example, your constant value would be the key, and the incoming data would be the data.

If your data is sizeable (unlikely from what you've described), you can call update multiple times, and the hash would work as though the data from those calls were all concatenated together.

Chris Jester-Young
I updated my question to clarify. Not using this for password hashing.
Robin Jamieson
@Robin: Based on your question edit, am I to take it that the "salt" here refers to the timestamp you're sending to the client, which you are expecting it to send back verbatim?
Chris Jester-Young
If so, one way to verify the hash is to use something like HMAC-MD5, and use your timestamp as the HMAC key.
Chris Jester-Young
no the salt is a constant value. timestamp is what I'm hashing and it will be unique each time. So salting is only necessary to make passwords unique but not for other scenarios?
Robin Jamieson
@Robin: In that case, using HMAC-MD5 (with your constant as the HMAC key), and hashing the timestamp, is definitely the right way to go. I'll update my answer with a code example (in Java, but easy enough to adapt to Groovy).
Chris Jester-Young
@Robin: Salt is not needed to make a password hash unique. Salt is used in passwords to make Rainbow table attacks more difficult. http://en.wikipedia.org/wiki/Rainbow_table
Heinrich Filter
@Robin: Oh, oh! Did I forget to mention, that once you call `doFinal`, the state of the MAC is reset, and you can start hashing your new batch of data---it will automatically use the same key. Very handy, since you have a fixed key---you just need one `Mac` object per thread.
Chris Jester-Young
A: 

Can't you use MessageDigest (Example)? You'll have to cache the original timestamp/hash, or better yet, store the inbound and outbound transmissions in some sort of auditing table.

Droo