locked
How to generate CheckSum for the document attached to an entity? RRS feed

  • Question

  • Hello,

    Can anyone help me with the below requirement?

    I need to generate CheckSum for a document body (Content) for the document attached to an entity in MS CRM 2011. The output of the CheckSum should be in 32 digit (eg. 1F47917CE08CF1C23BD82D95D3B5723D).

    Used this function to get the result but ended up with improper format. Please let me know if there is any issue in the below function or suggest with a correct solution in achieving it.

    public static string GenerateCheckSum(string docBodyBase64)
            {
                if (docBodyBase64 != null && !String.IsNullOrWhiteSpace(docBodyBase64))
                {
                    byte[] binaryData;
                    binaryData = System.Convert.FromBase64String(docBodyBase64);

                    using (MD5 md5 = MD5.Create())
                    {
                        return Encoding.Default.GetString(md5.ComputeHash(binaryData));
                    }

                }
                return string.Empty;
            }

    Thanks,

    Sri

    Wednesday, December 3, 2014 8:38 AM

Answers

  • I think your problem is how you are converting the byte array of the hash - I doubt that Encoding.Default.GetString will make a hex string (which I think is the output you want).

    Something like the following should convert this to a hex string

    byte[] hash = md5.ComputeHash(binaryData);
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < hash.Length; i++)
    {
        sb.Append(hash[i].ToString("x2"));
    }
    return sb.ToString();

     

    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    • Marked as answer by K_Srinivas Wednesday, December 3, 2014 1:17 PM
    Wednesday, December 3, 2014 10:24 AM
    Moderator

All replies

  • I think your problem is how you are converting the byte array of the hash - I doubt that Encoding.Default.GetString will make a hex string (which I think is the output you want).

    Something like the following should convert this to a hex string

    byte[] hash = md5.ComputeHash(binaryData);
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < hash.Length; i++)
    {
        sb.Append(hash[i].ToString("x2"));
    }
    return sb.ToString();

     

    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    • Marked as answer by K_Srinivas Wednesday, December 3, 2014 1:17 PM
    Wednesday, December 3, 2014 10:24 AM
    Moderator
  • Thank you David. It worked for me.

    - Sri

    Wednesday, December 3, 2014 1:18 PM