Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2005

Tip: Looking for answers? Try searching our database.

MD5 implementation help please...

Thread view: 
Aerodyne - 29 Jun 2005 12:46 GMT
Hi all,

I've copyed this code from someone else and trying to get it to work...

public static String getKeyedDigest(String input, String key) {
:
:
           MessageDigest md = MessageDigest.getInstance("MD5");
           md.update(input.getBytes());
           digest = md.digest(key.getBytes());
           dstr = new BigInteger(1, digest).toString(16);
           /* this is important, toString leaves out initial 0 */
           if (dstr.length() % 2 > 0)
               dstr = "0" + dstr;
       } catch (NoSuchAlgorithmException ex) {
           ex.printStackTrace();
       }

Unfortunately it retuns the correct check ... but not the leading
zero's which I need.

What it returned: 96f07821d958764gcc1fcd76d12358
expected:       0096f07821d958764gcc1fcd76d12358

or

What it returned: 0454abc257b09547a8a7f87c785328
expected:       000454abc257b09547a8a7f87c785328

TIA
shakah - 29 Jun 2005 13:01 GMT
> Hi all,
>
[quoted text clipped - 26 lines]
>
> TIA

Two quick hacks:
 dstr = new BigInteger(1, digest).toString(16);
 while(dstr.length() < 32) {
   dstr = "0" + dstr ;
 }

 -- or --
 dstr = (new BigInteger("100000000000000000000000000000000").add(
        new BigInteger(1,
digest).toString(16)).toString(16)).substring(2) ;
Roedy Green - 30 Jun 2005 07:26 GMT
>Unfortunately it retuns the correct check ... but not the leading
>zero's which I need.

add 10000000000000000000000000000 and trim it off later, or count the
missing 0s in the final result and prepend some.

See Misc.rep in the http://mindprod.com/products.html#BUSINESS

Signature

Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.