CommunityCommons Hash

1
Hello, I want to use the CommunityCommon's Hash Java action to hash a string, but I can't seem to get it to work properly. The Java Action always returns '32', no matter what the input string (or length) is. I've uploaded a very basic test project here: http://expirebox.com/download/5c578caa508e3593fc35058cbaee6e19.html Can anyone help or explain how it should be used or what I'm doing wrong? thanks!
asked
4 answers
1

Looks like the call to MessageDigest.digest() in CommunityCommons is used incorrectly. There are 3 overloads to this method, and the one used just returns the number of bytes used in the hash output buffer. The other two overloads return the hashed content.

I've uploaded a fixed StringUtils.java file here. You can overwrite the existing file in your project directory at:

\javasource\communitycommons\

I also commented on this issue and provided my code on github

answered
1

Since I couldn't get the CommunityCommons to work as expected, I've created my own SHA-256 function, based on this post. This function does give me the expected result...

// BEGIN USER CODE
    MessageDigest md = MessageDigest.getInstance("SHA-256");
    md.update(Value.getBytes());
    byte[] digest = md.digest();
    StringBuffer sb = new StringBuffer();
    for (byte b : digest) {
        sb.append(String.format("%02x", b & 0xff));
    }
    return sb.toString();
    // END USER CODE
answered
0

Try implementing your own. Here's one that you can use a reference. It takes a strin as input and returns an integer/long

import com.mendix.systemwideinterfaces.core.UserAction; import com.mendix.systemwideinterfaces.core.IContext; import com.mendix.webui.CustomJavaAction;

/**


*/ public class GetHash extends CustomJavaAction<long> { private String input;

public GetHash(IContext context, String input)
{
    super(context);
    this.input = input;
}

@Override
public Long executeAction() throws Exception
{
    // BEGIN USER CODE
    return Long.valueOf(input.hashCode());
    // END USER CODE
}

/**
 * Returns a string representation of this action
 */
@Override
public String toString()
{
    return "GetHash";
}

// BEGIN EXTRA CODE
// END EXTRA CODE

}

answered
0

Hi Eric,

great, thanks. but the uploaded StringUtils.java file seems to be identical to the one I already have... Perhaps you uploaded the wrong file?

answered