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
- 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");
- 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[4] 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[4] 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.
- alias crcHexString = toHexString;
- This is a convenience alias for std.digest.digest.toHexString producing the usual CRC32 string output.
- alias crcHexString = std.digest.digest.toHexString!(cast(Order)true, 16LU, cast(LetterCase)false).toHexString;
- ditto
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)