| 1 |
@safe unittest |
| 2 |
{ |
| 3 |
import std.digest.crc; |
| 4 |
|
| 5 |
//Template API |
| 6 |
import std.digest.crc; |
| 7 |
|
| 8 |
ubyte[4] hash = crc32Of("The quick brown fox jumps over the lazy dog"); |
| 9 |
assert(crcHexString(hash) == "414FA339"); |
| 10 |
|
| 11 |
//Feeding data |
| 12 |
ubyte[1024] data; |
| 13 |
CRC32 crc; |
| 14 |
crc.put(data[]); |
| 15 |
crc.start(); //Start again |
| 16 |
crc.put(data[]); |
| 17 |
hash = crc.finish(); |
| 18 |
} |
| 19 |
|
| 20 |
@safe unittest |
| 21 |
{ |
| 22 |
import std.digest.crc; |
| 23 |
|
| 24 |
//OOP API |
| 25 |
import std.digest.crc; |
| 26 |
|
| 27 |
auto crc = new CRC32Digest(); |
| 28 |
ubyte[] hash = crc.digest("The quick brown fox jumps over the lazy dog"); |
| 29 |
assert(crcHexString(hash) == "414FA339"); //352441c2 |
| 30 |
|
| 31 |
//Feeding data |
| 32 |
ubyte[1024] data; |
| 33 |
crc.put(data[]); |
| 34 |
crc.reset(); //Start again |
| 35 |
crc.put(data[]); |
| 36 |
hash = crc.finish(); |
| 37 |
} |
| 38 |
|
| 39 |
@safe unittest |
| 40 |
{ |
| 41 |
import std.digest.crc; |
| 42 |
|
| 43 |
//Simple example, hashing a string using crc32Of helper function |
| 44 |
ubyte[4] hash32 = crc32Of("abc"); |
| 45 |
//Let's get a hash string |
| 46 |
assert(crcHexString(hash32) == "352441C2"); |
| 47 |
// Repeat for CRC64 |
| 48 |
ubyte[8] hash64ecma = crc64ECMAOf("abc"); |
| 49 |
assert(crcHexString(hash64ecma) == "2CD8094A1A277627"); |
| 50 |
ubyte[8] hash64iso = crc64ISOOf("abc"); |
| 51 |
assert(crcHexString(hash64iso) == "3776C42000000000"); |
| 52 |
} |
| 53 |
|
| 54 |
@safe unittest |
| 55 |
{ |
| 56 |
import std.digest.crc; |
| 57 |
|
| 58 |
ubyte[1024] data; |
| 59 |
//Using the basic API |
| 60 |
CRC32 hash32; |
| 61 |
CRC64ECMA hash64ecma; |
| 62 |
CRC64ISO hash64iso; |
| 63 |
//Initialize data here... |
| 64 |
hash32.put(data); |
| 65 |
ubyte[4] result32 = hash32.finish(); |
| 66 |
hash64ecma.put(data); |
| 67 |
ubyte[8] result64ecma = hash64ecma.finish(); |
| 68 |
hash64iso.put(data); |
| 69 |
ubyte[8] result64iso = hash64iso.finish(); |
| 70 |
} |
| 71 |
|
| 72 |
@safe unittest |
| 73 |
{ |
| 74 |
import std.digest.crc; |
| 75 |
|
| 76 |
//Let's use the template features: |
| 77 |
//Note: When passing a CRC32 to a function, it must be passed by reference! |
| 78 |
void doSomething(T)(ref T hash) |
| 79 |
if (isDigest!T) |
| 80 |
{ |
| 81 |
hash.put(cast(ubyte) 0); |
| 82 |
} |
| 83 |
CRC32 crc32; |
| 84 |
crc32.start(); |
| 85 |
doSomething(crc32); |
| 86 |
assert(crcHexString(crc32.finish()) == "D202EF8D"); |
| 87 |
// repeat for CRC64 |
| 88 |
CRC64ECMA crc64ecma; |
| 89 |
crc64ecma.start(); |
| 90 |
doSomething(crc64ecma); |
| 91 |
assert(crcHexString(crc64ecma.finish()) == "1FADA17364673F59"); |
| 92 |
CRC64ISO crc64iso; |
| 93 |
crc64iso.start(); |
| 94 |
doSomething(crc64iso); |
| 95 |
assert(crcHexString(crc64iso.finish()) == "6F90000000000000"); |
| 96 |
} |
| 97 |
|
| 98 |
@system unittest |
| 99 |
{ |
| 100 |
import std.digest.crc; |
| 101 |
|
| 102 |
ubyte[] data = [4,5,7,25]; |
| 103 |
assert(data.crc32Of == [167, 180, 199, 131]); |
| 104 |
|
| 105 |
import std.utf : byChar; |
| 106 |
assert("hello"d.byChar.crc32Of == [134, 166, 16, 54]); |
| 107 |
|
| 108 |
ubyte[4] hash = "abc".crc32Of(); |
| 109 |
assert(hash == digest!CRC32("ab", "c")); |
| 110 |
|
| 111 |
import std.range : iota; |
| 112 |
enum ubyte S = 5, F = 66; |
| 113 |
assert(iota(S, F).crc32Of == [59, 140, 234, 154]); |
| 114 |
} |
| 115 |
|
| 116 |
@system unittest |
| 117 |
{ |
| 118 |
import std.digest.crc; |
| 119 |
|
| 120 |
ubyte[] data = [4,5,7,25]; |
| 121 |
assert(data.crc64ECMAOf == [58, 142, 220, 214, 118, 98, 105, 69]); |
| 122 |
|
| 123 |
import std.utf : byChar; |
| 124 |
assert("hello"d.byChar.crc64ECMAOf == [177, 55, 185, 219, 229, 218, 30, 155]); |
| 125 |
|
| 126 |
ubyte[8] hash = "abc".crc64ECMAOf(); |
| 127 |
assert("abc".crc64ECMAOf == [39, 118, 39, 26, 74, 9, 216, 44]); |
| 128 |
assert(hash == digest!CRC64ECMA("ab", "c")); |
| 129 |
|
| 130 |
import std.range : iota; |
| 131 |
enum ubyte S = 5, F = 66; |
| 132 |
assert(iota(S, F).crc64ECMAOf == [6, 184, 91, 238, 46, 213, 127, 188]); |
| 133 |
} |
| 134 |
|
| 135 |
@system unittest |
| 136 |
{ |
| 137 |
import std.digest.crc; |
| 138 |
|
| 139 |
ubyte[] data = [4,5,7,25]; |
| 140 |
assert(data.crc64ISOOf == [0, 0, 0, 80, 137, 232, 203, 120]); |
| 141 |
|
| 142 |
import std.utf : byChar; |
| 143 |
assert("hello"d.byChar.crc64ISOOf == [0, 0, 16, 216, 226, 238, 62, 60]); |
| 144 |
|
| 145 |
ubyte[8] hash = "abc".crc64ISOOf(); |
| 146 |
assert("abc".crc64ISOOf == [0, 0, 0, 0, 32, 196, 118, 55]); |
| 147 |
assert(hash == digest!CRC64ISO("ab", "c")); |
| 148 |
|
| 149 |
import std.range : iota; |
| 150 |
enum ubyte S = 5, F = 66; |
| 151 |
|
| 152 |
assert(iota(S, F).crc64ISOOf == [21, 185, 116, 95, 219, 11, 54, 7]); |
| 153 |
} |
| 154 |
|
| 155 |
@safe unittest |
| 156 |
{ |
| 157 |
import std.digest.crc; |
| 158 |
|
| 159 |
//Simple example, hashing a string using CRC32Digest.digest helper function |
| 160 |
auto crc = new CRC32Digest(); |
| 161 |
ubyte[] hash = crc.digest("abc"); |
| 162 |
//Let's get a hash string |
| 163 |
assert(crcHexString(hash) == "352441C2"); |
| 164 |
} |
| 165 |
|
| 166 |
@system unittest |
| 167 |
{ |
| 168 |
import std.digest.crc; |
| 169 |
|
| 170 |
//Let's use the OOP features: |
| 171 |
void test(Digest dig) |
| 172 |
{ |
| 173 |
dig.put(cast(ubyte) 0); |
| 174 |
} |
| 175 |
auto crc = new CRC32Digest(); |
| 176 |
test(crc); |
| 177 |
|
| 178 |
//Let's use a custom buffer: |
| 179 |
ubyte[4] buf; |
| 180 |
ubyte[] result = crc.finish(buf[]); |
| 181 |
assert(crcHexString(result) == "D202EF8D"); |
| 182 |
} |
| 183 |
|
| 184 |
@safe unittest |
| 185 |
{ |
| 186 |
import std.digest.crc; |
| 187 |
|
| 188 |
//Simple example |
| 189 |
auto hash = new CRC32Digest(); |
| 190 |
hash.put(cast(ubyte) 0); |
| 191 |
ubyte[] result = hash.finish(); |
| 192 |
} |
| 193 |
|
| 194 |
@system unittest |
| 195 |
{ |
| 196 |
import std.digest.crc; |
| 197 |
|
| 198 |
//using a supplied buffer |
| 199 |
ubyte[4] buf; |
| 200 |
auto hash = new CRC32Digest(); |
| 201 |
hash.put(cast(ubyte) 0); |
| 202 |
ubyte[] result = hash.finish(buf[]); |
| 203 |
//The result is now in result (and in buf. If you pass a buffer which is bigger than |
| 204 |
//necessary, result will have the correct length, but buf will still have it's original |
| 205 |
//length) |
| 206 |
} |
| 207 |
|