CRC

Generic Template API used for CRC32 and CRC64 implementations.

The N parameter indicate the size of the hash in bits. The parameter P specify the polynomial to be used for reduction.

You may want to use the CRC32, CRC65ECMA and CRC64ISO aliases for convenience.

See std.digest for differences between template and OOP API.

struct CRC (
uint N
ulong P
) if (
N == 32 ||
N == 64
) {}

Members

Functions

finish
R finish()

Returns the finished CRC hash. This also calls start to reset the internal state.

peek
R peek()

Works like finish but does not reset the internal state, so it's possible to continue putting data into this CRC after a call to peek.

put
void put(const(ubyte)[] data)

Use this to feed the digest with data. Also implements the std.range.primitives.isOutputRange interface for ubyte and const(ubyte)[].

start
void start()

Used to initialize the CRC32 digest.

Examples

//Simple example, hashing a string using crc32Of helper function
ubyte[4] hash32 = crc32Of("abc");
//Let's get a hash string
assert(crcHexString(hash32) == "352441C2");
// Repeat for CRC64
ubyte[8] hash64ecma = crc64ECMAOf("abc");
assert(crcHexString(hash64ecma) == "2CD8094A1A277627");
ubyte[8] hash64iso = crc64ISOOf("abc");
assert(crcHexString(hash64iso) == "3776C42000000000");
ubyte[1024] data;
//Using the basic API
CRC32 hash32;
CRC64ECMA hash64ecma;
CRC64ISO hash64iso;
//Initialize data here...
hash32.put(data);
ubyte[4] result32 = hash32.finish();
hash64ecma.put(data);
ubyte[8] result64ecma = hash64ecma.finish();
hash64iso.put(data);
ubyte[8] result64iso = hash64iso.finish();
1 //Let's use the template features:
2 //Note: When passing a CRC32 to a function, it must be passed by reference!
3 void doSomething(T)(ref T hash)
4 if (isDigest!T)
5 {
6   hash.put(cast(ubyte) 0);
7 }
8 CRC32 crc32;
9 crc32.start();
10 doSomething(crc32);
11 assert(crcHexString(crc32.finish()) == "D202EF8D");
12 // repeat for CRC64
13 CRC64ECMA crc64ecma;
14 crc64ecma.start();
15 doSomething(crc64ecma);
16 assert(crcHexString(crc64ecma.finish()) == "1FADA17364673F59");
17 CRC64ISO crc64iso;
18 crc64iso.start();
19 doSomething(crc64iso);
20 assert(crcHexString(crc64iso.finish()) == "6F90000000000000");

Meta

Suggestion Box / Bug Report