Skip to content

Instantly share code, notes, and snippets.

@cronoh
Created September 29, 2017 18:05
Show Gist options
  • Save cronoh/718fb87a2e1e14fc511adbd47adff1ed to your computer and use it in GitHub Desktop.
Save cronoh/718fb87a2e1e14fc511adbd47adff1ed to your computer and use it in GitHub Desktop.
Mirth Connect SOAP PasswordDigest Generation
/**
Generate a password digest for SOAP authentication.
Returns an object with a passwordDigest (Base64), nonce (Base64), and created property.
@param string password -
@return { passwordDigest: string, nonce: string, created: string }
*/
function generatePasswordDigest(password) {
// Generate nonce from random bytes
var random = java.security.SecureRandom.getInstance("SHA1PRNG");
random.setSeed(java.lang.System.currentTimeMillis());
var nonceBytes = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 16);
random.nextBytes(nonceBytes);
// Generate created at date
var sdf = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
sdf.setTimeZone(java.util.TimeZone.getTimeZone("UTC")); // Convert to UTC
var createdDate = sdf.format(new Date());
var createdBytes = createdDate.getBytes('UTF-8');
// Get bytes of password
var passwordBytes = java.lang.String(password).getBytes('UTF-8');
// Combine everything, hash with sha1
var baos = new java.io.ByteArrayOutputStream();
baos.write(nonceBytes);
baos.write(createdBytes);
baos.write(passwordBytes);
var md = java.security.MessageDigest.getInstance('SHA-1');
var digestedPassword = md.digest(baos.toByteArray());
// Base64 encode the nonce and password digest
var passwordHash = org.apache.commons.codec.binary.Base64.encodeBase64String(digestedPassword);
var nonceBytes64 = org.apache.commons.codec.binary.Base64.encodeBase64String(nonceBytes);
return {
passwordDigest: passwordHash,
nonce: nonceBytes64,
created: createdDate.toString(),
};
}
var digest = generatePasswordDigest('yourPasswordHere');
// Insert generated values into template (Or use as necessary)
tmp.*::['Header'].*::['Security'].*::['UsernameToken'].*::['Password'].setChildren(digest.passwordDigest);
tmp.*::['Header'].*::['Security'].*::['UsernameToken'].*::['Nonce'].setChildren(digest.nonce);
tmp.*::['Header'].*::['Security'].*::['UsernameToken'].*::['Created'].setChildren(digest.created);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment