Skip to content

Instantly share code, notes, and snippets.

@dhonig
Created June 5, 2012 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhonig/2876650 to your computer and use it in GitHub Desktop.
Save dhonig/2876650 to your computer and use it in GitHub Desktop.
SugarCRM MD5 example
*/
public String login() {
UserAuth userAuth = new UserAuth();
userAuth.setUserName(username);
userAuth.setPassword(password);
try {
EntryValue loginRes = wsClient.login(userAuth, Class.class.getName(), new NameValueList());
return loginRes.getId();
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
// if (!loginRes.getError().getNumber().equals("0")) {
// throw new RuntimeException("Error login to SugarCRM:" + " " + loginRes.getError().getNumber() + " " + loginRes.getError().getName() + " " + loginRes.getError().getDescription());
// }
}
/**
* @param data
* @return
* @throws Exception
*/
private String getHexString(byte[] data) throws Exception {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < data.length; i++) {
int halfbyte = (data[i] >>> 4) & 0x0F;
int two_halfs = 0;
do {
if ((0 <= halfbyte) && (halfbyte <= 9))
buf.append((char) ('0' + halfbyte));
else
buf.append((char) ('a' + (halfbyte - 10)));
halfbyte = data[i] & 0x0F;
} while (two_halfs++ < 1);
}
return buf.toString();
}
/**
* @param password
* @throws Exception
*/
public void setPassword(String password) throws Exception {
MessageDigest md = MessageDigest.getInstance("MD5");
this.password = getHexString(md.digest(password.getBytes()));
}
/**
* @param username
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @param wsClient
*/
public void setWsClient(SugarsoapPortType wsClient) {
this.wsClient = wsClient;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment