std.digest.crc

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.

Note
CRCs are usually printed with the MSB first. When using 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.
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");
writeln(crcHexString(hash)); // "414FA339"

//Feeding data
ubyte[1024] data;
CRC32 crc;
crc.put(data[]);
crc.start(); //Start again
crc.put(data[]);
hash = crc.finish();
Examples:
//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();
alias CRC32 = CRC!(32u, 3988292384LU).CRC;

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

alias CRC64ECMA = CRC!(64u, 14514072000185962306LU).CRC;

Template API CRC64-ECMA implementation. See std.digest for differences between template and OOP API.

alias CRC64ISO = CRC!(64u, 15564440312192434176LU).CRC;

Template API CRC64-ISO implementation. See std.digest for differences between template and OOP API.

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

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 for differences between template and OOP API.

Examples:
//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"
Examples:
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();
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 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"
pure nothrow @nogc @trusted void put(scope const(ubyte)[] data...);

Use this to feed the digest with data. Also implements the std.range.primitives.isOutputRange interface for ubyte and const(ubyte)[].

pure nothrow @nogc @safe 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.
pure nothrow @nogc @safe R finish();

Returns the finished CRC hash. This also calls start to reset the internal state.

const pure nothrow @nogc @safe R 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.

ubyte[4] crc32Of(T...)(T data);

This is a convenience alias for std.digest.digest using the CRC32 implementation.

Parameters:
T data InputRange of ElementType implicitly convertible to ubyte, ubyte[] or ubyte[num] or one or more arrays of any type.
Returns:
CRC32 of data
Examples:
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]
ubyte[8] crc64ECMAOf(T...)(T data);

This is a convenience alias for std.digest.digest using the CRC64-ECMA implementation.

Parameters:
T data InputRange of ElementType implicitly convertible to ubyte, ubyte[] or ubyte[num] or one or more arrays of any type.
Returns:
CRC64-ECMA of data
Examples:
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]
ubyte[8] crc64ISOOf(T...)(T data);

This is a convenience alias for std.digest.digest.digest using the CRC64-ISO implementation.

Parameters:
T data InputRange of ElementType implicitly convertible to ubyte, ubyte[] or ubyte[num] or one or more arrays of any type.
Returns:
CRC64-ISO of data
Examples:
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]
alias crcHexString = crcHexString;

alias crcHexString = std.digest.toHexString!(Order.decreasing, 16LU, LetterCase.upper).toHexString;

producing the usual CRC32 string output.

alias CRC32Digest = std.digest.WrapperDigest!(CRC!(32u, 3988292384LU)).WrapperDigest;

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.

alias CRC64ECMADigest = std.digest.WrapperDigest!(CRC!(64u, 14514072000185962306LU)).WrapperDigest;

OOP API CRC64-ECMA implementation. See std.digest for differences between template and OOP API.

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

alias CRC64ISODigest = std.digest.WrapperDigest!(CRC!(64u, 15564440312192434176LU)).WrapperDigest;

OOP API CRC64-ISO implementation. See std.digest for differences between template and OOP API.

This is an alias for std.digest.digest.WrapperDigest!CRC64ISO, see there 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
writeln(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[]);
writeln(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)

© 1999–2021 The D Language Foundation
Licensed under the Boost License 1.0.
https://dlang.org/phobos/std_digest_crc.html