Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: MD5 hash and MimeMessage problem

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Location
    Volos Greece
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default MD5 hash and MimeMessage problem

    Hello to all!I m new to forum so excuse me for any mistakes!!Iwan't to get MD5 hash from MimeMessage type!I use the following code to take MD5 :
    //MD5
          private static String convertToHex(byte[] data) {
            StringBuilder buf = new StringBuilder();
            for (int i = 0; i < data.length; i++) {
                int halfbyte = (data[i] >>> 4) & 0x0F;
                int two_halfs = 0;
                do {
                    if ((0 <= halfbyte) && (halfbyte <= 9))
                        buf.append((char) ('0' + halfbyte));
                    else
                        buf.append((char) ('a' + (halfbyte - 10)));
                    halfbyte = data[i] & 0x0F;
                } while(two_halfs++ < 1);
            }
            return buf.toString();
        }
     
     
           private static  String MD5(String text)    {
            MessageDigest md;
            md = MessageDigest.getInstance("MD5");
            byte[] md5hash = new byte[32];
            md.update(text.getBytes("iso-8859-1"), 0, text.length());
            md5hash = md.digest();
            return convertToHex(md5hash);

    As you understand i have to convert MimeMessage into String!My problem is when MimeMessage is type Multipart every time I close my programm the next time it gives me a different MD5 hash!!!But when MimeMessage is NOT Multipart it gives me always the same MD5 hash.The code i use to convert MimeMessage to String is the following:

    //This iis an example code
    MimeMessage message;
    String temp;
     
    //i use it like this
    temp=parseISToString(message.getInputStream());
     
     //Input stream to a string
        public static String parseISToString(java.io.InputStream is){
            java.io.DataInputStream din = new java.io.DataInputStream(is);
            StringBuilder sb = new StringBuilder();
            try{
            String line = null;
            while((line=din.readLine()) != null){
                    sb.append(line).append("\n");
            }
            }catch(Exception ex){
                ex.getMessage();
                }finally{
            try{
                    is.close();
            }catch(Exception ex){}
            }
            return sb.toString();
        }

    I would be grateful if someone gave a solution to this or give me a new way to compute MD5 hash from a MimeMessage type!!Thank you...


  2. #2
    Member
    Join Date
    Oct 2010
    Posts
    40
    Thanks
    0
    Thanked 2 Times in 1 Post

    Default Re: MD5 hash and MimeMessage problem

    try this. hope it helps...

        public String toMD5Hash(String str) throws NoSuchAlgorithmException{
           MessageDigest md = MessageDigest.getInstance("MD5");
           md.update(str.getBytes()); 
           byte[] mdbytes = md.digest();
     
           //convert the byte to hex format method 1
           StringBuffer sb = new StringBuffer();
           for (int i = 0; i < mdbytes.length; i++) {
             sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
           }
     
           return sb.toString();
        }

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Location
    Volos Greece
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: MD5 hash and MimeMessage problem

    Thank you for your answer and happy new year!!I've tried your code but i have the same results!!So i think the poblem is not with the code of MD5 but with the input String i give...The code i use to turn the MimeMessage into String is :

    //This iis an example code
    MimeMessage message;
    String temp;
     
    //I use method .getInputString an then i convert InputString into a String with parseISToString
    //Thats how i get the String for the MD5 
     
    temp=parseISToString(message.getInputStream());
     
     //Input stream to a string
        public static String parseISToString(java.io.InputStream is){
            java.io.DataInputStream din = new java.io.DataInputStream(is);
            StringBuilder sb = new StringBuilder();
            try{
            String line = null;
            while((line=din.readLine()) != null){
                    sb.append(line).append("\n");
            }
            }catch(Exception ex){
                ex.getMessage();
                }finally{
            try{
                    is.close();
            }catch(Exception ex){}
            }
            return sb.toString();
        }

    Somehow Multipart MimeMessage gives different String every time i close and reopen the program!!!Has anybody an idea why this happenning?? Thank you in advance!!

  4. #4
    Junior Member
    Join Date
    Dec 2010
    Location
    Volos Greece
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: MD5 hash and MimeMessage problem

    I found the problem!This is how you turn a MimeMessage to a String :

               int size;
               InputStream temp1 = null;
               String temp = new String();
                byte[] b;
               String value;
     
     
     
                size=message.getSize();
                temp1=message.getInputStream();
                b=new byte[size];
                temp1.read(b,0,size);
                value=new String(b);

    The value is the String you put in MD5(value) !!!!

Similar Threads

  1. Hash Functions
    By Blueshark in forum Java Theory & Questions
    Replies: 3
    Last Post: July 2nd, 2012, 03:04 PM
  2. Constructors, Hash Tables, & Linked Lists
    By illusion887 in forum Collections and Generics
    Replies: 2
    Last Post: December 3rd, 2009, 03:46 AM
  3. convert arraylist to a hash map
    By nadman123 in forum Collections and Generics
    Replies: 1
    Last Post: July 29th, 2009, 04:24 AM
  4. Comparing hash functions and collision resolutions
    By dansongarcia in forum Collections and Generics
    Replies: 0
    Last Post: November 11th, 2008, 10:50 AM