1

I am trying to create a unique name for each file you upload to my server from my Jar. I am using the MessageDigest MD5 class to create, but unfortunately I always get the same name.

BufferedImage buffImg = ImageIO.read(imageEntry);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(buffImg, Utils.getExtensionNoDot(imageEntry.getName()), outputStream);
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(outputStream.toByteArray());
BigInteger bigInt = new BigInteger(1,md.digest());
String md5 = bigInt.toString(16);

My md5 is the same as always, through print and verify the results, I know my MessageDigest is always different, but the result is always the same BigInt.

hateful
  • 141
  • 2
  • 13
  • You're using the BigInteger's constructor wrong. See: http://stackoverflow.com/a/13006907 or http://www.mkyong.com/java/java-md5-hashing-example/ – Koshinae Apr 11 '16 at 14:42
  • Implement one of the solutions he gave me, but the results are always the same, I do not think is that the problem must be elsewhere. @Koshinae – hateful Apr 11 '16 at 14:51
  • The number `1` is the same in every radix, you're using the constructor incorrectly. – Elliott Frisch Apr 11 '16 at 14:55
  • 3
    It's time for a [mcve]. – Tom Apr 11 '16 at 14:55
  • @Tom Ok, I'll try to dig deeper into the problem – hateful Apr 11 '16 at 14:57
  • I think something is wrong with data array, because `BigInteger` [works well](https://gist.github.com/yaruson/2b65f156e1a6c3fbc49ebafb1e5c2202) for me. Please print or debug contents of `outputStream.toByteArray()`. By the way, what hash do you get all the time? – Nikolai Kim Apr 11 '16 at 15:05
  • @ElliottFrisch Change the encryption method and is still the same, my problem is another. – hateful Apr 11 '16 at 15:06
  • I could get it to work. The error was in the method of creating the BigInteger, as mentioned above 1 to always be the same hash. – hateful Apr 11 '16 at 15:18
  • @hateful Glad to hear your doubts were solved. But I can't get what's wrong with `BigInteger` as in [this example](http://ideone.com/WZJofh)? – Nikolai Kim Apr 11 '16 at 15:22
  • I was also up images. 1.jpg image had another copy image 1.jpg and so many other images, the scope of copy name also had some problem and once you check it, but the parameter in the method BigInteger, the problem was solved. – hateful Apr 11 '16 at 15:22
  • @Yaruson I'm still looking for that failure, my best example is the name of the file in this case, even I keep my solution because I am verifying that this is indeed so and not an error code. – hateful Apr 11 '16 at 15:24

0 Answers0