Skip to content

Instantly share code, notes, and snippets.

@ramanathanrv
Created May 26, 2016 06:57
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 ramanathanrv/a400fb706897b2bc7438c99233076cb5 to your computer and use it in GitHub Desktop.
Save ramanathanrv/a400fb706897b2bc7438c99233076cb5 to your computer and use it in GitHub Desktop.
Calculate HMAC SHA-256 for a given message
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
public String calculateSignature() {
String secretKey = "<insert secret key here>";
String serialized = "order_id=1464092311945&status=CHARGED&status_id=21";
String algorithm = "HmacSHA256";
serialized = URLEncoder.encode(serialized);
SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(), algorithm);
Mac mac = Mac.getInstance(algorithm);
mac.init(secretKeySpec);
byte[] digest = mac.doFinal(serialized.getBytes());
return URLEncoder.encode(digest.encodeBase64().toString());
}
calculateSignature();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment