Skip to content

Instantly share code, notes, and snippets.

@artem-smotrakov
Created August 15, 2021 14:20
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 artem-smotrakov/933214caa4a0ed8d406b5b3389b7a71f to your computer and use it in GitHub Desktop.
Save artem-smotrakov/933214caa4a0ed8d406b5b3389b7a71f to your computer and use it in GitHub Desktop.
Possible timing attack against MAC validation
public boolean validate(HttpRequest request, SecretKey key) throws Exception {
byte[] message = getMessageFrom(request);
byte[] signature = getSignatureFrom(request);
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(key.getEncoded(), "HmacSHA256"));
byte[] actual = mac.doFinal(message);
return Arrays.equals(signature, actual);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment