Hi Paul, I have identified an error in the implementation of the HASH in the Community Commons. I will look to change this in the GitHub project so it will hopefully be fixed in the future.
The problem I see is as follows:
In the HASH java action for Community commons, we require as input a string and a buffer length argument.
We use the MessageDigest java method with the arguments ‘value’ being the input string and ‘length’ being the input buffer length.
(from StringUtils.java)
public static String hash(String value, int length) throws NoSuchAlgorithmException, DigestException { return String.valueOf(MessageDigest.getInstance(HASH_ALGORITHM).digest(value.getBytes(), 0, length)); }
When we make the call to the digest method, the code looks as follows:
(from MessageDigest.java)
public int digest(byte[] buf, int offset, int len) throws DigestException {
if (buf == null) {
throw new IllegalArgumentException("No output buffer given");
}
if (buf.length - offset < len) {
throw new IllegalArgumentException
("Output buffer too small for specified offset and length");
}
int numBytes = engineDigest(buf, offset, len);
state = INITIAL;
return numBytes; }
So it seems that the code throws an error if if the length of the array buf is less than the length parameter. This seems backwards to me
If you change line 53 of javasource/communitycommons/StringUtils.java from
return String.valueOf(MessageDigest.getInstance(HASH_ALGORITHM).digest(value.getBytes(), 0, length));
to
return String.valueOf(MessageDigest.getInstance(HASH_ALGORITHM).digest(value.getBytes()));
Does that solve the issue?