Created
August 15, 2021 14:22
-
-
Save artem-smotrakov/aaff397fa5194f975fa5b038a94361c1 to your computer and use it in GitHub Desktop.
Using constant-time algorithm for validating a MAC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 MessageDigest.isEqual(signature, actual); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment