Skip to content

[API Proposal]: Add custom polynomial support for Crc32 and Crc64 class #78063

@medo64

Description

@medo64

Background and motivation

There are many variants of CRC32 algorithm that differ only in polynomials used. The current implementation only supports IEEE 802.3 polynomial externally. It would be ideal if class would allow for defining custom polynomial thus supporting other CRC-32 variants (e.g. ISCSI, MPEG-2, etc.)

Looking at source, there is a Generate method in Crc32ReflectedTable so basic plumbing for custom polynomials seems to be there already.

As different polynomials use different initial and output XOR values, that would need to be added.

API Proposal

namespace namespace System.IO.Hashing

public sealed partial class Crc32 : NonCryptographicHashAlgorithm
{
    public Crc32(ReadOnlySpan<byte> reflectedPolynomial);
    public Crc32(ReadOnlySpan<byte> reflectedPolynomial,
                 ReadOnlySpan<byte> initialValue,
                 ReadOnlySpan<byte> xorValue,
                 bool reflectIn,
                 bool reflectOut);
}

API Usage

var crc = new Crc32(new byte[] { 0x8F, 0x6E, 0x37, 0xA0 });
crc.Append(Encoding.ASCII.GetBytes("123456789"));
var result = crc.GetCurrentHash();

Alternative Designs

No response

Risks

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions