Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

java.security.MessageDigest question - seems to generate same MD5 Value

Options
  • 17-05-2005 4:41pm
    #1
    Closed Accounts Posts: 1,719 ✭✭✭


    Hi, i've written a small method to compute the MD5 of a string which i'm passing to the method. the method then encodes the MD5 into Base64 with an another method ( not shown here ). i seem to be getting the same values each time for the MD5 tho. any suggestions?
    public String computeMD5(String xml)
    {
      String messagedigest = "";
      String digest64 = "";
    try
     {
      MessageDigest md = MessageDigest.getInstance("MD5");
      byte[] data = xml.getBytes();
      byte[] digest = md.digest(data);
      digest = md.digest();
      messagedigest = new String ( digest );
      digest64 = net.iharder.Base64.encodeBytes(digest);
     }
    catch(NoSuchAlgorithmException ex)
     { ex.toString(); }
       
     return digest64;
    


Comments

  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    I think it should be something like this:
       md = MessageDigest.getInstance("MD5");
       md.update(xml.getBytes());
       byte[] digestValue = md.digest();
       // then Base64 encode digestValue
    


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    It's quite possible that it's throwing an exception and returning the MD5 value of the hashed empty String.

    Try using System.err.print() for outputting any caught exceptions. ex.toString() doesn't do any useful debugging :)


  • Registered Users Posts: 1,996 ✭✭✭lynchie


    What enygma said, you need to call update on it first before calling digest.


  • Closed Accounts Posts: 92 ✭✭tempest


    Ruaidhri wrote:
    Hi, i've written a small method to compute the MD5 of a string which i'm passing to the method. the method then encodes the MD5 into Base64 with an another method ( not shown here ). i seem to be getting the same values each time for the MD5 tho. any suggestions?
    public String computeMD5(String xml)
    {
      String messagedigest = "";
      String digest64 = "";
    try
     {
      MessageDigest md = MessageDigest.getInstance("MD5");
      byte[] data = xml.getBytes();
      byte[] digest = md.digest(data);
    
      // This line is overwriting the previously digested data....
      // So just take it out and all will be ok..
      digest = md.digest();
    
      messagedigest = new String ( digest );
      digest64 = net.iharder.Base64.encodeBytes(digest);
     }
    catch(NoSuchAlgorithmException ex)
     { ex.toString(); }
       
     return digest64;
    

    See the comment in the code...
    The second call to digest undoes all of the good work performed by the digest(byte[] data) call...

    You can do it with update either but that's not the problem you are seeing as digest(byte[]) calls update(byte[]) followed by digest().


  • Closed Accounts Posts: 1,719 ✭✭✭Ruaidhri


    yep that did the trick thanks!


  • Advertisement
Advertisement