How to convert CryptHashData to .Net MD5CryptoServiceProvider?

Discussion in 'ASP .Net Security' started by Tino, Feb 10, 2009.

  1. Tino

    Tino Guest

    Hello All,

    I am trying to re-write an old functionality that we used in a Borland C++
    application usung the CryptHashData Windows function. It works good so far
    but I am having trouble getting the same hash value if I am using a salt.

    Here is what we did in the old software:
    CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
    CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash);
    CryptHashData(hHash, pbData, SaltLength, 0); // add the salt to the hash
    (salt is in pbData)
    CryptHashData(hHash, pSourceString.c_str(), pSourceString.Length(), 0); //
    hash the source string
    CryptGetHashParam(hHash, HP_HASHVAL, rgbHash, &cbHash, 0); // retrieve the
    hash

    .... worked - as said - beautifully.
    Now I am doing:

    HashAlgorithm md5 = new MD5CryptoServiceProvider();
    result = md5.ComputeHash(encoding.GetBytes(tbSource.Text));

    ....like a dream! It gives the same hash value if I am not using any salt in
    the first algorithm. But now if I am using salt, how can I do that with
    MD5CryptoServiceProvider? I thought I could just add it to the source string
    but that gives me a different hash.

    Certainly there is a way - I just can't get my head around it.

    Any help highly appreciated!

    Best Regards,
    Tino
    Tino, Feb 10, 2009
    #1
    1. Advertising

  2. Tino

    Tino Guest

    Sorry for posting the same message twice. I got an error when I sent it the
    first time.

    "Tino" wrote:

    > Hello All,
    >
    > I am trying to re-write an old functionality that we used in a Borland C++
    > application usung the CryptHashData Windows function. It works good so far
    > but I am having trouble getting the same hash value if I am using a salt.
    >
    > Here is what we did in the old software:
    > CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
    > CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash);
    > CryptHashData(hHash, pbData, SaltLength, 0); // add the salt to the hash
    > (salt is in pbData)
    > CryptHashData(hHash, pSourceString.c_str(), pSourceString.Length(), 0); //
    > hash the source string
    > CryptGetHashParam(hHash, HP_HASHVAL, rgbHash, &cbHash, 0); // retrieve the
    > hash
    >
    > ... worked - as said - beautifully.
    > Now I am doing:
    >
    > HashAlgorithm md5 = new MD5CryptoServiceProvider();
    > result = md5.ComputeHash(encoding.GetBytes(tbSource.Text));
    >
    > ...like a dream! It gives the same hash value if I am not using any salt in
    > the first algorithm. But now if I am using salt, how can I do that with
    > MD5CryptoServiceProvider? I thought I could just add it to the source string
    > but that gives me a different hash.
    >
    > Certainly there is a way - I just can't get my head around it.
    >
    > Any help highly appreciated!
    >
    > Best Regards,
    > Tino
    >
    >
    Tino, Feb 10, 2009
    #2
    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. Andreas Klemt
    Replies:
    1
    Views:
    387
    Karl Seguin
    Jul 23, 2003
  2. Mike
    Replies:
    1
    Views:
    188
    S├ębastien Pouliot
    Oct 16, 2003
  3. Viktor Popov

    MD5CryptoServiceProvider class

    Viktor Popov, Sep 24, 2004, in forum: ASP .Net Security
    Replies:
    2
    Views:
    182
    Gabe Garza
    Sep 24, 2004
  4. Tino
    Replies:
    0
    Views:
    791
  5. Derrick
    Replies:
    0
    Views:
    194
    Derrick
    Aug 23, 2004
Loading...

Share This Page