473,378 Members | 1,369 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Java's MD5 output shorter than 128 bits?

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+XpJ5Q==
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
# OWYxNGU2YzZjYTAyOGVhYzJhMjgyM2E0ZTJhNmU2ZTAK
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.getInstance("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(buffer,0,length);
byte[] raw = md.digest();

// print out the digest in base64
BASE64Encoder encoder = new BASE64Encoder();
String base64 = encoder.encode(raw);
System.out.println(base64);

Any help appreciated.

BRs,
Zulik
Jul 17 '05 #1
2 13354
Zulik <zu*****@yahoo.com> said:
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+XpJ5Q==
So, the output is 24 chars, ie. 64bits + 8 bit padding (two '=' chars at
the end represent padding in base64).
Are you certain you're calculating this correctly? The divisor of three
you're using seems strange. I guess it should be 4/3, and then your
source number should be bytes, not bits, as you can encode 3 binary bytes
into four base64-characters.

A single base64 -character carries 6 bits of data, thus in base64
you can express 128 bits with 128 / 6 = 21 characters and two bits
(so, 22 characters).
Now, when I execute the following:
# more Masher.java | openssl md5 | openssl base64


Note that here the output of "openssl md5" is already hex-encoded,
so you're getting two bytes for each one binary byte, so your output
length is twice the binary length (128 bits would be 16 binary bytes;
output of "openssl md5" is 33 bytes; 32 bytes of hex-encided data and
a newline; output of "openssl md5 -binary" is exactly 16 bytes).

However, if you do
echo foo | openssh md5 -binary | openssl base64
you get again 24 chars (plus newline) - which is nicely 22*6=132 bits,
so 128 bits and 4 bits of padding.
--
Wolf a.k.a. Juha Laiho Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)
Jul 17 '05 #2
On Sun, 15 Feb 2004 13:27:01 +0000, Juha Laiho wrote:

Terve,

<cut>
....
</cut>
Note that here the output of "openssl md5" is already hex-encoded,
so you're getting two bytes for each one binary byte, so your output
length is twice the binary length (128 bits would be 16 binary bytes;
output of "openssl md5" is 33 bytes; 32 bytes of hex-encided data and
a newline; output of "openssl md5 -binary" is exactly 16 bytes).

However, if you do
echo foo | openssh md5 -binary | openssl base64
you get again 24 chars (plus newline) - which is nicely 22*6=132 bits,
so 128 bits and 4 bits of padding.


Yup, you're right - I somehow missed the point that digest() method gives
pure binary result. I encoded buffer to hex and the result is now correct...

Kiitos!

BRs,
Zulik
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Lem | last post by:
I get the error Exception in thread "main" java.lang.NoClassDefFoundError when I type java app2 in the command prompt. I've tried moving to the jre directory and typed java c:\app2\app2, but it...
4
by: Laura P | last post by:
Hi, I wasn't sure whether this should be posted in the Java are or in a Solaris thread, so I shall post it in both. Sorry for the duplication. I am new to Solaris and am having trouble...
2
by: Dale Gerdemann | last post by:
I'm having trouble with Unicode encoding in DOM. As a simple example, I read in a UTF-8 encoded xml file such as: <?xml version="1.0" encoding="UTF-8" standalone="no"?> <aText>letter 'a' with...
289
by: napi | last post by:
I think you would agree with me that a C compiler that directly produces Java Byte Code to be run on any JVM is something that is missing to software programmers so far. With such a tool one could...
6
by: Rhino | last post by:
I'm trying to debug a simple Java UDF written in the DB2General style within Eclipse. I'm getting a java.lang.UnsatisfiedLinkError when I execute the set() method in the UDF. I know that the...
458
by: wellstone9912 | last post by:
Java programmers seem to always be whining about how confusing and overly complex C++ appears to them. I would like to introduce an explanation for this. Is it possible that Java programmers...
3
by: AHanso | last post by:
Hey I am new to C# (My background is in Java), I am writing a C# application (that uses the Compact Framework) that communicates to a Java server. To login the server is expecting the password...
8
by: Gabriele | last post by:
I'm studying differences between those two architecture in order to choose a setup to develop a completely new application. I'm a programmer with some experience on C++ and PHP and i have basic...
0
by: r035198x | last post by:
Inheritance We have already covered one important concept of object-oriented programming, namely encapsulation, in the previous article. These articles are not articles on object oriented...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.