View source code
Display the source code in std/digest/crc.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Struct std.digest.crc.CRC

Generic Template API used for CRC32 and CRC64 implementations.

struct CRC(uint N, ulong P)
  
if (N == 32 || N == 64);

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.

Methods

NameDescription
finish () Returns the finished CRC hash. This also calls start to reset the internal state.
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 () Use this to feed the digest with data. Also implements the isOutputRange interface for ubyte and const(ubyte)[].
start () Used to initialize the CRC32 digest.

Aliases

NameDescription
R Type of the finished CRC hash. ubyte[4] if N is 32, ubyte[8] if N is 64.

Example

//Simple example, hashing a string using crc32Of helper function
ubyte[4] hash32 = crc32Of("abc");
//Let's get a hash string
writeln(crcHexString(hash32)); // "352441C2"
// Repeat for CRC64
ubyte[8] hash64ecma = crc64ECMAOf("abc");
writeln(crcHexString(hash64ecma)); // "2CD8094A1A277627"
ubyte[8] hash64iso = crc64ISOOf("abc");
writeln(crcHexString(hash64iso)); // "3776C42000000000"

Example

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();

Example

//Let's use the template features:
//Note: When passing a CRC32 to a function, it must be passed by reference!
void doSomething(T)(ref T hash)
if (isDigest!T)
{
  hash.put(cast(ubyte) 0);
}
CRC32 crc32;
crc32.start();
doSomething(crc32);
writeln(crcHexString(crc32.finish())); // "D202EF8D"
// repeat for CRC64
CRC64ECMA crc64ecma;
crc64ecma.start();
doSomething(crc64ecma);
writeln(crcHexString(crc64ecma.finish())); // "1FADA17364673F59"
CRC64ISO crc64iso;
crc64iso.start();
doSomething(crc64iso);
writeln(crcHexString(crc64iso.finish())); // "6F90000000000000"

Authors

Pavel "EvilOne" Minayev, Alex Rønne Petersen, Johannes Pfau

License

Boost License 1.0.