Community Commons Hash: Error Output buffer too small

First, is there documented examples of using the Community Commons String Utilities? Short of that, I'm trying to use the Hash function, but I keep getting the error: com.mendix.core.CoreException: com.mendix.modules.microflowengine.MicroflowException: com.mendix.core.CoreRuntimeException: java.lang.IllegalArgumentException: Output buffer too small for specified offset and length at NxAPI.GetSessionInfoHC (JavaAction : 'Call 'Hash'') I'm guessing it doesn't like my value for "length". What kind of values goes here? thanks!
2 answers

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.


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:


           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/ from

return String.valueOf(MessageDigest.getInstance(HASH_ALGORITHM).digest(value.getBytes(), 0, length));


return String.valueOf(MessageDigest.getInstance(HASH_ALGORITHM).digest(value.getBytes()));

Does that solve the issue?