Create MD5 hash in transformation script

Hi,

I’m testing a mecross switch with MQTT & openhab, and it’s almost working. I have one last step, which is to create an MD5 hash of some values to sign the JSON payload in a javascript transform script.

My script so far:

(function(state) {
	var logger = Java.type("org.slf4j.LoggerFactory").getLogger("myScript");
	logger.warn("mecross.js - start");

	var message = "messsage" + Date.now();
	var timestamp = Date.now() / 100;

	var result = {};
	result.header = {};
	result.header.messageId = message;
	result.header.namespace = "Appliance.Control.ToggleX";
	result.header.method = "SET";
	result.header.payloadVersion = 1;
	result.header.from = "/appliance/openhab/ack";
	result.header.timestamp = timestamp;
	result.header.sign = MD5(message+timestamp).toString();

	result.payload = {}
	result.payload.togglex = {};
	result.payload.togglex.channel = 0;
	result.payload.togglex.onoff = state == "1"?1:0;

	logger.warn(JSON.stringify(result));
	return JSON.stringify(result);

})(input)

My issue is with the following line:

result.header.sign = MD5(message+timestamp).toString();

I need a function to create the MD5 hash — Does one exist? Or do I need to create my own?

Thank you.

Hi,

I found a pure JavaScript implementation of MD5 online and used that to create a script.

For reference, the script is here: https://www.theriom.com/assets/meross.js

Also, I created a small article on configuring Meross switches with openhab: https://www.theriom.com/homeautomation/2020/08/04/meross-switch-with-openhab.html

C.

2 Likes