public static byte[] computeHkdf(
      String macAlgorithm, final byte[] ikm, final byte[] salt, final byte[] info, int size)
      throws GeneralSecurityException {
    Mac mac = EngineFactory.MAC.getInstance(macAlgorithm);
    if (size > 255 * mac.getMacLength()) {
      throw new GeneralSecurityException("size too large");
    }
    if (salt == null || salt.length == 0) {
      // According to RFC 5869, Section 2.2 the salt is optional. If no salt is provided
      // then HKDF uses a salt that is an array of zeros of the same length as the hash digest.
      mac.init(new SecretKeySpec(new byte[mac.getMacLength()], macAlgorithm));
    } else {
      mac.init(new SecretKeySpec(salt, macAlgorithm));
    }
    byte[] prk = mac.doFinal(ikm);
    byte[] result = new byte[size];
    int ctr = 1;
    int pos = 0;
    mac.init(new SecretKeySpec(prk, macAlgorithm));
    byte[] digest = new byte[0];
    while (true) {
      mac.update(digest);
      mac.update(info);
      mac.update((byte) ctr);
      digest = mac.doFinal();
      if (pos + digest.length < size) {
        System.arraycopy(digest, 0, result, pos, digest.length);
        pos += digest.length;
        ctr++;
      } else {
        System.arraycopy(digest, 0, result, pos, size - pos);
        break;
      }
    }
    return result;
  }
Computes an HKDF.

@param macAlgorithm the MAC algorithm used for computing the Hkdf. I.e., "HMACSHA1" or
    "HMACSHA256".
@param ikm the input keying material.
@param salt optional salt. A possibly non-secret random value. If no salt is provided (i.e. if
    salt has length 0) then an array of 0s of the same size as the hash digest is used as salt.
@param info optional context and application specific information.
@param size The length of the generated pseudorandom string in bytes. The maximal size is
    255.DigestSize, where DigestSize is the size of the underlying HMAC.
@return size pseudorandom bytes.
@throws GeneralSecurityException if the {@code macAlgorithm} is not supported or if {@code
    size} is too large or if {@code salt} is not a valid key for macAlgorithm (which should not
    happen since HMAC allows key sizes up to 2^64).
java_src/src/main/java/com/google/crypto/tink/subtle/Hkdf.java