Anyone a sha384hmac generator ?

0
Hi all   Anyone a sha384hmac generator to share ?
asked
1 answers
1

Enzo,

Below is a version in java that can be easily implemented in a Mendix java action:

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

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


public class testHMAC {

	public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException {
		final String secretkey = "nonce";
	    final String message = "test string";
	    SecretKeySpec signingKey = new SecretKeySpec(secretkey.getBytes(), HMAC_SHA1_ALGORITHM);
	    final Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
	    mac.init(signingKey);
	    System.out.println(bytesToHex(mac.doFinal(message.getBytes())));

	}
	private static final String HMAC_SHA1_ALGORITHM = "HmacSHA384";
	
	private static String bytesToHex(final byte[] hash) {
	    final StringBuffer hexString = new StringBuffer();
	    for (int i = 0; i < hash.length; i++) {
	        final String hex = Integer.toHexString(0xff & hash[i]);
	        if (hex.length() == 1) {
	            hexString.append('0');
	        }
	        hexString.append(hex);
	    }
	    return hexString.toString();
	}
}

Update:

The java action in Mendix should have 2 parameters both String type:

- secret

- message

The code for the custom java action is stated below:

// This file was generated by Mendix Modeler.
//
// WARNING: Only the following code will be retained when actions are regenerated:
// - the import list
// - the code between BEGIN USER CODE and END USER CODE
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
// Special characters, e.g., é, ö, à, etc. are supported in comments.

package myfirstmodule.actions;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import com.mendix.core.Core;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.webui.CustomJavaAction;

public class JA_sha384hmac extends CustomJavaAction<java.lang.String>
{
	private java.lang.String message;
	private java.lang.String secret;

	public JA_sha384hmac(IContext context, java.lang.String message, java.lang.String secret)
	{
		super(context);
		this.message = message;
		this.secret = secret;
	}

	@Override
	public java.lang.String executeAction() throws Exception
	{
		// BEGIN USER CODE
		try{
	    SecretKeySpec signingKey = new SecretKeySpec(secret.getBytes(), HMAC_SHA1_ALGORITHM);
	    final Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
	    mac.init(signingKey);
	    return bytesToHex(mac.doFinal(message.getBytes()));
		}
		catch(Exception e){
			Core.getLogger("sha error").error(e.getMessage());
			return "";
		}
		// END USER CODE
	}

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

	// BEGIN EXTRA CODE
	
private static final String HMAC_SHA1_ALGORITHM = "HmacSHA384";
	
	private static String bytesToHex(final byte[] hash) {
	    final StringBuffer hexString = new StringBuffer();
	    for (int i = 0; i < hash.length; i++) {
	        final String hex = Integer.toHexString(0xff & hash[i]);
	        if (hex.length() == 1) {
	            hexString.append('0');
	        }
	        hexString.append(hex);
	    }
	    return hexString.toString();
	}
	// END EXTRA CODE
}

THis will return encoded string.

answered