code: public static byte[] calculateHMAC(byte[] message, byte[] keyBytes, String algorithm) throws NoSuchAlgorithmException, InvalidKeyException {
          if (keyBytes == null) {
              throw new IllegalArgumentException("Invalid HMAC key: null");
          }
          String hmacAlgorithmName = "Hmac" + algorithm.toUpperCase().replaceAll("[^\\p{Alnum}]", "");
  
          SecretKeySpec key = new SecretKeySpec(keyBytes, hmacAlgorithmName);
          Mac mac = Mac.getInstance(hmacAlgorithmName);
          mac.init(key);
          return mac.doFinal(message);
      }
docs: Calculates the RFC 2104 compatible HMAC for the given message, key, and algorithm.
  
  @param message
  message for which the MAC is to be calculated.
  @param keyBytes
  key for calculation.
  @param algorithm
  algorithm to be used (MD5, SHA1, SHA256).
  
  @return HMAC as byte array.
  
  @throws NoSuchAlgorithmException
  if invalid algorithm is provided.
  @throws InvalidKeyException
  if invalid key is provided.
  @throws IllegalArgumentException
  if HMAC key is null.