String macAlgorithm = "HmacMD5";
byte[] digestedPasswordBytes = MessageDigest.getInstance("MD5").digest(userPassword.getBytes("UTF-8"));
byte[] hexEncodedDigestedPasswordBytes = toHex(digestedPasswordBytes).getBytes("UTF-8");
Mac mac = Mac.getInstance(macAlgorithm);
mac.init(new SecretKeySpec(hexEncodedDigestedPasswordBytes, macAlgorithm));
final byte[] messageAuthenticationCode = mac.doFinal(challengeBytes);
String responseAsString = userName + " " + toHex(messageAuthenticationCode);
return responseAsString.getBytes();
}
private byte[] generateCramMD5ClientResponse(String userName, String userPassword, byte[] challengeBytes) throws Exception