Cyclic Redundancy Check (32-bit) implementation.
Category | Functions |
---|---|
Template API | CRC CRC32 CRC64ECMA CRC64ISO |
OOP API | CRC32Digest CRC64ECMADigest CRC64ISODigest |
Helpers | crcHexString crc32Of crc64ECMAOf crc64ISOOf |
This module conforms to the APIs defined in std.digest
. To understand the differences between the template and the OOP API, see std.digest
.
This module publicly imports std.digest
and can be used as a stand-alone module.
std.digest.toHexString
the result will be in an unexpected order. Use std.digest.toHexString
's optional order parameter to specify decreasing order for the correct result. The crcHexString
alias can also be used for this purpose. //Template API import std.digest.crc; ubyte[4] hash = crc32Of("The quick brown fox jumps over the lazy dog"); writeln(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"); //352441c2 //Feeding data ubyte[1024] data; crc.put(data[]); crc.reset(); //Start again crc.put(data[]); hash = crc.finish();
Template API CRC32 implementation. See std.digest
for differences between template and OOP API.
Template API CRC64-ECMA implementation. See std.digest.digest
for differences between template and OOP API.
Template API CRC64-ISO implementation. See std.digest.digest
for differences between template and OOP API.
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.digest
for differences between template and OOP API.
//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"
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();
//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"
Use this to feed the digest with data. Also implements the std.range.primitives.isOutputRange
interface for ubyte
and const(ubyte)[]
.
Used to initialize the CRC32 digest.
Returns the finished CRC hash. This also calls start
to reset the internal state.
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.
This is a convenience alias for std.digest.digest
using the CRC32 implementation.
T data
|
InputRange of ElementType implicitly convertible to ubyte , ubyte[] or ubyte[num] or one or more arrays of any type. |
ubyte[] data = [4,5,7,25]; writeln(data.crc32Of); // [167, 180, 199, 131] import std.utf : byChar; writeln("hello"d.byChar.crc32Of); // [134, 166, 16, 54] ubyte[4] hash = "abc".crc32Of(); writeln(hash); // digest!CRC32("ab", "c") import std.range : iota; enum ubyte S = 5, F = 66; writeln(iota(S, F).crc32Of); // [59, 140, 234, 154]
This is a convenience alias for std.digest.digest
using the CRC64-ECMA implementation.
T data
|
InputRange of ElementType implicitly convertible to ubyte , ubyte[] or ubyte[num] or one or more arrays of any type. |
ubyte[] data = [4,5,7,25]; writeln(data.crc64ECMAOf); // [58, 142, 220, 214, 118, 98, 105, 69] import std.utf : byChar; writeln("hello"d.byChar.crc64ECMAOf); // [177, 55, 185, 219, 229, 218, 30, 155] ubyte[8] hash = "abc".crc64ECMAOf(); writeln("abc".crc64ECMAOf); // [39, 118, 39, 26, 74, 9, 216, 44] writeln(hash); // digest!CRC64ECMA("ab", "c") import std.range : iota; enum ubyte S = 5, F = 66; writeln(iota(S, F).crc64ECMAOf); // [6, 184, 91, 238, 46, 213, 127, 188]
This is a convenience alias for std.digest.digest.digest
using the CRC64-ISO implementation.
T data
|
InputRange of ElementType implicitly convertible to ubyte , ubyte[] or ubyte[num] or one or more arrays of any type. |
ubyte[] data = [4,5,7,25]; writeln(data.crc64ISOOf); // [0, 0, 0, 80, 137, 232, 203, 120] import std.utf : byChar; writeln("hello"d.byChar.crc64ISOOf); // [0, 0, 16, 216, 226, 238, 62, 60] ubyte[8] hash = "abc".crc64ISOOf(); writeln("abc".crc64ISOOf); // [0, 0, 0, 0, 32, 196, 118, 55] writeln(hash); // digest!CRC64ISO("ab", "c") import std.range : iota; enum ubyte S = 5, F = 66; writeln(iota(S, F).crc64ISOOf); // [21, 185, 116, 95, 219, 11, 54, 7]
producing the usual CRC32 string output.
OOP API CRC32 implementation. See std.digest
for differences between template and OOP API.
This is an alias for std.digest.WrapperDigest!CRC32
, see there for more information.
OOP API CRC64-ECMA implementation. See std.digest.digest
for differences between template and OOP API.
This is an alias for std.digest.digest.WrapperDigest!CRC64ECMA
, see there for more information.
OOP API CRC64-ISO implementation. See std.digest.digest
for differences between template and OOP API.
This is an alias for std.digest.digest.WrapperDigest!CRC64ISO
, see there for more information.
//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 writeln(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[]); writeln(crcHexString(result)); // "D202EF8D"
//Simple example auto hash = new CRC32Digest(); hash.put(cast(ubyte) 0); ubyte[] result = hash.finish();
//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)
© 1999–2019 The D Language Foundation
Licensed under the Boost License 1.0.
https://dlang.org/phobos/std_digest_crc.html