apache/hadoop · error · IllegalArgumentException

Wrong length: {digest.length}

Error message

Wrong length: {digest.length}

What it means

Thrown by the MD5Hash(byte[]) constructor when the supplied digest array is not exactly MD5_LEN (16) bytes. MD5 always produces a 128-bit digest; any other length means the bytes came from a different algorithm or were truncated/over-read, so the constructor refuses them with IllegalArgumentException.

Source

Thrown at hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/MD5Hash.java:71

  public MD5Hash() {
    this.digest = new byte[MD5_LEN];
  }

  /**
   * Constructs an MD5Hash from a hex string.
   * @param hex input hex.
   */
  public MD5Hash(String hex) {
    setDigest(hex);
  }
  
  /**
   * Constructs an MD5Hash with a specified value.
   * @param digest digest.
   */
  public MD5Hash(byte[] digest) {
    if (digest.length != MD5_LEN)
      throw new IllegalArgumentException("Wrong length: " + digest.length);
    this.digest = digest;
  }
  
  // javadoc from Writable
  @Override
  public void readFields(DataInput in) throws IOException {
    in.readFully(digest);
  }

  /**
   * Constructs, reads and returns an instance.
   * @param in in.
   * @throws IOException raised on errors performing I/O.
   * @return MD5Hash.
   */
  public static MD5Hash read(DataInput in) throws IOException {
    MD5Hash result = new MD5Hash();
    result.readFields(in);

View on GitHub (pinned to 2add963021)

Solutions

  1. Ensure the digest is produced by MessageDigest.getInstance("MD5") — its digest() output is always 16 bytes and always valid here.
  2. If the bytes really are from another algorithm, don't use MD5Hash — represent them as a hex string or use the appropriate checksum type.
  3. Add a length guard before construction and fail with the algorithm/source context instead of the bare constructor error.

Example fix

// before: digest came from the wrong algorithm
byte[] d = MessageDigest.getInstance("SHA-256").digest(data);
MD5Hash md5 = new MD5Hash(d); // throws: length 32

// after: compute an actual MD5 digest
byte[] d = MessageDigest.getInstance("MD5").digest(data);
MD5Hash md5 = new MD5Hash(d); // 16 bytes, OK
Defensive patterns

Strategy: validation

Validate before calling

if (digest == null || digest.length != 16) {
  throw new IllegalArgumentException(
      "Expected 16-byte MD5 digest, got " + (digest == null ? "null" : digest.length));
}
return new MD5Hash(digest);

Type guard

static boolean isMd5Digest(byte[] d) {
  return d != null && d.length == 16; // org.apache.hadoop.io.MD5Hash.MD5_LEN
}

Prevention

When it happens

Trigger: new MD5Hash(digest) where digest came from MessageDigest.digest() of an algorithm other than MD5 (SHA-1=20 bytes, SHA-256=32), a hand-built byte array of the wrong size, or a copyOfRange that sliced the wrong bounds.

Common situations: Switching a checksum utility from SHA-256 to MD5 (or vice versa) but keeping the MD5Hash wrapper; test fixtures with placeholder arrays like new byte[20]; interop code reading digests from formats that store algorithm-prefixed or padded digests.

Related errors


AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22). Data as JSON: /api/errors/bb1fbb8c08cc080f. Report an issue: GitHub.