invertedtomato / crc

A flexible CRC implementation supporting most major algorithms. Also includes ability to support custom implementations.
MIT License
29 stars 10 forks source link

API should be better aligned with NonCryptographicHashAlgorithm #9

Open maettu-this opened 1 year ago

maettu-this commented 1 year ago

Hi Ben,

CrcAlgorithm already is quite close to NonCryptographicHashAlgorithm, the base for e.g. Crc32 (https://learn.microsoft.com/en-us/dotnet/api/system.io.hashing.crc32), but there are subtle differencies:

Aligning the API would enable deriving from NonCryptographicHashAlgorithm for .NET 6+.

Also note the remark "this stable output is the byte sequence { 0x1C, 0xDF, 0x44, 0x21 }, the Little Endian representation of 0x2144DF1C" which relates to your "TODO" (https://github.com/invertedtomato/crc/blob/master/Library/CrcAlgorithm.cs#L11). I think this "TODO" can be completed by equally documenting the output. A user can still reverse/rearrange the resulting CRC as needed.

Best regards, Matthias

invertedtomato commented 1 year ago

Hi Matthias. Thanks for pointing out NonCryptographicHashAlgorithm. I'm open to renaming symbols to match the pattern set by NonCryptographicHashAlgorithm, but I'm hesitant to make NonCryptographicHashAlgorithm a dependancy now as it raises the minimum supported framework to .NET 6 and adds a dependancy, both seemingly adding minimal value to it's function.

maettu-this commented 1 year ago

Hi Ben, I fully agree, it would just prepare such future step. You might be able to conditionally build for .NET 6+ with deriving from NonCryptographicHashAlgorithm, but keeping at least .NET Standard 2.0 builds is essential for compatibility with .NET Framework. My application actually still targets 4.8.

maettu-this commented 1 year ago

PS in case you missed that in https://github.com/invertedtomato/crc/pull/7:

More considerations for future refinement and extension of the library:

And I have just come across another extensive list of CRCs:

maettu-this commented 1 year ago

Endianness sometimes is a beast, the "TODO" in your code isn't for nothing:

I have created https://dotnetfiddle.net/OYNy6O that uses https://learn.microsoft.com/en-us/dotnet/api/system.io.hashing.crc32. https://crccalc.com/?crc=123456789&method=CRC-32&datatype=ascii&outtype=0 results in CRC of 0xCBF43926. The fiddle results in the same.

Now, crc.GetCurrentHash() "emits the answer in the Little Endian byte order", e.g. the LSB [0] is 0x26. This sequence is the reversed of the result of your CrcAlgorithm.ToByteArray(), which is:

grafik

It took me quite a while to conclude on this. The method's summary "...the CRC of the bytes that..." doesn't help much. While it is OK to have CrcAlgorithm.ToByteArray() reverse to big-endian, I think this must be improved, as other users likely fall into the same pit. Ideas:

invertedtomato commented 1 year ago

@maettu-this , I've split the endian discussion off into another issue to stay sane. Could you please expand on what you mean by these?

I'm glad for your feedback too @maettu-this!

maettu-this commented 1 year ago

8-bit bound algorithms covered by Rust crc_all not yet covered by InvertedTomato.Crc:

https://reveng.sourceforge.io/crc-catalogue/1-15.htm lists them as well.

https://reveng.sourceforge.io/crc-catalogue/16.htm lists them as well. Interestingly, this link also reveals "CRC-A" as "CRC-16/ISO-IEC-14443-3-A".

https://reveng.sourceforge.io/crc-catalogue/17plus.htm lists them as well. Even more:

Maybe better to extract this into a separate issue.

maettu-this commented 1 year ago

Ragarding "mixed-case names" I meant the string assigned to the algorithm's Name property. But maybe this isn't that good of an idea, if all resources stick the CAPITAL names.

invertedtomato commented 1 year ago

Yeah, using all-capital names is a stylistic decision that most seem to follow. I'm not keen to change that.

In regards to the additional CRCs, would you be interested in raising a PR to add these? I'm happy to incorporate them but I'm low on time myself

maettu-this commented 1 year ago

Extracted the CRC coverage to separate #13, which can then track PRs adding CRCs as people need more. Currently I am happy with those covered already, just saw that other resources list additional CRCs that people could find useful here as well.

So this issue is again reduced to the title: API should be better aligned with NonCryptographicHashAlgorithm.