MD5 implementation help please...

A

Aerodyne

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
 
S

shakah

Aerodyne said:
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

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) ;
 
R

Roedy Green

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

--
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top