class LengthAdjustment {

  static byte[] adjust(byte[] candidate, int byteLength) {
    Objects.requireNonNull(candidate);
    if (byteLength < 0) {
      throw new IllegalArgumentException("Can not adjust length to negative length: " + byteLength);
    }
    byte[] key;
    if (candidate.length >= byteLength) {
      key = Arrays.copyOf(candidate, byteLength);
    } else {
      key = new byte[byteLength];
      int offset = 0;
      int counter = 0;
      while (offset < byteLength) {
        MessageDigest digest = getDigest(DIGEST_ALGO);
        digest.update(intToBytes(counter++));
        digest.update(candidate);
        int len = Math.min(digest.getDigestLength(), byteLength - offset);
        System.arraycopy(digest.digest(), 0, key, offset, len);
        offset += len;
      }
    }
    return key;
  }

  private  LengthAdjustment();

}