private void validateMac ( byte [ ] key , HashAlgorithm hmacAlgorithm ) throws KSIException { try { HashAlgorithm receivedHmacAlgorithm = mac . getAlgorithm ( ) ; if ( receivedHmacAlgorithm != hmacAlgorithm ) { throw new KSIException ( "HMAC algorithm mismatch. Expected " + hmacAlgorithm . getName ( ) + ", received " + receivedHmacAlgorithm . getName ( ) ) ; } // calculate and set the MAC value DataHash macValue = new DataHash ( hmacAlgorithm , Util . calculateHMAC ( getContent ( ) , key , hmacAlgorithm . getName ( ) ) ) ; if ( ! mac . equals ( macValue ) ) { throw new InvalidMessageAuthenticationCodeException ( "Invalid MAC code. Expected " + mac + ", calculated " + macValue ) ; } } catch ( IOException e ) { throw new InvalidMessageAuthenticationCodeException ( "IO Exception occurred turning MAC calculation" , e ) ; } catch ( InvalidKeyException e ) { throw new InvalidMessageAuthenticationCodeException ( "Problem with HMAC key." , e ) ; } catch ( NoSuchAlgorithmException e ) { throw new InvalidMessageAuthenticationCodeException ( "Unsupported HMAC algorithm." , e ) ; } catch ( HashException e ) { throw new KSIProtocolException ( "Hashing exception occurred when calculating KSI service response HMAC" , e ) ; } }
Checks the MAC code .