locked
Simple cheksum calculation. RRS feed

  • Question

  • All,

    We have a requirment where in we need to caluclate the checksum of a string and return an double/int.

    Are there any out of the box methods from .net which will dp this for us?

    if not can you please point us to any links thats doing the same?

     

    Thanks


    Monday, July 25, 2011 12:19 PM

Answers

  • Hi

    I think what you are looking for is something along these lines ?

     

     

      string str = "string for test";
      System.Security.Cryptography.MD5CryptoServiceProvider provider = new System.Security.Cryptography.MD5CryptoServiceProvider();
      byte[] byteArray = provider.ComputeHash(System.Text.Encoding.UTF8.GetBytes(str));
    
      double d = BitConverter.ToDouble(byteArray, 0);
      int i = BitConverter.ToInt32(byteArray, 0);
    



    Edit: Or just use the regular str.GetHashCode();
    • Proposed as answer by Marcus Pålsson Monday, July 25, 2011 12:50 PM
    • Marked as answer by madhugilla Monday, July 25, 2011 3:19 PM
    • Unmarked as answer by madhugilla Monday, July 25, 2011 3:21 PM
    • Marked as answer by madhugilla Monday, July 25, 2011 3:21 PM
    Monday, July 25, 2011 12:28 PM
  • Thank you Marcus on final question

     

    Does the code return the same value irrespective of how many time you run it ? (on the same machine). i tried it couple of times, it returned the same value.


    Regards, Madhukar Gilla If this answers your question please mark it accordingly

    MD5 will always return the same value for the same string (with the same encoding).
    • Marked as answer by madhugilla Tuesday, July 26, 2011 12:40 PM
    Monday, July 25, 2011 3:41 PM

All replies

  • Hi

    I think what you are looking for is something along these lines ?

     

     

      string str = "string for test";
      System.Security.Cryptography.MD5CryptoServiceProvider provider = new System.Security.Cryptography.MD5CryptoServiceProvider();
      byte[] byteArray = provider.ComputeHash(System.Text.Encoding.UTF8.GetBytes(str));
    
      double d = BitConverter.ToDouble(byteArray, 0);
      int i = BitConverter.ToInt32(byteArray, 0);
    



    Edit: Or just use the regular str.GetHashCode();
    • Proposed as answer by Marcus Pålsson Monday, July 25, 2011 12:50 PM
    • Marked as answer by madhugilla Monday, July 25, 2011 3:19 PM
    • Unmarked as answer by madhugilla Monday, July 25, 2011 3:21 PM
    • Marked as answer by madhugilla Monday, July 25, 2011 3:21 PM
    Monday, July 25, 2011 12:28 PM
  • Just a note

    The value returned by GetHashCode is platform-dependent. It differs on the 32-bit and 64-bit versions of the .NET Framework.

    Monday, July 25, 2011 12:51 PM
  • WHS ^^

    Also, the value isn't guaranteed to be the same from run to run, so it should never be persisted.

    Monday, July 25, 2011 1:59 PM
  • MD5 may be a bit overkill, for a simpler implementation of a check digit, there are some already in use by various systems.

    For example, credit cards have an intrinsic (non-printed) check character. This is where you make your data in such a way that a check digit % 10 is always 0. http://www.beachnet.com/~hstiles/cardtype.html Bank account numbers work in a similar fashion.

    Another, if you have to support more than base-10 digits, is some schemes for calculating barcode check characters. USPS labels are what I've implemented in the past: (Scroll down to the 'Calculation example for x' sections)

    http://www.idautomation.com/code128faq.html#CalculationExamples

    These barcodes are implemented with a slightly different character code table, but I think you can adapt it for ASCII character codes. I think the mod-by value is supposed to be a prime number.

    Monday, July 25, 2011 2:24 PM
  • Thank you Marcus on final question

     

    Does the code return the same value irrespective of how many time you run it ? (on the same machine). i tried it couple of times, it returned the same value.


    Regards, Madhukar Gilla If this answers your question please mark it accordingly
    Monday, July 25, 2011 3:21 PM
  • Thank you Marcus on final question

     

    Does the code return the same value irrespective of how many time you run it ? (on the same machine). i tried it couple of times, it returned the same value.


    Regards, Madhukar Gilla If this answers your question please mark it accordingly

    MD5 will always return the same value for the same string (with the same encoding).
    • Marked as answer by madhugilla Tuesday, July 26, 2011 12:40 PM
    Monday, July 25, 2011 3:41 PM
  • Sorry the delayed answer.

     

    Both solutions will return the same value everytime you run it. the MD5 solution will return same no matter what machine you run it on.

    The second (GetHashCode) can differ if you run 32bits or 64bits application. Also there might be a risk that it will change if they update .net framework.

     

    Tuesday, July 26, 2011 8:29 AM