|
|||
|
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 |
|
|
||||
|
||||
|
|
|
|||
|
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 > |
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |