groovydataweavemule4mule-el

Trouble converting SHA-256 hashing from Mule 3 to Mule 4


Does anyone know the Mule 4 (4.4.0) equivalent to the following Mule 3 (3.9.2) code? I'm stuck...

<set-payload value="#[com.google.common.hash.Hashing.sha256().hashBytes(flowVars['varHash'].getBytes('UTF-8'))]" doc:name="Set Payload"/>
    <byte-array-to-string-transformer doc:name="Byte Array to String"/>

Solution

  • Depending on the encoding of varHash -assuming it is a Java String- the easiest way to do it is to use DataWeave built-in Crypto::hashWith() function in a transformer:

    %dw 2.0
    import dw::Crypto
    output application/java
    ---
    Crypto::hashWith(vars.varHash as Binary, "SHA-256") 
    

    If that doesn't match the expected result because of encoding or padding, then you should be able to reproduce the Mule 3 expression in a Groovy script using the Scripting Module in a Groovy script. The Mule 3 MEL expression is also a Java sentence, and should also work in Groovy.

    Be sure to follow the configuration instructions for the module and the Groovy library. You may need to add the Maven dependency to your pom for the com.google.common.hash.Hashing class, which I suspect is the Google Guava library.

    An alternative to the Groovy script is to create a small Java class with a method to implement this expression. Then you can invoke it with the Java module or if it is a public static method you can call it from DataWeave.