aspose file tools*
The moose likes Security and the fly likes java MD5 hashed string doesn't match the one given by md5sum command Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » Security
Bookmark "java MD5 hashed string doesn Watch "java MD5 hashed string doesn New topic
Author

java MD5 hashed string doesn't match the one given by md5sum command

Leon scott
Greenhorn

Joined: May 14, 2013
Posts: 2
Hi guys,

I need your help, i want to hash a string with java. Here is the code i use :


public static void main(String[] args)throws Exception
{
String password = "testtest";

MessageDigest md = MessageDigest.getInstance("MD5");
md.update(password.getBytes());

byte byteData[] = md.digest();

StringBuffer hexString = new StringBuffer();
for (int i=0;i<byteData.length;i++) {
String hex=Integer.toHexString(0xff & byteData[i]);
if(hex.length()==1) hexString.append('0');
hexString.append(hex);
}
System.out.println(hexString.toString());
}

The result obtained is : 05a671c66aefea124cc08b76ea6d30bb

The problem is that this result is different from the one given by md5sum on Ubuntu

# echo testtest | md5sum
199389a12492266114933fc428e8cfdc -

Can anyone tell me what it is wrong with my code to get a different hash for the same string ?

Thanks in advance for your responses.
Leon scott
Greenhorn

Joined: May 14, 2013
Posts: 2
I found the problem : my java code was OK, the problem comes from the echo command that adds a newline caracter to the string to be hashed.
Use the command below instead :
# echo -n testtest | md5sum
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4634
    
    5

Right, MD5 and SHA* are defined on octets (unsigned bytes) and any changes will give you different results.

Most programatic usages have to decide whether or not to include line endings, and if so, what line endings to use. OS-X and Linux use different line endings than Windows.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java MD5 hashed string doesn't match the one given by md5sum command
 
Similar Threads
MD5
MD5 Class
MD5.java doesn't match md5sum
Converting String to Byte[] array
There is no Action mapped for action name prePaymentManager