views:

77

answers:

2

I'm pulling 500 results from a search query to a webservice; I store these in the session for that user so pagnation doesnt cause further calls.

What I want to do is stick the parameters together into one long string and hash them so I have a quick hash to check against.

in php this would look something like...

<?php
$_SESSION["shash"] = md5($_GET['x'] . $_GET['y'] . $_GET['z']);
?>

Done Lazily anyway.

So in C# I have...

    #region Session Check
        string sCheckStr = rarREF;
        string searchCheck = GetMd5Sum(sCheckStr);
        if ((Session["schk"].ToString().Length > 0) && (Session["schk"].ToString() == searchCheck))
        { }
        else
        {
            if (searchResults != null) this.mySess.SessionVariables.SearchResults = null;
            Session["schk"] = searchCheck;
        }
    #endregion

And apparently no default MD5 Class built in so I've used one off another site.

  #region MD5 Class
    static public string GetMd5Sum(string str)
    {
        // First we need to convert the string into bytes, which
        // means using a text encoder.
        Encoder enc = System.Text.Encoding.Unicode.GetEncoder();

        // Create a buffer large enough to hold the string
        byte[] unicodeText = new byte[str.Length * 2];
        enc.GetBytes(str.ToCharArray(), 0, str.Length, unicodeText, 0, true);

        // Now that we have a byte array we can ask the CSP to hash it
        MD5 md5 = new MD5CryptoServiceProvider();
        byte[] result = md5.ComputeHash(unicodeText);

        // Build the final string by converting each byte
        // into hex and appending it to a StringBuilder
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < result.Length; i++)
        {
            sb.Append(result[i].ToString("X2"));
        }

        // And return it
        return sb.ToString();
    }
    #endregion

Which doesn't work properly. rarRef is in the original (public ActionResult Index(string rarREF)) Is there a quicker way, as this needs to be fast. Would Base64 Encoding it do?

+2  A: 

That's a pretty inefficient way of doing the conversion - as well as longwinded. There are certainly simpler ways of doing it.

Here's a much simpler routine to get an MD5 hash for a string, converted back to a string too:

public static string Md5HashString(string input)
{
    byte[] bytes = Encoding.UTF8.GetBytes(input);
    byte[] hash;
    using (MD5 md5 = MD5.Create())
    {
        hash = md5.ComputeHash(bytes);
    )
    return Convert.ToBase64String(hash);
}

Having said that, the original routine (now that I've looked at it more carefully) looks like it should have been working okay. I'd recommend using the above anyway, as it's simpler, but...

If that still doesn't work properly for you, could you explain exactly what you observe? What is happening?

Jon Skeet
I'd be inclined to dispose of the created MD5 instance given that it implements IDisposable though...
Greg Beech
Eek, hadn't spotted that. *Hangs head in shame.* Will edit.
Jon Skeet
but why are we doing it at all? Sounds like we're trying to build a HashTable - we have libraries for that.
djna
@djna - we would know if the question weren't abandoned since it was written :)
John Rasch
Sorry for 'abandoning the issue' I had to drive back from work and proceed to bang my head on the wall for a few hours.Anyhoo, I just wanted the most efficient way to hash the query parameters and save it, djna's suggestion of a hash table sounds quite cool and neat. I looked at MD5 as its a quick way of doing the job... in PHP, why the hell it cant be a built in function in C# is beyond me. Anyway thanks again :o)
Chris M
+1  A: 

Why not just use a hash table?

The key would be your concatenated query parameters (with a suitable separator) the value would be your result object. Allow the HashTable class to deal with hashing and collisions

djna
That's actually a very good idea... I'll bugger off and look at HashTables now :o)
Chris M