MD5CryptoServiceProvider (ported from CryptHashData)

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

  1. Tino

    Tino Guest

    Hello all,

    I am having some problems converting an old Borland C++ program into C# with
    ..Net. In the old software, we used a salt value to calculate the hash. We did
    like this:

    CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
    CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash);

    // add salt to the hash (the salt is in phData)
    CryptHashData(hHash, pbData, SaltLength, 0);

    // hash source string
    CryptHashData(hHash, pSourceString.c_str(), pSourceString.Length(), 0);

    // retrieve hashed value
    CryptGetHashParam(hHash, HP_HASHVAL, rgbHash, &cbHash, 0)

    .... it worked like a dream. Now I did:

    System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
    HashAlgorithm md5 = new MD5CryptoServiceProvider();
    byte[] result = md5.ComputeHash(encoding.GetBytes(tbSource.Text));

    .... works even better - and gives me the same hash value as long as I am not
    using any salt. Now my question is, how do I get the salt value into the hash
    with the new .NET - method?

    I tried combining the salt with the source string before hashing it but that
    gives a different value. How would I do it to get the same result as in the
    old method when using a salt?

    Thanks in advance and best regards!

    Tino
     
    Tino, Feb 10, 2009
    #1
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.