Skip to content

Instantly share code, notes, and snippets.

@artemnikitin
Last active September 26, 2015 23:25
Show Gist options
  • Save artemnikitin/d45a5746d68965545d03 to your computer and use it in GitHub Desktop.
Save artemnikitin/d45a5746d68965545d03 to your computer and use it in GitHub Desktop.
Generate Hmac SHA-1 signature via Java
import java.util.Base64;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
public class Hmac {
private static String data = "2.00|5101603|RUB|bill|test-checking-one-way-response-from-processing|0|белый тест|paid|tel:+79167421378";
private static String key = "123456789";
public static void main(String[] args){
System.out.println(getSignedBody(data, key));
}
private static String getSignedBody(String data, String key){
String result = "";
try {
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(signingKey);
byte[] rawHmac = mac.doFinal(data.getBytes("UTF-8"));
result = Base64.getEncoder().encodeToString(rawHmac);
} catch (Exception e) {
System.out.println("Failed to generate HMAC: " + e.getMessage());
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment