<p>How do I calculate the CRC32 (Cyclic Redundancy Checksum) of a string in .NET?</p>

[{'answer_id': 8130, 'author': 'Pete', 'author_id': 76, 'author_profile': 'https://Stackoverflow.com/users/76', 'pm_score': 6, 'selected': True, 'text': '<p>This guy seems to have your answer.</p>\n\n<blockquote>\n  <p><a href="https://damieng.com/blog/2006/08/08/calculating_crc32_in_c_and_net" rel="noreferrer">https://damieng.com/blog/2006/08/08/calculating_crc32_in_c_and_net</a></p>\n</blockquote>\n\n<p>And in case the blog ever goes away or breaks the url, here\'s the github link:</p>\n\n<blockquote>\n  <p><a href="https://github.com/damieng/DamienGKit/blob/master/CSharp/DamienG.Library/Security/Cryptography/Crc32.cs" rel="noreferrer">https://github.com/damieng/DamienGKit/blob/master/CSharp/DamienG.Library/Security/Cryptography/Crc32.cs</a></p>\n</blockquote>\n\n<hr>\n\n<p>Usage of the Crc32 class from the blog post:</p>\n\n<pre><code>Crc32 crc32 = new Crc32();\nString hash = String.Empty;\n\nusing (FileStream fs = File.Open("c:\\\\myfile.txt", FileMode.Open))\n  foreach (byte b in crc32.ComputeHash(fs)) hash += b.ToString("x2").ToLower();\n\nConsole.WriteLine("CRC-32 is {0}", hash);\n</code></pre>\n'}, {'answer_id': 51922169, 'author': 'SharpC', 'author_id': 1741690, 'author_profile': 'https://Stackoverflow.com/users/1741690', 'pm_score': 4, 'selected': False, 'text': '<p>Since you seem to be looking to calculate the CRC32 of a <em>string</em> (rather than a file) there\'s a good example here: <a href="https://rosettacode.org/wiki/CRC-32#C.23" rel="nofollow noreferrer">https://rosettacode.org/wiki/CRC-32#C.23</a></p>\n<p>The code, should it ever disappear:</p>\n<pre><code>/// &lt;summary&gt;\n/// Performs 32-bit reversed cyclic redundancy checks.\n/// &lt;/summary&gt;\npublic class Crc32\n{\n    #region Constants\n    /// &lt;summary&gt;\n    /// Generator polynomial (modulo 2) for the reversed CRC32 algorithm. \n    /// &lt;/summary&gt;\n    private const UInt32 s_generator = 0xEDB88320;\n    #endregion\n\n    #region Constructors\n    /// &lt;summary&gt;\n    /// Creates a new instance of the Crc32 class.\n    /// &lt;/summary&gt;\n    public Crc32()\n    {\n        // Constructs the checksum lookup table. Used to optimize the checksum.\n        m_checksumTable = Enumerable.Range(0, 256).Select(i =&gt;\n        {\n            var tableEntry = (uint)i;\n            for (var j = 0; j &lt; 8; ++j)\n            {\n                tableEntry = ((tableEntry &amp; 1) != 0)\n                    ? (s_generator ^ (tableEntry &gt;&gt; 1)) \n                    : (tableEntry &gt;&gt; 1);\n            }\n            return tableEntry;\n        }).ToArray();\n    }\n    #endregion\n\n    #region Methods\n    /// &lt;summary&gt;\n    /// Calculates the checksum of the byte stream.\n    /// &lt;/summary&gt;\n    /// &lt;param name=&quot;byteStream&quot;&gt;The byte stream to calculate the checksum for.&lt;/param&gt;\n    /// &lt;returns&gt;A 32-bit reversed checksum.&lt;/returns&gt;\n    public UInt32 Get&lt;T&gt;(IEnumerable&lt;T&gt; byteStream)\n    {\n        try\n        {\n            // Initialize checksumRegister to 0xFFFFFFFF and calculate the checksum.\n            return ~byteStream.Aggregate(0xFFFFFFFF, (checksumRegister, currentByte) =&gt; \n                      (m_checksumTable[(checksumRegister &amp; 0xFF) ^ Convert.ToByte(currentByte)] ^ (checksumRegister &gt;&gt; 8)));\n        }\n        catch (FormatException e)\n        {\n            throw new CrcException(&quot;Could not read the stream out as bytes.&quot;, e);\n        }\n        catch (InvalidCastException e)\n        {\n            throw new CrcException(&quot;Could not read the stream out as bytes.&quot;, e);\n        }\n        catch (OverflowException e)\n        {\n            throw new CrcException(&quot;Could not read the stream out as bytes.&quot;, e);\n        }\n    }\n    #endregion\n\n    #region Fields\n    /// &lt;summary&gt;\n    /// Contains a cache of calculated checksum chunks.\n    /// &lt;/summary&gt;\n    private readonly UInt32[] m_checksumTable;\n\n    #endregion\n}\n</code></pre>\n<p>and to use it:</p>\n<pre><code>var arrayOfBytes = Encoding.ASCII.GetBytes(&quot;The quick brown fox jumps over the lazy dog&quot;);\n\nvar crc32 = new Crc32();\nConsole.WriteLine(crc32.Get(arrayOfBytes).ToString(&quot;X&quot;));\n</code></pre>\n<p>You can test the input / output values here: <a href="https://crccalc.com/" rel="nofollow noreferrer">https://crccalc.com/</a></p>\n'}, {'answer_id': 61495753, 'author': 'spludlow', 'author_id': 8815031, 'author_profile': 'https://Stackoverflow.com/users/8815031', 'pm_score': 2, 'selected': False, 'text': '<p>Using the logic from the previous answer, this was my take:</p>\n\n<pre><code>public class CRC32\n{\n    private readonly uint[] ChecksumTable;\n    private readonly uint Polynomial = 0xEDB88320;\n\n    public CRC32()\n    {\n        ChecksumTable = new uint[0x100];\n\n        for (uint index = 0; index &lt; 0x100; ++index)\n        {\n            uint item = index;\n            for (int bit = 0; bit &lt; 8; ++bit)\n                item = ((item &amp; 1) != 0) ? (Polynomial ^ (item &gt;&gt; 1)) : (item &gt;&gt; 1);\n            ChecksumTable[index] = item;\n        }\n    }\n\n    public byte[] ComputeHash(Stream stream)\n    {\n        uint result = 0xFFFFFFFF;\n\n        int current;\n        while ((current = stream.ReadByte()) != -1)\n            result = ChecksumTable[(result &amp; 0xFF) ^ (byte)current] ^ (result &gt;&gt; 8);\n\n        byte[] hash = BitConverter.GetBytes(~result);\n        Array.Reverse(hash);\n        return hash;\n    }\n\n    public byte[] ComputeHash(byte[] data)\n    {\n        using (MemoryStream stream = new MemoryStream(data))\n            return ComputeHash(stream);\n    }\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/8128', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/17/']