Hello,
I have a problem with MessageDigest MD5 in Java. I want to calculate the
digest from a file, encode it in base64 and display it. Now, according to
MD5 spec, digest shall be 128 bits so, encoding it to base64 shall give 44
characters output ((128bit + 4bit padding)/3 = 44). When I execute a
program to digest a given file:
# java Masher Masher.java
# nfEOH/5M+yDLaxaJ+XpJ5 Q==
So, the output is 24 chars, ie. 64bits + 8 bit padding (two '=' chars at
the end represent padding in base64). Now, when I execute the following:
# more Masher.java | openssl md5 | openssl base64
# OWYxNGU2YzZjYTA yOGVhYzJhMjgyM2 E0ZTJhNmU2ZTAK
So, 44 characters as expected. My assumption is that output of these two
commands shall be the same (after all MD5 and base64 implementations shall
give always the same output given same input, or? - excluding the hash
function collision of course.).
What's wrong here? Java's MD5 output is too short? base64 encoding in Java
doesn't work properly? Or am I simply missing something?
The code I used for MD5 calculation in Java is from Knudsen's "Java
Cryptography" book:
// obtain a message digest object
MessageDigest md = MessageDigest.g etInstance("MD5 ");
// calculate the digest for the given file
FileInputStream in = new FileInputStream (args[0]);
byte[] buffer = new byte[8192];
int length;
while ((length = in.read(buffer) ) != -1)
md.update(buffe r,0,length);
byte[] raw = md.digest();
// print out the digest in base64
BASE64Encoder encoder = new BASE64Encoder() ;
String base64 = encoder.encode( raw);
System.out.prin tln(base64);
Any help appreciated.
BRs,
Zulik
I have a problem with MessageDigest MD5 in Java. I want to calculate the
digest from a file, encode it in base64 and display it. Now, according to
MD5 spec, digest shall be 128 bits so, encoding it to base64 shall give 44
characters output ((128bit + 4bit padding)/3 = 44). When I execute a
program to digest a given file:
# java Masher Masher.java
# nfEOH/5M+yDLaxaJ+XpJ5 Q==
So, the output is 24 chars, ie. 64bits + 8 bit padding (two '=' chars at
the end represent padding in base64). Now, when I execute the following:
# more Masher.java | openssl md5 | openssl base64
# OWYxNGU2YzZjYTA yOGVhYzJhMjgyM2 E0ZTJhNmU2ZTAK
So, 44 characters as expected. My assumption is that output of these two
commands shall be the same (after all MD5 and base64 implementations shall
give always the same output given same input, or? - excluding the hash
function collision of course.).
What's wrong here? Java's MD5 output is too short? base64 encoding in Java
doesn't work properly? Or am I simply missing something?
The code I used for MD5 calculation in Java is from Knudsen's "Java
Cryptography" book:
// obtain a message digest object
MessageDigest md = MessageDigest.g etInstance("MD5 ");
// calculate the digest for the given file
FileInputStream in = new FileInputStream (args[0]);
byte[] buffer = new byte[8192];
int length;
while ((length = in.read(buffer) ) != -1)
md.update(buffe r,0,length);
byte[] raw = md.digest();
// print out the digest in base64
BASE64Encoder encoder = new BASE64Encoder() ;
String base64 = encoder.encode( raw);
System.out.prin tln(base64);
Any help appreciated.
BRs,
Zulik
Comment