Movatterモバイル変換


[0]ホーム

URL:


D Logo
Menu
Search

Library Reference

version 2.112.0

overview

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 usinga local clone.

std.digest.crc

Cyclic Redundancy Check (32-bit) implementation.
CategoryFunctions
Template APICRC CRC32 CRC64ECMA CRC64ISO 
OOP APICRC32Digest CRC64ECMADigest CRC64ISODigest 
HelperscrcHexString crc32Of crc64ECMAOf crc64ISOOf 
This module conforms to the APIs defined instd.digest. To understand the differences between the template and the OOP API, seestd.digest.
This module publicly importsstd.digest and can be used as a stand-alone module.

NoteCRCs are usually printed with the MSB first. When usingstd.digest.toHexString the result will be in an unexpected order. Usestd.digest.toHexString's optional order parameter to specify decreasing order for the correct result. ThecrcHexString alias can also be used for this purpose.

License:
Boost License 1.0.
Authors:
Pavel "EvilOne" Minayev, Alex Rønne Petersen, Johannes Pfau

ReferencesWikipedia on CRC

Sourcestd/digest/crc.d

Standards:
Implements the 'common' IEEE CRC32 variant (LSB-first order, Initial value uint.max, complement result)

CTFEDigests do not work in CTFE

Examples:
//Template APIimport std.digest.crc;ubyte[4] hash = crc32Of("The quick brown fox jumps over the lazy dog");writeln(crcHexString(hash));// "414FA339"//Feeding dataubyte[1024] data;CRC32crc;crc.put(data[]);crc.start();//Start againcrc.put(data[]);hash =crc.finish();
Examples:
//OOP APIimport std.digest.crc;autocrc =new CRC32Digest();ubyte[] hash =crc.digest("The quick brown fox jumps over the lazy dog");assert(crcHexString(hash) =="414FA339");//352441c2//Feeding dataubyte[1024] data;crc.put(data[]);crc.reset();//Start againcrc.put(data[]);hash =crc.finish();
aliasCRC32 = CRC!(32u, 3988292384LU).CRC;
Template API CRC32 implementation. Seestd.digest for differences between template and OOP API.
aliasCRC64ECMA = CRC!(64u, 14514072000185962306LU).CRC;
Template API CRC64-ECMA implementation. Seestd.digest for differences between template and OOP API.
aliasCRC64ISO = CRC!(64u, 15564440312192434176LU).CRC;
Template API CRC64-ISO implementation. Seestd.digest for differences between template and OOP API.
structCRC(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.
Seestd.digest for differences between template and OOP API.
Examples:
//Simple example, hashing a string using crc32Of helper functionubyte[4] hash32 = crc32Of("abc");//Let's get a hash stringwriteln(crcHexString(hash32));// "352441C2"// Repeat for CRC64ubyte[8] hash64ecma = crc64ECMAOf("abc");writeln(crcHexString(hash64ecma));// "2CD8094A1A277627"ubyte[8] hash64iso = crc64ISOOf("abc");writeln(crcHexString(hash64iso));// "3776C42000000000"
Examples:
ubyte[1024] data;//Using the basic APICRC32 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 CRC64CRC64ECMA 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 voidput(scope const(ubyte)[]data...);
Use this to feed the digest with data. Also implements thestd.range.primitives.isOutputRange interface forubyte andconst(ubyte)[].
pure nothrow @nogc @safe voidstart();
Used to initialize the CRC32 digest.

NoteFor 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 Rfinish();
Returns the finished CRC hash. This also callsstart to reset the internal state.
pure nothrow @nogc @safe Rpeek() const;
Works likefinish 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...)(Tdata);
This is a convenience alias forstd.digest.digest using the CRC32 implementation.
Parameters:
TdataInputRange ofElementType implicitly convertible toubyte,ubyte[] orubyte[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;enumubyte S = 5, F = 66;writeln(iota(S, F).crc32Of);// [59, 140, 234, 154]
ubyte[8]crc64ECMAOf(T...)(Tdata);
This is a convenience alias forstd.digest.digest using the CRC64-ECMA implementation.
Parameters:
TdataInputRange ofElementType implicitly convertible toubyte,ubyte[] orubyte[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;enumubyte S = 5, F = 66;writeln(iota(S, F).crc64ECMAOf);// [6, 184, 91, 238, 46, 213, 127, 188]
ubyte[8]crc64ISOOf(T...)(Tdata);
This is a convenience alias forstd.digest.digest using the CRC64-ISO implementation.
Parameters:
TdataInputRange ofElementType implicitly convertible toubyte,ubyte[] orubyte[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;enumubyte S = 5, F = 66;writeln(iota(S, F).crc64ISOOf);// [21, 185, 116, 95, 219, 11, 54, 7]
aliascrcHexString =crcHexString;

aliascrcHexString = std.digest.toHexString!(Order.decreasing, 16LU, LetterCase.upper).toHexString;
producing the usual CRC32 string output.
aliasCRC32Digest = std.digest.WrapperDigest!(CRC!(32u, 3988292384LU)).WrapperDigest;
OOP API CRC32 implementation. Seestd.digest for differences between template and OOP API.
This is an alias forstd.digest.WrapperDigest!CRC32, see there for more information.
aliasCRC64ECMADigest = std.digest.WrapperDigest!(CRC!(64u, 14514072000185962306LU)).WrapperDigest;
OOP API CRC64-ECMA implementation. Seestd.digest for differences between template and OOP API.
This is an alias forstd.digest.WrapperDigest!CRC64ECMA, see there for more information.
aliasCRC64ISODigest = std.digest.WrapperDigest!(CRC!(64u, 15564440312192434176LU)).WrapperDigest;
OOP API CRC64-ISO implementation. Seestd.digest for differences between template and OOP API.
This is an alias forstd.digest.WrapperDigest!CRC64ISO, see there for more information.
Examples:
//Simple example, hashing a string using CRC32Digest.digest helper functionauto crc =new CRC32Digest();ubyte[] hash = crc.digest("abc");//Let's get a hash stringwriteln(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 exampleauto hash =new CRC32Digest();hash.put(cast(ubyte) 0);ubyte[] result = hash.finish();
Examples:
//using a supplied bufferubyte[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-2026 by theD Language Foundation | Page generated byDdoc on Fri Feb 20 17:59:04 2026

[8]ページ先頭

©2009-2026 Movatter.jp