I want to compute md5 hash from a MimeMessage type.I use the following code to do this:

//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);

The code works perfectly!The problem is with the MimeMessage!I have to turn the MimeMessage into a String and then use md5 code!When the MimeMessage is Multipart ,every time i close the programm the next gives me a different md5 hash!!!The code i use to turn tha MimeMessage into a String is the following :

//This iis an example code
MimeMessage message;
String temp;

//i use it like this message.getInputStream() and then i turn InputStream to a String
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();
    }

If MimeMessage is not Multipart it works just fine!!Has anybody an idea why this happenning to Multipart MimeMessage??

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) !!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.