Skip to content

Instantly share code, notes, and snippets.

@jweisman
Last active July 14, 2019 18:17
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 jweisman/2a0326e0e8d7978639229a6dbc9f55f2 to your computer and use it in GitHub Desktop.
Save jweisman/2a0326e0e8d7978639229a6dbc9f55f2 to your computer and use it in GitHub Desktop.
Validate Alma webhook signature in Java
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
public class ValidateSignature {
public static String encode(String key, byte[] data) throws Exception {
Mac hmac = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), key);
hmac.init(secret_key);
return Base64.getEncoder().encodeToString(hmac.doFinal(data));
}
public static void main(String [] args) throws Exception {
System.out.println(encode(args[0], Files.readAllBytes(Paths.get(args[1]))));
}
}
@jweisman
Copy link
Author

jweisman commented Jul 14, 2019

$ javac ValidateSignature.java
$ java ValidateSignature [KEY] ../json.txt
I/OLK158f+VJrF0cy8bNszVo6rcwl6/97p6Xqm9cqMM=

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment