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. Page wiki View or edit the community-maintained wiki page associated with this page.

std.digest.crc

Category Functions
Template API CRC32 
OOP API CRC32Digest 
Helpers crcHexString  crc32Of 

Cyclic Redundancy Check (32-bit) implementation.

This module conforms to the APIs defined in std.digest.digest. To understand the differences between the template and the OOP API, see std.digest.digest.

This module publicly imports std.digest.digest and can be used as a stand-alone module.

Note:
CRCs are usually printed with the MSB first. When using std.digest.digest.toHexString the result will be in an unexpected order. Use std.digest.digest.toHexStrings optional order parameter to specify decreasing order for the correct result. The crcHexString alias can also be used for this purpose.

License:
Boost License 1.0

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

References:
Wikipedia on CRC

Source:
std/digest/crc.d

Standards:
Implements the 'common' IEEE CRC32 variant (LSB-first order, Initial value uint.max, complement result)

CTFE:
Digests do not work in CTFE

struct CRC32;
Template API CRC32 implementation. See std.digest.digest for differences between template and OOP API.

Examples:
//Simple example, hashing a string using crc32Of helper function
ubyte[4] hash = crc32Of("abc");
//Let's get a hash string
assert(crcHexString(hash) == "352441C2");

Examples:
//Using the basic API
CRC32 hash;
ubyte[1024] data;
//Initialize data here...
hash.put(data);
ubyte[4] result = hash.finish();

Examples:
//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 crc;
crc.start();
doSomething(crc);
assert(crcHexString(crc.finish()) == "D202EF8D");

auto crc32Of(T...)(T data);
This is a convenience alias for std.digest.digest.digest using the CRC32 implementation.

alias crcHexString = toHexString;
alias crcHexString = std.digest.digest.toHexString!(cast(Order)true, 16).toHexString;
This is a convenience alias for std.digest.digest.toHexString producing the usual CRC32 string output.

Examples:
ubyte[4] hash = crc32Of("abc");
assert(hash == digest!CRC32("abc")); //This is the same as above

alias CRC32Digest = std.digest.digest.WrapperDigest!(CRC32).WrapperDigest;
OOP API CRC32 implementation. See std.digest.digest for differences between template and OOP API.

This is an alias for std.digest.digest.WrapperDigest!CRC32, see std.digest.digest.WrapperDigest for more information.

Examples:
//Simple example, hashing a string using Digest.digest helper function
auto crc = new CRC32Digest();
ubyte[] hash = crc.digest("abc");
//Let's get a hash string
assert(crcHexString(hash) == "352441C2");

Examples:
//Let's use the OOP features:
void test(Digest dig)
{
 dig.put(cast(ubyte)0);
}
auto crc = new CRC32Digest();
test(crc);

//Let's use a custom buffer:
ubyte[4] buf;
ubyte[] result = crc.finish(buf[]);
assert(crcHexString(result) == "D202EF8D");

Examples:
//Simple example
auto hash = new CRC32Digest();
hash.put(cast(ubyte)0);
ubyte[] result = hash.finish();

Examples:
//using a supplied buffer
ubyte[4] buf;
auto hash = new CRC32Digest();
hash.put(cast(ubyte)0);
ubyte[] result = hash.finish(buf[]);
//The result is now in result (and in buf. If you pass a buffer which is bigger than
//necessary, result will have the correct length, but buf will still have it's original
//length)