Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.java.* > Newsgroup comp.lang.java.security

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 07-11-2003, 01:21 AM
Michael Amling
Guest
 
Posts: n/a
Default Re: Problem with MD5

Henry wrote:
> Hi,
>
> I try to create a finger print of a simple String with MessageDigest.
> I want to have a 128 digit fingerprint with numbers and letters
> but I receive just some ugly crap like Ÿ/þ¡³ù±íK.§—¬d¿.
> Please somebody tell me how I can get the beautiful 128 digit
> fingerprint as a String out of a byte array.
>
> MessageDigest md5 = MessageDigest.getInstance("MD5");
> md5.update("A stupid programmer".getBytes());
> System.out.println(new String(md5.digest()));


It's binary data, so conversion to String using the default encoding
is not likely to give you what you want. You could try converting to
hexadecimal:

byte[] digest=md5.digest();
StringBuffer sb=new StringBuffer();
for (int jj=0; jj<digest.length; ++jj) {
// Append two hexadecimal digits for each byte.
sb.append("0123456789ABCDEF".charAt((digest[jj]>>4) & 0x0F));
sb.append("0123456789ABCDEF".charAt(digest[jj] & 0x0F));
}
System.out.println(sb.toString());

--Mike Amling

Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 07-11-2003, 08:43 AM
JK
Guest
 
Posts: n/a
Default Re: Problem with MD5

Even easier, if you don't need leading zeros:

byte[] digest=md5.digest();
String hex = (new BigInteger(1, digest)).toString(16);

JK.

Michael Amling wrote:
> Henry wrote:
>
>> Hi,
>>
>> I try to create a finger print of a simple String with MessageDigest.
>> I want to have a 128 digit fingerprint with numbers and letters but I
>> receive just some ugly crap like Ÿ/þ¡³ù±íK.§—¬d¿.
>> Please somebody tell me how I can get the beautiful 128 digit
>> fingerprint as a String out of a byte array.
>>
>> MessageDigest md5 = MessageDigest.getInstance("MD5");
>> md5.update("A stupid programmer".getBytes());
>> System.out.println(new String(md5.digest()));

>
>
> It's binary data, so conversion to String using the default encoding
> is not likely to give you what you want. You could try converting to
> hexadecimal:
>
> byte[] digest=md5.digest();
> StringBuffer sb=new StringBuffer();
> for (int jj=0; jj<digest.length; ++jj) {
> // Append two hexadecimal digits for each byte.
> sb.append("0123456789ABCDEF".charAt((digest[jj]>>4) & 0x0F));
> sb.append("0123456789ABCDEF".charAt(digest[jj] & 0x0F));
> }
> System.out.println(sb.toString());
>
> --Mike Amling
>


Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Both character and numeric, Problem SUBSCRIBE SAS-L Yufei Wang Newsgroup comp.soft-sys.sas 0 11-17-2006 08:06 PM
Re: SAS Problem nospam@HOWLES.COM (Howard Schreier Newsgroup comp.soft-sys.sas 0 05-03-2006 03:23 AM
Re: SAS ETL- is Password Protection for files a problem? GE Consumer Finance Newsgroup comp.soft-sys.sas 1 01-18-2006 09:17 AM
Re: Help on a grouping problem nospam@HOWLES.COM (Howard Schreier Newsgroup comp.soft-sys.sas 0 02-16-2005 02:52 AM
Re: Help on a grouping problem Sigurd Hermansen Newsgroup comp.soft-sys.sas 0 02-15-2005 11:49 PM



All times are GMT. The time now is 05:17 AM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.