You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug] Bytes.getMD5(InputStream/File) returns incorrect MD5 for short final reads #16451
I am sure that all the content I provide is in English.
Search before asking
I had searched in the issues and found no similar issues.
Apache Dubbo Component
Java SDK (apache/dubbo)
Dubbo Version
Apache Dubbo 3.3 branch, JDK 8 or later, any operating system.
Steps to reproduce this issue
Bytes.getMD5(InputStream) uses InputStream.available() as an EOF check and updates the message digest with the entire buffer instead of the number of bytes actually read.
For example, the bundled md5.testfile.txt contains only:
hello world!
Calling Bytes.getMD5(File) returns the Base64 value:
iNZ+5qHafVNPLJxHwLKJ3w==
The correct MD5 of hello world! is:
/D/5joxqDTCH1RXARz+Gdw==
The same problem occurs for an InputStream whose final read is shorter than 8192 bytes.
What you expected to happen
Bytes.getMD5(InputStream) and Bytes.getMD5(File) should calculate the MD5 of exactly the bytes supplied by the input source.
Anything else
The implementation should read until read() returns -1 and update the digest with only the bytes returned by each read:
A pull request is prepared with tests for 12, 8191, 8192, and 8193 byte inputs, plus an input stream whose available() method returns zero while data is still readable.
Do you have a (mini) reproduction demo?
Yes, I have a minimal reproduction demo to help resolve this issue more effectively!
Are you willing to submit a pull request to fix on your own?
Yes I am willing to submit a pull request on my own!
Root cause: getMD5(InputStream, int) hashed the whole buffer via md.update(buf) instead of the total bytes actually read, so the final chunk of any stream whose length is not a multiple of the 8 KiB buffer hashed stale tail bytes from the previous chunk. The outer loop was also gated on available() > 0, which returns the digest of an empty input for streams that short-read (FileInputStream on network mounts and encrypted volumes, which is what getMD5(File) forwards to).
The PR hashes only the bytes read and drops the available() gate; the stale expectation in BytesTest.testMD5ForFile is corrected to the real digest. CI is green (32 checks).
Activity
HhH666-ux commentedon Oct 28, 2026
Picked this up — PR #16543 is open for review.
Root cause:
getMD5(InputStream, int)hashed the whole buffer viamd.update(buf)instead of thetotalbytes actually read, so the final chunk of any stream whose length is not a multiple of the 8 KiB buffer hashed stale tail bytes from the previous chunk. The outer loop was also gated onavailable() > 0, which returns the digest of an empty input for streams that short-read (FileInputStreamon network mounts and encrypted volumes, which is whatgetMD5(File)forwards to).The PR hashes only the bytes read and drops the
available()gate; the stale expectation inBytesTest.testMD5ForFileis corrected to the real digest. CI is green (32 checks).