java - Apache DigestUtils seem to calculate MD5 partially wrong -
tonight working on easy solution on-the-fly-md5 calculation while stream passed. main framework working straight away. data got streamed , md5-hash computed. started monitoring md5-hash @ 3 different stages. before streaming, while streaming , after streaming. , suprised result.
the important md5-digest wrongly computed. encoding used same method of apache's digestutils. instead of generating 3 times same output, got 2 of 3.
to verify, there no general error in construct additionally used apache's (...).binary.hex
class encode digest. , time result matched other ones.
md5 before streaming: 9065793b048f4efee5ccddb34798ee19
md5 after streaming: 9065793b048f4efee5ccddb34798ee19
md5 while streaming encoded digestutils: 95d845ff55b5918edc8d1222045dd1cb
md5 while streaming encoded binary.hex: 9065793b048f4efee5ccddb34798ee19
and here simple code.
the first class started via main method. calls instance of class streamforwarder , passes test string inputstream. that's it.
import java.io.bytearrayinputstream; import java.io.inputstream; import org.apache.commons.codec.digest.digestutils; public class streamprovider { private static final byte[] streamcontent; static { streamcontent = "i9bjyxhria7fvuq8wtihibgcgwfigkfu".getbytes(); } private static inputstream getstreamcontent() { return new bytearrayinputstream(streamcontent); } public static void main(string args[]) { system.out.println("md5 before streaming: " + digestutils.md5hex(streamcontent)); streamforwarder streamforwarder = new streamforwarder(); streamforwarder.forwardstream(getstreamcontent()); } }
the streamforwarder wrapps inputstream in digestinputstream , gets instance of messagedigest compute md5-hash while streaming. calls instance of class streamconsumer , passes digestinputstream.
import java.io.inputstream; import java.security.digestinputstream; import java.security.messagedigest; import java.security.nosuchalgorithmexception; import org.apache.commons.codec.binary.hex; import org.apache.commons.codec.digest.digestutils; public class streamforwarder { public void forwardstream(inputstream is) throws nosuchalgorithmexception { messagedigest md = messagedigest.getinstance("md5"); digestinputstream dis = new digestinputstream(is, md); streamconsumer streamconsumer = new streamconsumer(); streamconsumer.printstreamcontent(dis); byte digest[] = md.digest(); string digestutilshexstring = digestutils.md5hex(digest); string binaryhexstring = new string(hex.encodehex(digest)); system.out.println("md5 while streaming encoded digestutils: " + digestutilshexstring); system.out.println("md5 while streaming encoded binary.hex: " + binaryhexstring); } }
finally streamconsumer reads stream using apache's ioutils.
import java.io.ioexception; import java.io.inputstream; import org.apache.commons.codec.digest.digestutils; import org.apache.commons.io.ioutils; public class streamconsumer { public void printstreamcontent(inputstream is) throws ioexception { string mystring = ioutils.tostring(is, "utf-8"); system.out.println("md5 after streaming: " + digestutils.md5hex(mystring.getbytes())); } }
as can see there no rocket science behind it. still output of streamforwarder.class not expecting concerning encoded hexstring of digestutils.
has explanation me why happens? have done wrong?
thanks in advance!
the problem in line
string digestutilshexstring = digestutils.md5hex(digest);
you occasionaly calculating digest of digest - md5hex(md5(is)). may check in bash
$ echo -n 9065793b048f4efee5ccddb34798ee19 \ | perl -pe 's/([0-9a-f]{2})/chr hex $1/gie' \ | md5sum 95d845ff55b5918edc8d1222045dd1cb -
Comments
Post a Comment