on 02-11-2016 8:42 AM
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
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:
Regards,
Vadim
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks to you both!!! First code with applying conversion to hex works fine.
BR
Markus
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
95 | |
11 | |
11 | |
10 | |
9 | |
8 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.