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