MD5 implementation help please...

Discussion in 'Java' started by Aerodyne, Jun 29, 2005.

  1. Aerodyne

    Aerodyne Guest

    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
    Aerodyne, Jun 29, 2005
    #1
    1. Advertising

  2. Aerodyne

    shakah Guest

    Aerodyne wrote:
    > 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) ;
    shakah, Jun 29, 2005
    #2
    1. Advertising

  3. Aerodyne

    Roedy Green Guest

    On 29 Jun 2005 04:46:51 -0700, "Aerodyne" <> wrote or
    quoted :

    >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
    Roedy Green, Jun 30, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. KK
    Replies:
    2
    Views:
    522
    Big Brian
    Oct 14, 2003
  2. Replies:
    9
    Views:
    17,060
    John Salerno
    May 8, 2006
  3. Casper Hornstrup

    MD5 implementation

    Casper Hornstrup, Feb 4, 2004, in forum: ASP .Net Security
    Replies:
    3
    Views:
    170
    Hernan de Lahitte
    Feb 4, 2004
  4. Peter Woodsky

    create a md5 / md5 passwd with a salt

    Peter Woodsky, Nov 20, 2008, in forum: Ruby
    Replies:
    6
    Views:
    193
    Brian Candler
    Nov 21, 2008
  5. Replies:
    2
    Views:
    79
Loading...

Share This Page