Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Security / October 2005

Tip: Looking for answers? Try searching our database.

Calculate MD5 using also a signature

Thread view: 
theodosisx@yahoo.com - 27 Sep 2005 15:23 GMT
Hello *,

I found/modified the source bellow to calculate the MD5 of a file and
it works perfect. My question is, how can I add a more argument in the
function so that I can include a signature in the calculation of MD5.

 /**
  * MD5
  *
http://groups.google.com/group/comp.lang.java.programmer/browse_thread/thread/77
a5d25babc6a839/3188354930173acc?q=md5&rnum=1&hl=en#3188354930173acc

  * @param filename
  * @return
  */
 public static char[] calcMD5(String filename)
 {
   MessageDigest md = null;
   char[] char_md5 = new char[16];
   byte[] byte_md5 = new byte[16];
   int i = 0;
   int help_md5;
   File f;
   try
   {
     f = new File(filename);
     md = MessageDigest.getInstance("MD5");
     InputStream in = new FileInputStream(f);
     byte[] buffer = new byte[8192];    // arbitrary buffer size
     int count = in.read(buffer);
     while (count != -1)
     {
       md.update(buffer, 0, count);
       count = in.read(buffer);
     }
   }
   catch (Exception ex)
   {
     ex.printStackTrace();
   }

   byte_md5 = md.digest();
   for ( i = 0; i < 16; i++)
   {
     help_md5 = byte_md5[i];
     if ( help_md5 < 0 )
       help_md5 += 256;
     char_md5[i] = (char) help_md5;
   }
   return char_md5;
 }

thanks.
Tommy Grändefors - 27 Sep 2005 21:50 GMT
Hi,

What do you mean by "adding one more argument to the function for
including a signature"? A signature is created when a hash valus is
encrypted with a private key. It is not part of the hash value. Do you
mean that you want to append some (any) additional data for hashing, in
addition to the one contained in the file?

Btw, when hashing data from streams, you can use the class
'java.security.DigestInputStream' for this. Here's a modded variant of
your function:

  public static char[] calcMD5(String filename) throws Exception
  {
       FileInputStream fis = new FileInputStream(filename);
       MessageDigest md = MessageDigest.getInstance("MD5");

       try {
           DigestInputStream dis = new DigestInputStream(fis, md);
           byte[] buffer = new byte[8192];
           while(dis.read(buffer) != -1)
           ;
       } finally {
           fis.close();
       }

       byte[] bDigest = md.digest();
       char[] cDigest = new char[16];
       for(int i = 0; i < 16; i++)
           cDigest[i] = (char)(bDigest[i] & 0xff);

       return cDigest;
  }

Regards,
Tommy Grändefors
www.pheox.com

> Hello *,
>
[quoted text clipped - 47 lines]
>
>  thanks.
theodosisx@yahoo.com - 28 Sep 2005 09:05 GMT
Hello

Thank you for the optimizations in the source.

Yes, that is what I meant. I want to produce the md5 of a file using
additional data for hashing. Can these data be of any length/type?

Theodosis
Tommy Grändefors - 28 Sep 2005 20:04 GMT
Yes, the additional data can be of any length, but must be an array of
bytes. Just feed the hash engine through one of its update() methods
with your data, and you'll be fine.

Here's another version supporting your extra information to be hashed:

   public static char[] calcMD5(String filename, byte[] extra)
       throws Exception
   {
       FileInputStream fis = new FileInputStream(filename);
       MessageDigest md = MessageDigest.getInstance("MD5");

       try {
           DigestInputStream dis = new DigestInputStream(fis, md);
           byte[] buffer = new byte[8192];
           while(dis.read(buffer) != -1)
           ;
       } finally {
           fis.close();
       }

       if(extra != null && extra.length > 0)
           md.update(extra);

       byte[] bDigest = md.digest();
       char[] cDigest = new char[16];
       for(int i = 0; i < 16; i++)
           cDigest[i] = (char)(bDigest[i] & 0xff);

       return cDigest;
   }

Regards,
Tommy Grändefors
www.pheox.com

> Hello
>
[quoted text clipped - 4 lines]
>
> Theodosis
theodosisx@yahoo.com - 29 Sep 2005 09:01 GMT
Yes, thank you very much Tommy,

it seems simpler than I though to be.

thank you once again,

Theodosis
JavaByExample_at_KickJava_com@yahoo.com - 07 Oct 2005 12:16 GMT
Wow, Your code is just simple and works great.

Thanks Tom

David J
--------------------------------------------------------------
http://KickJava.com/news - Daily Java news and articles, updated
continuously from 100+ sources
Michel Gallant - 07 Oct 2005 15:46 GMT
Here's a little signed Java applet (for JPI 1.4+) which computes
SHA1 or MD5 hash for specified string or specified local file,
and displays the result as box hex and b64:
 http://www.jensign.com/JavaScience/www/messagedigestj2
Source code included (extended example based on Core Java sample).
- Mitch Gallant

> Wow, Your code is just simple and works great.
>
[quoted text clipped - 4 lines]
> http://KickJava.com/news - Daily Java news and articles, updated
> continuously from 100+ sources


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.