Menu

- Learn
- Documentation
  - Language Reference
  - Library Reference
  - Feature Overview
  - DMD Manual
  - Articles
- Packages
- Community
  - Bug Tracker
  - Forums
  - IRC
  - D on GitHub
  - Wiki
  - Review Queue
  - Twitter
  - More Links
- Resources
  - NEW Library Reference Preview
  - D-Specific Tools
  - Visual D
  - Editors
  - IDEs
  - Tutorials
  - Books
  - The D Style
  - Glossary
  - Acknowledgments
  - Sitemap

Search
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
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 a local clone.

# std.digest.crc

Cyclic Redundancy Check (32-bit) implementation.

Category | Functions
--- | ---
Template API | CRC32
OOP API | CRC32Digest
Helpers | crcHexString crc32Of

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.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");
    assert(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();

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 @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)[].
    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 @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.
    Examples:

        CRC32 digest;
        //digest.start(); //Not necessary
        digest.put(0);

  pure nothrow @nogc @safe 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 @nogc @safe 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.
ubyte[4] crc32Of(T...)(T data);
  This is a convenience alias for std.digest.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];
      assert(data.crc32Of == [167, 180, 199, 131]);

      import std.utf : byChar;
      assert("hello"d.byChar.crc32Of == [134, 166, 16, 54]);

      ubyte[4] hash = "abc".crc32Of();
      assert(hash == digest!CRC32("ab", "c"));

      import std.range : iota;
      enum ubyte S = 5, F = 66;
      assert(iota(S, F).crc32Of == [59, 140, 234, 154]);

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

Copyright © 1999-2016 by Digital Mars ®, All Rights Reserved | Page generated by Ddoc on Mon Sep 19 09:25:32 2016