code: 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);
          }
      }
docs: Checks the MAC code.
  
  @param key
  key to be used to calculate MAC code.
  @param hmacAlgorithm
  algorithm for verifying the HMAC of incoming messages.
  @throws KSIException
  when MAC code doesn't validate