Leon scott

Greenhorn
+ Follow
since May 14, 2013
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Leon scott

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
10 years ago
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.
10 years ago