cancel
Showing results for 
Search instead for 
Did you mean: 

UDF to create SHA256 Hash of String by signature key

Former Member
0 Kudos

Hi Gurus,

has anybody an example for a UDF which creates a SHA256 Hash of a given String by secret key in Message mapping.

I just need to create the hash for one Field in Mapping not the whole message so not possible in Adapter.

BR

Markus

Accepted Solutions (1)

Accepted Solutions (1)

vadimklimov
Active Contributor
0 Kudos

Hi Markus,

Assuming you are speaking about usage of HMAC SHA256 (keyed-hash code using SHA256 algorithm), this can be achieved using following code in UDF:


final String ALGORITHM = "HmacSHA256";

final String CHARSET = "UTF-8";

byte[] valueHash = null;

try {

     Mac hmacSHA256 = Mac.getInstance(ALGORITHM);

     SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(CHARSET), ALGORITHM);

     hmacSHA256.init(secretKey);

     valueHash = hmacSHA256.doFinal(valuePlaintext.getBytes(CHARSET));

} catch (NoSuchAlgorithmException e) {

     return "Error";

} catch (UnsupportedEncodingException e) {

     return "Error";

} catch (InvalidKeyException e) {

     return "Error"

}

return new String(valueHash);

Few remarks:

  • Following UDF String parameters are assumed to be defined in the function: variable "valuePlaintext" - holds a value to be hashed and variable "key" - holds a value of a key that has to be used during hash generation;
  • Corresponding used classes shall be included in imports section of a UDF - namely: java.io.UnsupportedEncodingException, java.security.InvalidKeyException, java.security.NoSuchAlgorithmException, javax.crypto.Mac, javax.crypto.spec.SecretKeySpec;
  • Character encoding is set explicitly - you can either omit it and use default or adopt in case you use another encoding;
  • Exception handling logic is missing - please implement it according to your needs.

Regards,

Vadim

Answers (2)

Answers (2)

Former Member
0 Kudos

Thanks to you both!!! First code with applying conversion to hex works fine.

BR

Markus

anupam_ghosh2
Active Contributor
0 Kudos

Hi Markus,

                Generally the output after applying the algo should be in hexadecimal format. The reason behind the same is the fact that algo might generate characters which might invalidate the target XML.

Here is the code , you can try this code


public static String hash(String s) throws StreamTransformationException

  {

  try {

  java.security.MessageDigest md = java.security.MessageDigest.getInstance("SHA-256");

     

  md.update(s.getBytes("UTF-8"));

  byte[] byteData=md.digest();

  s=new String(byteData);

  //converting to hex

  StringBuffer hexString = new StringBuffer();

      for (int i=0;i<byteData.length;i++) {

      String hex=Integer.toHexString(0xff & byteData[i]);

        if(hex.length()==1) hexString.append('0');

        hexString.append(hex);

      

      }

      s=hexString.toString();

  } catch (Exception e) {

  // TODO Auto-generated catch block

  throw new StreamTransformationException(e.getMessage());

  }

     

  

  return s;

  }

output

String input=44-

Output=ac7852d303c6f30021b5fead9d95333c801428aa0dc282b2d63f66032da5dabe

String input=abcd

Output=88d4266fd4e6338d13b845fcf289579d209c897823b9217da3e161936f031589

String input=1234

Output=03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4

String input=xyz

Output=3608bca1e44ea6c4d268eb6db02260269892c0b42b86bbf1e77a6fa16c3c9282

Regards

Anupam

anupam_ghosh2
Active Contributor
0 Kudos

Hi Peter,

           Please replace  this line

java.security.MessageDigest md = java.security.MessageDigest.getInstance("SHA-256");


with this line


java.security.MessageDigest md = java.security.MessageDigest.getInstance("SHA-512");



Regards

Anupam

Former Member
0 Kudos

Thanks Anupam. That works really well 🙂