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 |
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 Examples:
//Template API import std.digest.crc; ubyte[4] hash = crc32Of("The quick brown fox jumps over the lazy dog"); assert(crcHexString(hash) == "414FA339"); //Feeding data ubyte[1024] data; CRC32 crc; crc.put(data[]); crc.start(); //Start again crc.put(data[]); hash = crc.finish();
//OOP API import std.digest.crc; auto crc = new CRC32Digest(); ubyte[] hash = crc.digest("The quick brown fox jumps over the lazy dog"); assert(crcHexString(hash) == "414FA339"); //Feeding data ubyte[1024] data; crc.put(data[]); crc.reset(); //Start again crc.put(data[]); hash = crc.finish();
- 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");
//Using the basic API CRC32 hash; ubyte[1024] data; //Initialize data here... hash.put(data); ubyte[4] result = hash.finish();
//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");
- pure nothrow @trusted void put(scope const(ubyte)[] data...);
- Use this to feed the digest with data.
Also implements the std.range.OutputRange interface for ubyte and
const(ubyte)[].
Examples:
CRC32 dig; dig.put(cast(ubyte)0); //single ubyte dig.put(cast(ubyte)0, cast(ubyte)0); //variadic ubyte[10] buf; dig.put(buf); //buffer
- pure nothrow @trusted void start();
- Used to initialize the CRC32 digest.
Note:
For this CRC32 Digest implementation calling start after default construction is not necessary. Calling start is only necessary to reset the Digest. Generic code which deals with different Digest types should always call start though. Examples:CRC32 digest; //digest.start(); //Not necessary digest.put(0);
- pure nothrow @trusted ubyte[4LU] finish();
- Returns the finished CRC32 hash. This also calls start to
reset the internal state.
Examples:
//Simple example CRC32 hash; hash.put(cast(ubyte)0); ubyte[4] result = hash.finish();
- const pure nothrow @trusted ubyte[4LU] peek();
- Works like finish but does not reset the internal state, so it's possible to continue putting data into this CRC32 after a call to peek.
- auto crc32Of(T...)(T data);
- This is a convenience alias for std.digest.digest.digest using the
CRC32 implementation.
Examples:
ubyte[4] hash = crc32Of("abc"); assert(hash == digest!CRC32("abc")); //This is the same as above
- alias toHexString crcHexString;
alias std.digest.digest.toHexString!(cast(Order)true, 16).toHexString crcHexString; - This is a convenience alias for std.digest.digest.toHexString producing the usual CRC32 string output.
- alias std.digest.digest.WrapperDigest!(CRC32).WrapperDigest CRC32Digest;
- 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");
//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");