{"record":{"id":"bb1fbb8c08cc080f","repo":"apache/hadoop","slug":"wrong-length-digest-length","errorCode":null,"errorMessage":"Wrong length: {digest.length}","messagePattern":"Wrong length: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/MD5Hash.java","lineNumber":71,"sourceCode":"  public MD5Hash() {\n    this.digest = new byte[MD5_LEN];\n  }\n\n  /**\n   * Constructs an MD5Hash from a hex string.\n   * @param hex input hex.\n   */\n  public MD5Hash(String hex) {\n    setDigest(hex);\n  }\n  \n  /**\n   * Constructs an MD5Hash with a specified value.\n   * @param digest digest.\n   */\n  public MD5Hash(byte[] digest) {\n    if (digest.length != MD5_LEN)\n      throw new IllegalArgumentException(\"Wrong length: \" + digest.length);\n    this.digest = digest;\n  }\n  \n  // javadoc from Writable\n  @Override\n  public void readFields(DataInput in) throws IOException {\n    in.readFully(digest);\n  }\n\n  /**\n   * Constructs, reads and returns an instance.\n   * @param in in.\n   * @throws IOException raised on errors performing I/O.\n   * @return MD5Hash.\n   */\n  public static MD5Hash read(DataInput in) throws IOException {\n    MD5Hash result = new MD5Hash();\n    result.readFields(in);","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/MD5Hash.java#L53-L89","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the digest is produced by MessageDigest.getInstance(\"MD5\") — its digest() output is always 16 bytes and always valid here.","If the bytes really are from another algorithm, don't use MD5Hash — represent them as a hex string or use the appropriate checksum type.","Add a length guard before construction and fail with the algorithm/source context instead of the bare constructor error."],"exampleFix":"// before: digest came from the wrong algorithm\nbyte[] d = MessageDigest.getInstance(\"SHA-256\").digest(data);\nMD5Hash md5 = new MD5Hash(d); // throws: length 32\n\n// after: compute an actual MD5 digest\nbyte[] d = MessageDigest.getInstance(\"MD5\").digest(data);\nMD5Hash md5 = new MD5Hash(d); // 16 bytes, OK","handlingStrategy":"validation","validationCode":"if (digest == null || digest.length != 16) {\n  throw new IllegalArgumentException(\n      \"Expected 16-byte MD5 digest, got \" + (digest == null ? \"null\" : digest.length));\n}\nreturn new MD5Hash(digest);","typeGuard":"static boolean isMd5Digest(byte[] d) {\n  return d != null && d.length == 16; // org.apache.hadoop.io.MD5Hash.MD5_LEN\n}","tryCatchPattern":null,"preventionTips":["Always compute digests with MessageDigest.getInstance(\"MD5\") — output length is guaranteed 16.","Centralize digest→MD5Hash construction behind one helper that guards the length.","When switching hash algorithms, audit every MD5Hash construction site — the class only models MD5."],"tags":["md5","checksum","validation","hadoop-common"],"backgroundTag":"digest-length-mismatch","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}