| 1 |
@system unittest |
| 2 |
{ |
| 3 |
import std.digest; |
| 4 |
|
| 5 |
import std.digest.crc; |
| 6 |
|
| 7 |
//Simple example |
| 8 |
char[8] hexHash = hexDigest!CRC32("The quick brown fox jumps over the lazy dog"); |
| 9 |
assert(hexHash == "39A34F41"); |
| 10 |
|
| 11 |
//Simple example, using the API manually |
| 12 |
CRC32 context = makeDigest!CRC32(); |
| 13 |
context.put(cast(ubyte[])"The quick brown fox jumps over the lazy dog"); |
| 14 |
ubyte[4] hash = context.finish(); |
| 15 |
assert(toHexString(hash) == "39A34F41"); |
| 16 |
} |
| 17 |
|
| 18 |
@system unittest |
| 19 |
{ |
| 20 |
import std.digest; |
| 21 |
|
| 22 |
//Generating the hashes of a file, idiomatic D way |
| 23 |
import std.digest.crc, std.digest.md, std.digest.sha; |
| 24 |
import std.stdio; |
| 25 |
|
| 26 |
// Digests a file and prints the result. |
| 27 |
void digestFile(Hash)(string filename) |
| 28 |
if (isDigest!Hash) |
| 29 |
{ |
| 30 |
auto file = File(filename); |
| 31 |
auto result = digest!Hash(file.byChunk(4096 * 1024)); |
| 32 |
writefln("%s (%s) = %s", Hash.stringof, filename, toHexString(result)); |
| 33 |
} |
| 34 |
|
| 35 |
void main(string[] args) |
| 36 |
{ |
| 37 |
foreach (name; args[1 .. $]) |
| 38 |
{ |
| 39 |
digestFile!MD5(name); |
| 40 |
digestFile!SHA1(name); |
| 41 |
digestFile!CRC32(name); |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
@system unittest |
| 47 |
{ |
| 48 |
import std.digest; |
| 49 |
|
| 50 |
//Generating the hashes of a file using the template API |
| 51 |
import std.digest.crc, std.digest.md, std.digest.sha; |
| 52 |
import std.stdio; |
| 53 |
// Digests a file and prints the result. |
| 54 |
void digestFile(Hash)(ref Hash hash, string filename) |
| 55 |
if (isDigest!Hash) |
| 56 |
{ |
| 57 |
File file = File(filename); |
| 58 |
|
| 59 |
//As digests imlement OutputRange, we could use std.algorithm.copy |
| 60 |
//Let's do it manually for now |
| 61 |
foreach (buffer; file.byChunk(4096 * 1024)) |
| 62 |
hash.put(buffer); |
| 63 |
|
| 64 |
auto result = hash.finish(); |
| 65 |
writefln("%s (%s) = %s", Hash.stringof, filename, toHexString(result)); |
| 66 |
} |
| 67 |
|
| 68 |
void uMain(string[] args) |
| 69 |
{ |
| 70 |
MD5 md5; |
| 71 |
SHA1 sha1; |
| 72 |
CRC32 crc32; |
| 73 |
|
| 74 |
md5.start(); |
| 75 |
sha1.start(); |
| 76 |
crc32.start(); |
| 77 |
|
| 78 |
foreach (arg; args[1 .. $]) |
| 79 |
{ |
| 80 |
digestFile(md5, arg); |
| 81 |
digestFile(sha1, arg); |
| 82 |
digestFile(crc32, arg); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
@system unittest |
| 88 |
{ |
| 89 |
import std.digest; |
| 90 |
|
| 91 |
import std.digest.crc, std.digest.md, std.digest.sha; |
| 92 |
import std.stdio; |
| 93 |
|
| 94 |
// Digests a file and prints the result. |
| 95 |
void digestFile(Digest hash, string filename) |
| 96 |
{ |
| 97 |
File file = File(filename); |
| 98 |
|
| 99 |
//As digests implement OutputRange, we could use std.algorithm.copy |
| 100 |
//Let's do it manually for now |
| 101 |
foreach (buffer; file.byChunk(4096 * 1024)) |
| 102 |
hash.put(buffer); |
| 103 |
|
| 104 |
ubyte[] result = hash.finish(); |
| 105 |
writefln("%s (%s) = %s", typeid(hash).toString(), filename, toHexString(result)); |
| 106 |
} |
| 107 |
|
| 108 |
void umain(string[] args) |
| 109 |
{ |
| 110 |
auto md5 = new MD5Digest(); |
| 111 |
auto sha1 = new SHA1Digest(); |
| 112 |
auto crc32 = new CRC32Digest(); |
| 113 |
|
| 114 |
foreach (arg; args[1 .. $]) |
| 115 |
{ |
| 116 |
digestFile(md5, arg); |
| 117 |
digestFile(sha1, arg); |
| 118 |
digestFile(crc32, arg); |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
@system unittest |
| 124 |
{ |
| 125 |
import std.digest; |
| 126 |
|
| 127 |
//Using the OutputRange feature |
| 128 |
import std.algorithm.mutation : copy; |
| 129 |
import std.digest.md; |
| 130 |
import std.range : repeat; |
| 131 |
|
| 132 |
auto oneMillionRange = repeat!ubyte(cast(ubyte)'a', 1000000); |
| 133 |
auto ctx = makeDigest!MD5(); |
| 134 |
copy(oneMillionRange, &ctx); //Note: You must pass a pointer to copy! |
| 135 |
assert(ctx.finish().toHexString() == "7707D6AE4E027C70EEA2A935C2296F21"); |
| 136 |
} |
| 137 |
|
| 138 |
@system unittest |
| 139 |
{ |
| 140 |
import std.digest; |
| 141 |
|
| 142 |
import std.digest.crc; |
| 143 |
static assert(isDigest!CRC32); |
| 144 |
} |
| 145 |
|
| 146 |
@system unittest |
| 147 |
{ |
| 148 |
import std.digest; |
| 149 |
|
| 150 |
import std.digest.crc; |
| 151 |
void myFunction(T)() |
| 152 |
if (isDigest!T) |
| 153 |
{ |
| 154 |
T dig; |
| 155 |
dig.start(); |
| 156 |
auto result = dig.finish(); |
| 157 |
} |
| 158 |
myFunction!CRC32(); |
| 159 |
} |
| 160 |
|
| 161 |
@system unittest |
| 162 |
{ |
| 163 |
import std.digest; |
| 164 |
|
| 165 |
import std.digest.crc; |
| 166 |
assert(is(DigestType!(CRC32) == ubyte[4])); |
| 167 |
} |
| 168 |
|
| 169 |
@system unittest |
| 170 |
{ |
| 171 |
import std.digest; |
| 172 |
|
| 173 |
import std.digest.crc; |
| 174 |
CRC32 dig; |
| 175 |
dig.start(); |
| 176 |
DigestType!CRC32 result = dig.finish(); |
| 177 |
} |
| 178 |
|
| 179 |
@system unittest |
| 180 |
{ |
| 181 |
import std.digest; |
| 182 |
|
| 183 |
import std.digest.crc, std.digest.md; |
| 184 |
assert(!hasPeek!(MD5)); |
| 185 |
assert(hasPeek!CRC32); |
| 186 |
} |
| 187 |
|
| 188 |
@system unittest |
| 189 |
{ |
| 190 |
import std.digest; |
| 191 |
|
| 192 |
import std.digest.crc; |
| 193 |
void myFunction(T)() |
| 194 |
if (hasPeek!T) |
| 195 |
{ |
| 196 |
T dig; |
| 197 |
dig.start(); |
| 198 |
auto result = dig.peek(); |
| 199 |
} |
| 200 |
myFunction!CRC32(); |
| 201 |
} |
| 202 |
|
| 203 |
@system unittest |
| 204 |
{ |
| 205 |
import std.digest; |
| 206 |
|
| 207 |
import std.digest.hmac, std.digest.md; |
| 208 |
static assert(hasBlockSize!MD5 && MD5.blockSize == 512); |
| 209 |
static assert(hasBlockSize!(HMAC!MD5) && HMAC!MD5.blockSize == 512); |
| 210 |
} |
| 211 |
|
| 212 |
@system unittest |
| 213 |
{ |
| 214 |
import std.digest; |
| 215 |
|
| 216 |
import std.digest.md; |
| 217 |
import std.range : repeat; |
| 218 |
auto testRange = repeat!ubyte(cast(ubyte)'a', 100); |
| 219 |
auto md5 = digest!MD5(testRange); |
| 220 |
} |
| 221 |
|
| 222 |
@system unittest |
| 223 |
{ |
| 224 |
import std.digest; |
| 225 |
|
| 226 |
import std.digest.crc, std.digest.md, std.digest.sha; |
| 227 |
auto md5 = digest!MD5( "The quick brown fox jumps over the lazy dog"); |
| 228 |
auto sha1 = digest!SHA1( "The quick brown fox jumps over the lazy dog"); |
| 229 |
auto crc32 = digest!CRC32("The quick brown fox jumps over the lazy dog"); |
| 230 |
assert(toHexString(crc32) == "39A34F41"); |
| 231 |
} |
| 232 |
|
| 233 |
@system unittest |
| 234 |
{ |
| 235 |
import std.digest; |
| 236 |
|
| 237 |
import std.digest.crc; |
| 238 |
auto crc32 = digest!CRC32("The quick ", "brown ", "fox jumps over the lazy dog"); |
| 239 |
assert(toHexString(crc32) == "39A34F41"); |
| 240 |
} |
| 241 |
|
| 242 |
@system unittest |
| 243 |
{ |
| 244 |
import std.digest; |
| 245 |
|
| 246 |
import std.digest.md; |
| 247 |
import std.range : repeat; |
| 248 |
auto testRange = repeat!ubyte(cast(ubyte)'a', 100); |
| 249 |
assert(hexDigest!MD5(testRange) == "36A92CC94A9E0FA21F625F8BFB007ADF"); |
| 250 |
} |
| 251 |
|
| 252 |
@system unittest |
| 253 |
{ |
| 254 |
import std.digest; |
| 255 |
|
| 256 |
import std.digest.crc; |
| 257 |
assert(hexDigest!(CRC32, Order.decreasing)("The quick brown fox jumps over the lazy dog") == "414FA339"); |
| 258 |
} |
| 259 |
|
| 260 |
@system unittest |
| 261 |
{ |
| 262 |
import std.digest; |
| 263 |
|
| 264 |
import std.digest.crc; |
| 265 |
assert(hexDigest!(CRC32, Order.decreasing)("The quick ", "brown ", "fox jumps over the lazy dog") == "414FA339"); |
| 266 |
} |
| 267 |
|
| 268 |
@system unittest |
| 269 |
{ |
| 270 |
import std.digest; |
| 271 |
|
| 272 |
import std.digest.md; |
| 273 |
auto md5 = makeDigest!MD5(); |
| 274 |
md5.put(0); |
| 275 |
assert(toHexString(md5.finish()) == "93B885ADFE0DA089CDF634904FD59F71"); |
| 276 |
} |
| 277 |
|
| 278 |
@system unittest |
| 279 |
{ |
| 280 |
import std.digest; |
| 281 |
|
| 282 |
//Using the OutputRange feature |
| 283 |
import std.algorithm.mutation : copy; |
| 284 |
import std.digest.md; |
| 285 |
import std.range : repeat; |
| 286 |
|
| 287 |
auto oneMillionRange = repeat!ubyte(cast(ubyte)'a', 1000000); |
| 288 |
auto ctx = new MD5Digest(); |
| 289 |
copy(oneMillionRange, ctx); |
| 290 |
assert(ctx.finish().toHexString() == "7707D6AE4E027C70EEA2A935C2296F21"); |
| 291 |
} |
| 292 |
|
| 293 |
@system unittest |
| 294 |
{ |
| 295 |
import std.digest; |
| 296 |
|
| 297 |
import std.digest.crc, std.digest.md, std.digest.sha; |
| 298 |
ubyte[] md5 = (new MD5Digest()).digest("The quick brown fox jumps over the lazy dog"); |
| 299 |
ubyte[] sha1 = (new SHA1Digest()).digest("The quick brown fox jumps over the lazy dog"); |
| 300 |
ubyte[] crc32 = (new CRC32Digest()).digest("The quick brown fox jumps over the lazy dog"); |
| 301 |
assert(crcHexString(crc32) == "414FA339"); |
| 302 |
} |
| 303 |
|
| 304 |
@system unittest |
| 305 |
{ |
| 306 |
import std.digest; |
| 307 |
|
| 308 |
import std.digest.crc; |
| 309 |
ubyte[] crc32 = (new CRC32Digest()).digest("The quick ", "brown ", "fox jumps over the lazy dog"); |
| 310 |
assert(crcHexString(crc32) == "414FA339"); |
| 311 |
} |
| 312 |
|
| 313 |
@system unittest |
| 314 |
{ |
| 315 |
import std.digest; |
| 316 |
|
| 317 |
void test(Digest dig) |
| 318 |
{ |
| 319 |
dig.put(cast(ubyte) 0); //single ubyte |
| 320 |
dig.put(cast(ubyte) 0, cast(ubyte) 0); //variadic |
| 321 |
ubyte[10] buf; |
| 322 |
dig.put(buf); //buffer |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
@safe unittest |
| 327 |
{ |
| 328 |
import std.digest; |
| 329 |
|
| 330 |
import std.digest.crc : CRC32; |
| 331 |
|
| 332 |
auto crc32 = digest!CRC32("The quick ", "brown ", "fox jumps over the lazy dog"); |
| 333 |
assert(crc32.toHexString!(Order.decreasing) == "414FA339"); |
| 334 |
assert(crc32.toHexString!(LetterCase.lower, Order.decreasing) == "414fa339"); |
| 335 |
} |
| 336 |
|
| 337 |
@safe unittest |
| 338 |
{ |
| 339 |
import std.digest; |
| 340 |
|
| 341 |
import std.digest.crc; |
| 342 |
//Test with template API: |
| 343 |
auto crc32 = digest!CRC32("The quick ", "brown ", "fox jumps over the lazy dog"); |
| 344 |
//Lower case variant: |
| 345 |
assert(toHexString!(LetterCase.lower)(crc32) == "39a34f41"); |
| 346 |
//Usually CRCs are printed in this order, though: |
| 347 |
assert(toHexString!(Order.decreasing)(crc32) == "414FA339"); |
| 348 |
assert(toHexString!(LetterCase.lower, Order.decreasing)(crc32) == "414fa339"); |
| 349 |
} |
| 350 |
|
| 351 |
@safe unittest |
| 352 |
{ |
| 353 |
import std.digest; |
| 354 |
|
| 355 |
import std.digest.crc; |
| 356 |
// With OOP API |
| 357 |
auto crc32 = (new CRC32Digest()).digest("The quick ", "brown ", "fox jumps over the lazy dog"); |
| 358 |
//Usually CRCs are printed in this order, though: |
| 359 |
assert(toHexString!(Order.decreasing)(crc32) == "414FA339"); |
| 360 |
} |
| 361 |
|
| 362 |
@system unittest |
| 363 |
{ |
| 364 |
import std.digest; |
| 365 |
|
| 366 |
import std.digest.md; |
| 367 |
//Simple example |
| 368 |
auto hash = new WrapperDigest!MD5(); |
| 369 |
hash.put(cast(ubyte) 0); |
| 370 |
auto result = hash.finish(); |
| 371 |
} |
| 372 |
|
| 373 |
@system unittest |
| 374 |
{ |
| 375 |
import std.digest; |
| 376 |
|
| 377 |
//using a supplied buffer |
| 378 |
import std.digest.md; |
| 379 |
ubyte[16] buf; |
| 380 |
auto hash = new WrapperDigest!MD5(); |
| 381 |
hash.put(cast(ubyte) 0); |
| 382 |
auto result = hash.finish(buf[]); |
| 383 |
//The result is now in result (and in buf). If you pass a buffer which is bigger than |
| 384 |
//necessary, result will have the correct length, but buf will still have it's original |
| 385 |
//length |
| 386 |
} |
| 387 |
|
| 388 |
@system pure unittest |
| 389 |
{ |
| 390 |
import std.digest; |
| 391 |
|
| 392 |
import std.digest.hmac : hmac; |
| 393 |
import std.digest.sha : SHA1; |
| 394 |
import std.string : representation; |
| 395 |
|
| 396 |
// a typical HMAC data integrity verification |
| 397 |
auto secret = "A7GZIP6TAQA6OHM7KZ42KB9303CEY0MOV5DD6NTV".representation; |
| 398 |
auto data = "data".representation; |
| 399 |
|
| 400 |
auto hex1 = data.hmac!SHA1(secret).toHexString; |
| 401 |
auto hex2 = data.hmac!SHA1(secret).toHexString; |
| 402 |
auto hex3 = "data1".representation.hmac!SHA1(secret).toHexString; |
| 403 |
|
| 404 |
assert( secureEqual(hex1[], hex2[])); |
| 405 |
assert(!secureEqual(hex1[], hex3[])); |
| 406 |
} |
| 407 |
|
| 408 |
@safe unittest |
| 409 |
{ |
| 410 |
import std.digest; |
| 411 |
|
| 412 |
assert(isHexString("0x0123456789ABCDEFabcdef")); |
| 413 |
assert(isHexString("0123456789ABCDEFabcdef")); |
| 414 |
assert(!isHexString("g")); |
| 415 |
assert(!isHexString("#")); |
| 416 |
} |
| 417 |
|
| 418 |
@safe unittest |
| 419 |
{ |
| 420 |
import std.digest; |
| 421 |
|
| 422 |
import std.range.primitives : ElementType, isForwardRange; |
| 423 |
import std.traits : ReturnType; |
| 424 |
|
| 425 |
// The decoder implements a forward range. |
| 426 |
static assert(isForwardRange!(ReturnType!(fromHexStringAsRange!string))); |
| 427 |
static assert(isForwardRange!(ReturnType!(fromHexStringAsRange!wstring))); |
| 428 |
static assert(isForwardRange!(ReturnType!(fromHexStringAsRange!dstring))); |
| 429 |
|
| 430 |
// The element type of the range is always `ubyte`. |
| 431 |
static assert( |
| 432 |
is(ElementType!(ReturnType!(fromHexStringAsRange!string)) == ubyte) |
| 433 |
); |
| 434 |
static assert( |
| 435 |
is(ElementType!(ReturnType!(fromHexStringAsRange!wstring)) == ubyte) |
| 436 |
); |
| 437 |
static assert( |
| 438 |
is(ElementType!(ReturnType!(fromHexStringAsRange!dstring)) == ubyte) |
| 439 |
); |
| 440 |
} |
| 441 |
|
| 442 |
@safe unittest |
| 443 |
{ |
| 444 |
import std.digest; |
| 445 |
|
| 446 |
// Single byte |
| 447 |
assert("0xff".fromHexString == [255]); |
| 448 |
assert("0xff"w.fromHexString == [255]); |
| 449 |
assert("0xff"d.fromHexString == [255]); |
| 450 |
assert("0xC0".fromHexString == [192]); |
| 451 |
assert("0x00".fromHexString == [0]); |
| 452 |
|
| 453 |
// Nothing |
| 454 |
assert("".fromHexString == []); |
| 455 |
assert(""w.fromHexString == []); |
| 456 |
assert(""d.fromHexString == []); |
| 457 |
|
| 458 |
// Nothing but a prefix |
| 459 |
assert("0x".fromHexString == []); |
| 460 |
assert("0x"w.fromHexString == []); |
| 461 |
assert("0x"d.fromHexString == []); |
| 462 |
|
| 463 |
// Half a byte |
| 464 |
assert("0x1".fromHexString == [0x01]); |
| 465 |
assert("0x1"w.fromHexString == [0x01]); |
| 466 |
assert("0x1"d.fromHexString == [0x01]); |
| 467 |
|
| 468 |
// Mixed case is fine. |
| 469 |
assert("0xAf".fromHexString == [0xAF]); |
| 470 |
assert("0xaF".fromHexString == [0xAF]); |
| 471 |
|
| 472 |
// Multiple bytes |
| 473 |
assert("0xfff".fromHexString == [0x0F, 0xFF]); |
| 474 |
assert("0x123AaAa".fromHexString == [0x01, 0x23, 0xAA, 0xAA]); |
| 475 |
assert("EBBBBF".fromHexString == [0xEB, 0xBB, 0xBF]); |
| 476 |
|
| 477 |
// md5 sum |
| 478 |
assert("d41d8cd98f00b204e9800998ecf8427e".fromHexString == [ |
| 479 |
0xD4, 0x1D, 0x8C, 0xD9, 0x8F, 0x00, 0xB2, 0x04, |
| 480 |
0xE9, 0x80, 0x09, 0x98, 0xEC, 0xF8, 0x42, 0x7E, |
| 481 |
]); |
| 482 |
} |
| 483 |
|
| 484 |
@safe unittest |
| 485 |
{ |
| 486 |
import std.digest; |
| 487 |
|
| 488 |
// Cycle self-test |
| 489 |
const ubyte[] initial = [0x00, 0x12, 0x34, 0xEB]; |
| 490 |
assert(initial == initial.toHexString().fromHexString()); |
| 491 |
} |
| 492 |
|