force-net / Crc32.NET

Fast version of Crc32 algorithm for .NET
MIT License
199 stars 52 forks source link

Missing functionality for setting the initial value when using instance methods #9

Open Blenderpics opened 6 years ago

Blenderpics commented 6 years ago

I need to calculate a crc32 checksum with LSB bit-ordering and a start value of 0xFFFFFFFF. This seems to be impossible with this library.

By using the public static uint Append(uint initial, byte[] input, int offset, int length) function, you can specify a start value (by using the uint initial argument to do so). However it is not possible to specify the bit-ordering when using the static functions.

It is possible to specify the bit-ordering by instantiating the public class Crc32Algorithm : HashAlgorithm via the constructor argument bool isBigEndian. However, using an instance it is not possible to set a start value since public abstract class HashAlgorithm : IDisposable, ICryptoTransform has no functionality for it.

force-net commented 6 years ago

Main usage of instance methods is to use as specific implementation of HashAlgorithm class. Ordering is only used in HashFinal method, when byte array is needed. If you use crc32 as 'integer' value, there is no difference in bit ordering. To convert to byte array manually, you can use BitConverter.GetBytes or use such code:

public static byte[] ToBigEndian(int v)
{
    return new[] { (byte)(v >> 24), (byte)(v >> 16), (byte)(v >> 8), (byte)v };
}

Anyway, I'll think about adding initial value for instance methods.

Blenderpics commented 6 years ago

Whoops, i thought the bool isBigEndian argument was used to indicate the bit-order of the input data (the old Endian vs. LSB/MSB confusion). that certainly explains why i could not reproduce any checksums i generated with this lib with other tools.

I will swap the bit-order of my input data manually then. Adding support for an initial value for the instance methods would be quite nice, since there are quite a few usecases where a start value of 0xFFFFFFFF is used.

should i close this issue or do you want to use it to keep track of the potential improvement regarding the initial value for instance methods?

force-net commented 6 years ago

Let task will be opened until ability to set initial value will be implemented.