[{"data":1,"prerenderedAt":288},["ShallowReactive",2],{"blog:2006:calculating_crc32_in_c_and_net":3,"blogMore-Development":274,"comments-calculating_crc32_in_c_and_net":287},{"id":4,"title":5,"body":6,"category":256,"commentCount":257,"date":258,"description":12,"excerpt":259,"extension":260,"filenames":261,"hidden":262,"image":261,"meta":263,"minutes":91,"navigation":114,"path":264,"seo":265,"showCategory":261,"stem":266,"tags":267,"updated":261,"url":271,"wordCount":272,"__hash__":273},"content\u002Fblog\u002F2006\u002Fcalculating_crc32_in_c_and_net.md","Calculating CRC-32 in C# and .NET",{"type":7,"value":8,"toc":254},"minimark",[9,13,24,32,35,49,52,245,250],[10,11,12],"p",{},"Just a few days ago I found myself needing to calculate a CRC-32 in .NET. With so many facilities available I was a little shocked that there was nothing built-in to do it so knocked up something myself.",[14,15,16],"blockquote",{},[10,17,18,19],{},"GitHub has the ",[20,21,23],"a",{"href":22},"https:\u002F\u002Fgithub.com\u002Fdamieng\u002FDamienGKit\u002Fblob\u002Fmaster\u002FCSharp\u002FDamienG.Library\u002FSecurity\u002FCryptography\u002FCrc32.cs","latest version of Crc32",[10,25,26,27,31],{},"Because unsigned ints aren’t CLS compliant it won’t play well with VB.NET and implementing the ",[28,29,30],"code",{},"HashAlgorithm"," might lead people to believe it’s suitable for signing, but it isn’t. CRC-32’s are only any good for check-sums along the lines of WinZIP, RAR etc. and certainly shouldn’t come near a password and instead consider SHA-512 or similar.",[10,33,34],{},"As well as using it as a HashAlgorithm with block processing you can also access the static method Compute although there is an overhead with every call building the table with that. If neither option suits you needs cut ‘n splice it to something that does.",[14,36,37],{},[10,38,39,40,43,44,48],{},"If using the ",[28,41,42],{},"Compute"," methods multiple times for the same hash you ",[45,46,47],"em",{},"must"," XOR (~) the current hash when passing it in to the next subsequent call’s seed parameter.",[10,50,51],{},"To compute the hash for a file simply:",[53,54,59],"pre",{"className":55,"code":56,"language":57,"meta":58,"style":58},"language-csharp shiki shiki-themes everforest-light dracula","var crc32 = new Crc32();\nvar hash = String.Empty;\n\nusing (var 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","csharp","",[28,60,61,89,109,116,167,219,224],{"__ignoreMap":58},[62,63,66,70,74,78,82,86],"span",{"class":64,"line":65},"line",1,[62,67,69],{"class":68},"sXAHl","var",[62,71,73],{"class":72},"s6Vpi"," crc32 ",[62,75,77],{"class":76},"s9HRq","=",[62,79,81],{"class":80},"smiwp"," new",[62,83,85],{"class":84},"snuxY"," Crc32",[62,87,88],{"class":72},"();\n",[62,90,92,94,97,99,102,106],{"class":64,"line":91},2,[62,93,69],{"class":68},[62,95,96],{"class":72}," hash ",[62,98,77],{"class":76},[62,100,101],{"class":72}," String.",[62,103,105],{"class":104},"sSKRk","Empty",[62,107,108],{"class":72},";\n",[62,110,112],{"class":64,"line":111},3,[62,113,115],{"emptyLinePlaceholder":114},true,"\n",[62,117,119,122,125,127,130,132,135,139,142,146,150,154,157,159,162,164],{"class":64,"line":118},4,[62,120,121],{"class":80},"using",[62,123,124],{"class":72}," (",[62,126,69],{"class":68},[62,128,129],{"class":72}," fs ",[62,131,77],{"class":76},[62,133,134],{"class":72}," File.",[62,136,138],{"class":137},"sS4Kt","Open",[62,140,141],{"class":72},"(",[62,143,145],{"class":144},"sciFF","\"",[62,147,149],{"class":148},"sJQOs","c:",[62,151,153],{"class":152},"smfUS","\\\\",[62,155,156],{"class":148},"myfile.txt",[62,158,145],{"class":144},[62,160,161],{"class":72},", FileMode.",[62,163,138],{"class":104},[62,165,166],{"class":72},"))\n",[62,168,170,173,175,178,181,184,187,190,193,196,199,202,204,206,209,211,214,217],{"class":64,"line":169},5,[62,171,172],{"class":80},"  foreach",[62,174,124],{"class":72},[62,176,177],{"class":68},"byte",[62,179,180],{"class":72}," b ",[62,182,183],{"class":80},"in",[62,185,186],{"class":72}," crc32.",[62,188,189],{"class":137},"ComputeHash",[62,191,192],{"class":72},"(fs)) hash ",[62,194,195],{"class":76},"+=",[62,197,198],{"class":72}," b.",[62,200,201],{"class":137},"ToString",[62,203,141],{"class":72},[62,205,145],{"class":144},[62,207,208],{"class":148},"x2",[62,210,145],{"class":144},[62,212,213],{"class":72},").",[62,215,216],{"class":137},"ToLower",[62,218,88],{"class":72},[62,220,222],{"class":64,"line":221},6,[62,223,115],{"emptyLinePlaceholder":114},[62,225,227,230,233,235,237,240,242],{"class":64,"line":226},7,[62,228,229],{"class":72},"Console.",[62,231,232],{"class":137},"WriteLine",[62,234,141],{"class":72},[62,236,145],{"class":144},[62,238,239],{"class":148},"CRC-32 is {0}",[62,241,145],{"class":144},[62,243,244],{"class":72},", hash);\n",[10,246,247],{},[45,248,249],{},"[)amien",[251,252,253],"style",{},"html pre.shiki code .sXAHl, html code.shiki .sXAHl{--shiki-default:#3A94C5;--shiki-dark:#FF79C6}html pre.shiki code .s6Vpi, html code.shiki .s6Vpi{--shiki-default:#5C6A72;--shiki-dark:#F8F8F2}html pre.shiki code .s9HRq, html code.shiki .s9HRq{--shiki-default:#F57D26;--shiki-dark:#FF79C6}html pre.shiki code .smiwp, html code.shiki .smiwp{--shiki-default:#F85552;--shiki-dark:#FF79C6}html pre.shiki code .snuxY, html code.shiki .snuxY{--shiki-default:#3A94C5;--shiki-default-font-style:inherit;--shiki-dark:#8BE9FD;--shiki-dark-font-style:italic}html pre.shiki code .sSKRk, html code.shiki .sSKRk{--shiki-default:#35A77C;--shiki-dark:#F8F8F2}html pre.shiki code .sS4Kt, html code.shiki .sS4Kt{--shiki-default:#8DA101;--shiki-dark:#50FA7B}html pre.shiki code .sciFF, html code.shiki .sciFF{--shiki-default:#8DA101;--shiki-dark:#E9F284}html pre.shiki code .sJQOs, html code.shiki .sJQOs{--shiki-default:#8DA101;--shiki-dark:#F1FA8C}html pre.shiki code .smfUS, html code.shiki .smfUS{--shiki-default:#DFA000;--shiki-dark:#FF79C6}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":58,"searchDepth":91,"depth":91,"links":255},[],"Development",16,"2006-08-08T06:49:22+00:00","[object Object]","md",null,false,{},"\u002Fblog\u002F2006\u002Fcalculating_crc32_in_c_and_net",{"title":5,"description":12},"blog\u002F2006\u002Fcalculating_crc32_in_c_and_net",[268,269,270],".NET","C#","hashing","\u002Fblog\u002F2006\u002Fcalculating_crc32_in_c_and_net\u002F",337,"pCR-wPNUncGbM4_yLLlqPbqQw_ETJ21k7mg-A-ngnI4",[275,279,283],{"title":276,"date":277,"url":278},"Transactions in the MongoDB EF Core Provider","2025-10-25","\u002Fblog\u002F2025\u002Fmongodb-explicit-transactions\u002F",{"title":280,"date":281,"url":282},"Queryable Encryption with the MongoDB EF Core Provider","2025-09-22","\u002Fblog\u002F2025\u002Fmongodb-queryable-encryption\u002F",{"title":284,"date":285,"url":286},"Lazy Loading with EF Core Proxies","2025-04-02","\u002Fblog\u002F2025\u002Fef-proxies\u002F",[],1780900533123]