Crc32.NET icon indicating copy to clipboard operation
Crc32.NET copied to clipboard

Missing functionality for setting the initial value when using instance methods

Open Blenderpics opened this issue 7 years ago • 3 comments

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.

Blenderpics avatar Aug 10 '18 10:08 Blenderpics

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.

force-net avatar Aug 10 '18 11:08 force-net

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?

Blenderpics avatar Aug 10 '18 14:08 Blenderpics

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

force-net avatar Aug 10 '18 14:08 force-net