简体   繁体   中英

Calculate Md5 on server side

I am uploading a file using html5 and calculate md5 on client side(JavaScript) Then on the server side (handler) I am inserting the file slices in a table like this:

public void WriteBlobsToDB(byte[] buffer,int id)
    {
        File_Data fl = new File_Data();
        fl.FileId = id;
        fl.FileChunks = buffer;
        try
        {
            WiFileData.InsertOnSubmit(fl);
            dc.SubmitChanges();
        }
        catch (ExecutionEngineException e){throw e;}
    }

and then when all the file slices are saved on the database I try to read them ad save them in a byte[] allData and insert it in another Table:

public void WriteBlobs(int id, string fileName,int count,byte[] allData)
{
    List<chunkInfo> listOfChunks;
    File_Data fd = new File_Data();
    File_List fl = new File_List();
    try
    {   var chunks = (from c in FileData
                      where c.FileId == id
                      orderby c.Id ascending
                      select new chunkInfo
                      {  Id = c.Id,
                         ChunkData = c.FileChunks.ToArray()
                      });
        listOfChunks = chunks.ToList();

        foreach (chunkInfo sChunk in listOfChunks)
        {   fl.FileId = id;
            fl.FileName = fileName;
            int dstoffset = count * sChunk.ChunkData.Length;// count is set to 0 at teh beggginning
            Buffer.BlockCopy(sChunk.ChunkData, 0, allData, dstoffset, sChunk.ChunkData.Length);
            count++;
        }
        int l = allData.Length;
        fl.FileData = new Binary(allData);
        FileList.InsertOnSubmit(fl);
        dc.SubmitChanges();

    }
    catch (Exception){throw;}
}

The code is working and I am saving the data in the database and allDatasize is the same size of my file in the end. I just need to verify if the saved data in Filedata field in the table is write or not. How can I calculate md5 for it and compare it?

Thanks

If you're asking how are you able to do this purely in SQL Server, you could simply leverage the HASH_BYTES function, something like this:

select  stored_md5_column, hashbytes('MD5', Filedata) as md5hash
from    your_table
where   stored_md5_column != hashbytes('MD5', Filedata);

Depending on how you're storing your "stored_md5_column" value, you may need to cast the result of the hashbytes to something else to get the proper equality comparison naturally.

If you're instead looking to perform the check/verification in C#, you instead would want to make use of the MD5 class and something like this after you have your allData array buffer filled:

using (var md5Obj = MD5.Create())
{
    var md5Hash = md5Obj.ComputeHash(allData);
}

This assumes your allData is a byte array (I can't tell from your code). If it is not a byte array, you'll first need to convert it to a standard byte array or stream to pass to the MD5 ComputeHash method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM