Skip to content

Instantly share code, notes, and snippets.

@vishnugopal
Created February 15, 2012 11:28
Show Gist options
  • Save vishnugopal/1835178 to your computer and use it in GitHub Desktop.
Save vishnugopal/1835178 to your computer and use it in GitHub Desktop.
Refactor Java!
private String[] getAuthenticationParameters() throws NoSuchAlgorithmException {
String[] parameters = { "", "" };
MessageDigest md = MessageDigest.getInstance("MD5");
String timestamp = String.valueOf(java.lang.System.currentTimeMillis());
String keyWithTimestamp = timestamp.concat(Constants.RPC.Services.API_KEY);
// Let's generate a hex representation of the hash
// http://stackoverflow.com/a/421696
md.reset();
md.update(keyWithTimestamp.getBytes());
byte[] digest = md.digest();
BigInteger bigInt = new BigInteger(1, digest);
String hashtext = bigInt.toString(16);
while(hashtext.length() < 32 ) {
hashtext = "0" + hashtext;
}
parameters[0] = timestamp;
parameters[1] = hashtext;
return parameters;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment