Calculate the MD5 hash of a string with Groovy

Document created by steve_skripek082158 Employee on Sep 19, 2017Last modified by Adam Arrowsmith on Oct 9, 2017
Version 7Show Document
  • View in full screen mode

 

Use Case

You can easily calculate the MD5 hash of a string value in AtomSphere using a small Groovy script. Some APIs such as MailChimp require the hashing of one or more values to include in an HTTP Client operation web service request.

 

This script outputs the hashed value as a string.

 

Implementation

 

Option 1 (Data Process context)

 

 

If the hashed value needs to be sent "outside" the message payload such as in an HTTP header, resource path (URL):

  1. Add a Set Properties shape to set a dynamic document property with the value(s) that need to be hashed. This makes the value easier to access in the script vs. parsing the document's contents.
  2. Add a Data Process shape with a custom scripting step with the script below. The script performs the hash and updates the value of the same document property.
  3. Add another Set Properties shape to override the Replacement Variable configured in the HTTP Client Operation (see next step). Concatenate additional portions of the resource path as required by the destination API endpoint.
    • IMPORTANT: Ensure the property name matches exactly the Replacement Variable configured in the HTTP Client Operation.
  4. Add a Connector shape with the desired HTTP Client Connection and Operation. The operation is configured with the appropriate replacement variable defined, such as in the Resource Path in this example.

 

import java.util.Properties;
import java.io.InputStream;
import java.security.MessageDigest;
import java.math.BigInteger;


for( int i = 0; i < dataContext.getDataCount(); i++ ) {
  InputStream is = dataContext.getStream(i);
  Properties props = dataContext.getProperties(i);

  // Get dyn doc prop to hash
  String input_string = props.getProperty("document.dynamic.userdefined.DDP_HASH_VALUE");

  MessageDigest digest = MessageDigest.getInstance("MD5") ;
  digest.update(input_string.bytes);
  output = new BigInteger(1, digest.digest()).toString(16).padLeft(32, '0') ;

  // Update dyn doc prop
  props.setProperty("document.dynamic.userdefined.DDP_HASH_VALUE", output);

  dataContext.storeStream(is, props);
}

 

Option 2 (Map Function context)

If you are already mapping data or the hashed value should be included in the actual message payload:

  1. Add a Map shape.
  2. Add a Custom Scripting map function step with the script below.
  3. Map the source value(s) into the function inputs; map the function outputs to the destination field.

 

import java.security.MessageDigest;

MessageDigest digest = MessageDigest.getInstance("MD5") ;
digest.update(input_string.bytes);
output = new BigInteger(1, digest.digest()).toString(16).padLeft(32, '0') ;

 

 

Considerations

  • In Groovy when converting from an integer to a string any leading zeros will get dropped. To preserve any leading zeros we need the while loop at the end to pad them back.

Attachments

    Outcomes