/usr/include/d/std/digest/ripemd.d is in libphobos2-ldc-dev 1:0.17.1-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 | /**
* Computes RIPEMD-160 hashes of arbitrary data. RIPEMD-160 hashes are 20 byte quantities
* that are like a checksum or CRC, but are more robust.
*
$(SCRIPT inhibitQuickIndex = 1;)
$(DIVC quickindex,
$(BOOKTABLE ,
$(TR $(TH Category) $(TH Functions)
)
$(TR $(TDNW Template API) $(TD $(MYREF RIPEMD160)
)
)
$(TR $(TDNW OOP API) $(TD $(MYREF RIPEMD160Digest))
)
$(TR $(TDNW Helpers) $(TD $(MYREF ripemd160Of))
)
)
)
* This module conforms to the APIs defined in $(D std.digest.digest). To understand the
* differences between the template and the OOP API, see $(D std.digest.digest).
*
* This module publicly imports $(D std.digest.digest) and can be used as a stand-alone
* module.
*
* License: $(WEB www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
*
* CTFE:
* Digests do not work in CTFE
*
* Authors:
* Kai Nacke $(BR)
* The algorithm was designed by Hans Dobbertin, Antoon Bosselaers, and Bart Preneel. $(BR)
* The D implementation is a direct translation of the ANSI C implementation by Antoon Bosselaers.
*
* References:
* $(UL
* $(LI $(LINK2 http://homes.esat.kuleuven.be/~bosselae/ripemd160.html, The hash function RIPEMD-160))
* $(LI $(LINK2 http://en.wikipedia.org/wiki/RIPEMD-160, Wikipedia on RIPEMD-160))
* )
*
* Source: $(PHOBOSSRC std/digest/_ripemd.d)
*
* Macros:
* WIKI = Phobos/StdRipemd
*/
module std.digest.ripemd;
public import std.digest.digest;
///
unittest
{
//Template API
import std.digest.md;
ubyte[20] hash = ripemd160Of("abc");
assert(toHexString(hash) == "8EB208F7E05D987A9B044A8E98C6B087F15A0BFC");
//Feeding data
ubyte[1024] data;
RIPEMD160 md;
md.start();
md.put(data[]);
md.start(); //Start again
md.put(data[]);
hash = md.finish();
}
///
unittest
{
//OOP API
import std.digest.md;
auto md = new RIPEMD160Digest();
ubyte[] hash = md.digest("abc");
assert(toHexString(hash) == "8EB208F7E05D987A9B044A8E98C6B087F15A0BFC");
//Feeding data
ubyte[1024] data;
md.put(data[]);
md.reset(); //Start again
md.put(data[]);
hash = md.finish();
}
//rotateLeft rotates x left n bits
private uint rotateLeft(uint x, uint n) @safe pure nothrow @nogc
{
// With recently added optimization to DMD (commit 32ea0206 at 07/28/11), this is translated to rol.
// No assembler required.
return (x << n) | (x >> (32-n));
}
/**
* Template API RIPEMD160 implementation.
* See $(D std.digest.digest) for differences between template and OOP API.
*/
struct RIPEMD160
{
private:
// magic initialization constants
uint[5] _state = [0x67452301,0xefcdab89,0x98badcfe,0x10325476,0xc3d2e1f0]; // state (ABCDE)
ulong _count; //number of bits, modulo 2^64
ubyte[64] _buffer; // input buffer
static immutable ubyte[64] _padding =
[
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
];
// F, G, H, I and J are basic RIPEMD160 functions
static @safe pure nothrow @nogc
{
uint F(uint x, uint y, uint z) { return x ^ y ^ z; }
uint G(uint x, uint y, uint z) { return (x & y) | (~x & z); }
uint H(uint x, uint y, uint z) { return (x | ~y) ^ z; }
uint I(uint x, uint y, uint z) { return (x & z) | (y & ~z); }
uint J(uint x, uint y, uint z) { return x ^ (y | ~z); }
}
/*
* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
* Rotation is separate from addition to prevent recomputation.
*/
/* the ten basic operations FF() through III() */
static void FF(ref uint a, uint b, ref uint c, uint d, uint e, uint x, uint s)
@safe pure nothrow @nogc
{
a += F(b, c, d) + x;
a = rotateLeft(a, s) + e;
c = rotateLeft(c, 10);
}
static void GG(ref uint a, uint b, ref uint c, uint d, uint e, uint x, uint s)
@safe pure nothrow @nogc
{
a += G(b, c, d) + x + 0x5a827999UL;
a = rotateLeft(a, s) + e;
c = rotateLeft(c, 10);
}
static void HH(ref uint a, uint b, ref uint c, uint d, uint e, uint x, uint s)
@safe pure nothrow @nogc
{
a += H(b, c, d) + x + 0x6ed9eba1UL;
a = rotateLeft(a, s) + e;
c = rotateLeft(c, 10);
}
static void II(ref uint a, uint b, ref uint c, uint d, uint e, uint x, uint s)
@safe pure nothrow @nogc
{
a += I(b, c, d) + x + 0x8f1bbcdcUL;
a = rotateLeft(a, s) + e;
c = rotateLeft(c, 10);
}
static void JJ(ref uint a, uint b, ref uint c, uint d, uint e, uint x, uint s)
@safe pure nothrow @nogc
{
a += J(b, c, d) + x + 0xa953fd4eUL;
a = rotateLeft(a, s) + e;
c = rotateLeft(c, 10);
}
/*
* FFF, GGG, HHH, and III transformations for parallel rounds 1, 2, 3, and 4.
* Rotation is separate from addition to prevent recomputation.
*/
static void FFF(ref uint a, uint b, ref uint c, uint d, uint e, uint x, uint s)
@safe pure nothrow @nogc
{
a += F(b, c, d) + x;
a = rotateLeft(a, s) + e;
c = rotateLeft(c, 10);
}
static void GGG(ref uint a, uint b, ref uint c, uint d, uint e, uint x, uint s)
@safe pure nothrow @nogc
{
a += G(b, c, d) + x + 0x7a6d76e9UL;
a = rotateLeft(a, s) + e;
c = rotateLeft(c, 10);
}
static void HHH(ref uint a, uint b, ref uint c, uint d, uint e, uint x, uint s)
@safe pure nothrow @nogc
{
a += H(b, c, d) + x + 0x6d703ef3UL;
a = rotateLeft(a, s) + e;
c = rotateLeft(c, 10);
}
static void III(ref uint a, uint b, ref uint c, uint d, uint e, uint x, uint s)
@safe pure nothrow @nogc
{
a += I(b, c, d) + x + 0x5c4dd124UL;
a = rotateLeft(a, s) + e;
c = rotateLeft(c, 10);
}
static void JJJ(ref uint a, uint b, ref uint c, uint d, uint e, uint x, uint s)
@safe pure nothrow @nogc
{
a += J(b, c, d) + x + 0x50a28be6UL;
a = rotateLeft(a, s) + e;
c = rotateLeft(c, 10);
}
/*
* RIPEMD160 basic transformation. Transforms state based on block.
*/
private void transform(const(ubyte[64])* block)
pure nothrow @nogc
{
uint aa = _state[0],
bb = _state[1],
cc = _state[2],
dd = _state[3],
ee = _state[4];
uint aaa = _state[0],
bbb = _state[1],
ccc = _state[2],
ddd = _state[3],
eee = _state[4];
uint[16] x = void;
version(BigEndian)
{
import std.bitmanip : littleEndianToNative;
for(size_t i = 0; i < 16; i++)
{
x[i] = littleEndianToNative!uint(*cast(ubyte[4]*)&(*block)[i*4]);
}
}
else
{
(cast(ubyte*)x.ptr)[0 .. 64] = (cast(ubyte*)block)[0 .. 64];
}
/* round 1 */
FF(aa, bb, cc, dd, ee, x[ 0], 11);
FF(ee, aa, bb, cc, dd, x[ 1], 14);
FF(dd, ee, aa, bb, cc, x[ 2], 15);
FF(cc, dd, ee, aa, bb, x[ 3], 12);
FF(bb, cc, dd, ee, aa, x[ 4], 5);
FF(aa, bb, cc, dd, ee, x[ 5], 8);
FF(ee, aa, bb, cc, dd, x[ 6], 7);
FF(dd, ee, aa, bb, cc, x[ 7], 9);
FF(cc, dd, ee, aa, bb, x[ 8], 11);
FF(bb, cc, dd, ee, aa, x[ 9], 13);
FF(aa, bb, cc, dd, ee, x[10], 14);
FF(ee, aa, bb, cc, dd, x[11], 15);
FF(dd, ee, aa, bb, cc, x[12], 6);
FF(cc, dd, ee, aa, bb, x[13], 7);
FF(bb, cc, dd, ee, aa, x[14], 9);
FF(aa, bb, cc, dd, ee, x[15], 8);
/* round 2 */
GG(ee, aa, bb, cc, dd, x[ 7], 7);
GG(dd, ee, aa, bb, cc, x[ 4], 6);
GG(cc, dd, ee, aa, bb, x[13], 8);
GG(bb, cc, dd, ee, aa, x[ 1], 13);
GG(aa, bb, cc, dd, ee, x[10], 11);
GG(ee, aa, bb, cc, dd, x[ 6], 9);
GG(dd, ee, aa, bb, cc, x[15], 7);
GG(cc, dd, ee, aa, bb, x[ 3], 15);
GG(bb, cc, dd, ee, aa, x[12], 7);
GG(aa, bb, cc, dd, ee, x[ 0], 12);
GG(ee, aa, bb, cc, dd, x[ 9], 15);
GG(dd, ee, aa, bb, cc, x[ 5], 9);
GG(cc, dd, ee, aa, bb, x[ 2], 11);
GG(bb, cc, dd, ee, aa, x[14], 7);
GG(aa, bb, cc, dd, ee, x[11], 13);
GG(ee, aa, bb, cc, dd, x[ 8], 12);
/* round 3 */
HH(dd, ee, aa, bb, cc, x[ 3], 11);
HH(cc, dd, ee, aa, bb, x[10], 13);
HH(bb, cc, dd, ee, aa, x[14], 6);
HH(aa, bb, cc, dd, ee, x[ 4], 7);
HH(ee, aa, bb, cc, dd, x[ 9], 14);
HH(dd, ee, aa, bb, cc, x[15], 9);
HH(cc, dd, ee, aa, bb, x[ 8], 13);
HH(bb, cc, dd, ee, aa, x[ 1], 15);
HH(aa, bb, cc, dd, ee, x[ 2], 14);
HH(ee, aa, bb, cc, dd, x[ 7], 8);
HH(dd, ee, aa, bb, cc, x[ 0], 13);
HH(cc, dd, ee, aa, bb, x[ 6], 6);
HH(bb, cc, dd, ee, aa, x[13], 5);
HH(aa, bb, cc, dd, ee, x[11], 12);
HH(ee, aa, bb, cc, dd, x[ 5], 7);
HH(dd, ee, aa, bb, cc, x[12], 5);
/* round 4 */
II(cc, dd, ee, aa, bb, x[ 1], 11);
II(bb, cc, dd, ee, aa, x[ 9], 12);
II(aa, bb, cc, dd, ee, x[11], 14);
II(ee, aa, bb, cc, dd, x[10], 15);
II(dd, ee, aa, bb, cc, x[ 0], 14);
II(cc, dd, ee, aa, bb, x[ 8], 15);
II(bb, cc, dd, ee, aa, x[12], 9);
II(aa, bb, cc, dd, ee, x[ 4], 8);
II(ee, aa, bb, cc, dd, x[13], 9);
II(dd, ee, aa, bb, cc, x[ 3], 14);
II(cc, dd, ee, aa, bb, x[ 7], 5);
II(bb, cc, dd, ee, aa, x[15], 6);
II(aa, bb, cc, dd, ee, x[14], 8);
II(ee, aa, bb, cc, dd, x[ 5], 6);
II(dd, ee, aa, bb, cc, x[ 6], 5);
II(cc, dd, ee, aa, bb, x[ 2], 12);
/* round 5 */
JJ(bb, cc, dd, ee, aa, x[ 4], 9);
JJ(aa, bb, cc, dd, ee, x[ 0], 15);
JJ(ee, aa, bb, cc, dd, x[ 5], 5);
JJ(dd, ee, aa, bb, cc, x[ 9], 11);
JJ(cc, dd, ee, aa, bb, x[ 7], 6);
JJ(bb, cc, dd, ee, aa, x[12], 8);
JJ(aa, bb, cc, dd, ee, x[ 2], 13);
JJ(ee, aa, bb, cc, dd, x[10], 12);
JJ(dd, ee, aa, bb, cc, x[14], 5);
JJ(cc, dd, ee, aa, bb, x[ 1], 12);
JJ(bb, cc, dd, ee, aa, x[ 3], 13);
JJ(aa, bb, cc, dd, ee, x[ 8], 14);
JJ(ee, aa, bb, cc, dd, x[11], 11);
JJ(dd, ee, aa, bb, cc, x[ 6], 8);
JJ(cc, dd, ee, aa, bb, x[15], 5);
JJ(bb, cc, dd, ee, aa, x[13], 6);
/* parallel round 1 */
JJJ(aaa, bbb, ccc, ddd, eee, x[ 5], 8);
JJJ(eee, aaa, bbb, ccc, ddd, x[14], 9);
JJJ(ddd, eee, aaa, bbb, ccc, x[ 7], 9);
JJJ(ccc, ddd, eee, aaa, bbb, x[ 0], 11);
JJJ(bbb, ccc, ddd, eee, aaa, x[ 9], 13);
JJJ(aaa, bbb, ccc, ddd, eee, x[ 2], 15);
JJJ(eee, aaa, bbb, ccc, ddd, x[11], 15);
JJJ(ddd, eee, aaa, bbb, ccc, x[ 4], 5);
JJJ(ccc, ddd, eee, aaa, bbb, x[13], 7);
JJJ(bbb, ccc, ddd, eee, aaa, x[ 6], 7);
JJJ(aaa, bbb, ccc, ddd, eee, x[15], 8);
JJJ(eee, aaa, bbb, ccc, ddd, x[ 8], 11);
JJJ(ddd, eee, aaa, bbb, ccc, x[ 1], 14);
JJJ(ccc, ddd, eee, aaa, bbb, x[10], 14);
JJJ(bbb, ccc, ddd, eee, aaa, x[ 3], 12);
JJJ(aaa, bbb, ccc, ddd, eee, x[12], 6);
/* parallel round 2 */
III(eee, aaa, bbb, ccc, ddd, x[ 6], 9);
III(ddd, eee, aaa, bbb, ccc, x[11], 13);
III(ccc, ddd, eee, aaa, bbb, x[ 3], 15);
III(bbb, ccc, ddd, eee, aaa, x[ 7], 7);
III(aaa, bbb, ccc, ddd, eee, x[ 0], 12);
III(eee, aaa, bbb, ccc, ddd, x[13], 8);
III(ddd, eee, aaa, bbb, ccc, x[ 5], 9);
III(ccc, ddd, eee, aaa, bbb, x[10], 11);
III(bbb, ccc, ddd, eee, aaa, x[14], 7);
III(aaa, bbb, ccc, ddd, eee, x[15], 7);
III(eee, aaa, bbb, ccc, ddd, x[ 8], 12);
III(ddd, eee, aaa, bbb, ccc, x[12], 7);
III(ccc, ddd, eee, aaa, bbb, x[ 4], 6);
III(bbb, ccc, ddd, eee, aaa, x[ 9], 15);
III(aaa, bbb, ccc, ddd, eee, x[ 1], 13);
III(eee, aaa, bbb, ccc, ddd, x[ 2], 11);
/* parallel round 3 */
HHH(ddd, eee, aaa, bbb, ccc, x[15], 9);
HHH(ccc, ddd, eee, aaa, bbb, x[ 5], 7);
HHH(bbb, ccc, ddd, eee, aaa, x[ 1], 15);
HHH(aaa, bbb, ccc, ddd, eee, x[ 3], 11);
HHH(eee, aaa, bbb, ccc, ddd, x[ 7], 8);
HHH(ddd, eee, aaa, bbb, ccc, x[14], 6);
HHH(ccc, ddd, eee, aaa, bbb, x[ 6], 6);
HHH(bbb, ccc, ddd, eee, aaa, x[ 9], 14);
HHH(aaa, bbb, ccc, ddd, eee, x[11], 12);
HHH(eee, aaa, bbb, ccc, ddd, x[ 8], 13);
HHH(ddd, eee, aaa, bbb, ccc, x[12], 5);
HHH(ccc, ddd, eee, aaa, bbb, x[ 2], 14);
HHH(bbb, ccc, ddd, eee, aaa, x[10], 13);
HHH(aaa, bbb, ccc, ddd, eee, x[ 0], 13);
HHH(eee, aaa, bbb, ccc, ddd, x[ 4], 7);
HHH(ddd, eee, aaa, bbb, ccc, x[13], 5);
/* parallel round 4 */
GGG(ccc, ddd, eee, aaa, bbb, x[ 8], 15);
GGG(bbb, ccc, ddd, eee, aaa, x[ 6], 5);
GGG(aaa, bbb, ccc, ddd, eee, x[ 4], 8);
GGG(eee, aaa, bbb, ccc, ddd, x[ 1], 11);
GGG(ddd, eee, aaa, bbb, ccc, x[ 3], 14);
GGG(ccc, ddd, eee, aaa, bbb, x[11], 14);
GGG(bbb, ccc, ddd, eee, aaa, x[15], 6);
GGG(aaa, bbb, ccc, ddd, eee, x[ 0], 14);
GGG(eee, aaa, bbb, ccc, ddd, x[ 5], 6);
GGG(ddd, eee, aaa, bbb, ccc, x[12], 9);
GGG(ccc, ddd, eee, aaa, bbb, x[ 2], 12);
GGG(bbb, ccc, ddd, eee, aaa, x[13], 9);
GGG(aaa, bbb, ccc, ddd, eee, x[ 9], 12);
GGG(eee, aaa, bbb, ccc, ddd, x[ 7], 5);
GGG(ddd, eee, aaa, bbb, ccc, x[10], 15);
GGG(ccc, ddd, eee, aaa, bbb, x[14], 8);
/* parallel round 5 */
FFF(bbb, ccc, ddd, eee, aaa, x[12] , 8);
FFF(aaa, bbb, ccc, ddd, eee, x[15] , 5);
FFF(eee, aaa, bbb, ccc, ddd, x[10] , 12);
FFF(ddd, eee, aaa, bbb, ccc, x[ 4] , 9);
FFF(ccc, ddd, eee, aaa, bbb, x[ 1] , 12);
FFF(bbb, ccc, ddd, eee, aaa, x[ 5] , 5);
FFF(aaa, bbb, ccc, ddd, eee, x[ 8] , 14);
FFF(eee, aaa, bbb, ccc, ddd, x[ 7] , 6);
FFF(ddd, eee, aaa, bbb, ccc, x[ 6] , 8);
FFF(ccc, ddd, eee, aaa, bbb, x[ 2] , 13);
FFF(bbb, ccc, ddd, eee, aaa, x[13] , 6);
FFF(aaa, bbb, ccc, ddd, eee, x[14] , 5);
FFF(eee, aaa, bbb, ccc, ddd, x[ 0] , 15);
FFF(ddd, eee, aaa, bbb, ccc, x[ 3] , 13);
FFF(ccc, ddd, eee, aaa, bbb, x[ 9] , 11);
FFF(bbb, ccc, ddd, eee, aaa, x[11] , 11);
/* combine results */
ddd += cc + _state[1]; /* final result for _state[0] */
_state[1] = _state[2] + dd + eee;
_state[2] = _state[3] + ee + aaa;
_state[3] = _state[4] + aa + bbb;
_state[4] = _state[0] + bb + ccc;
_state[0] = ddd;
//Zeroize sensitive information.
x[] = 0;
}
public:
/**
* Use this to feed the digest with data.
* Also implements the $(XREF_PACK range,primitives,isOutputRange)
* interface for $(D ubyte) and $(D const(ubyte)[]).
*
* Examples:
* ----
* RIPEMD160 dig;
* dig.put(cast(ubyte)0); //single ubyte
* dig.put(cast(ubyte)0, cast(ubyte)0); //variadic
* ubyte[10] buf;
* dig.put(buf); //buffer
* ----
*/
void put(scope const(ubyte)[] data...) @trusted pure nothrow @nogc
{
uint i, index, partLen;
auto inputLen = data.length;
//Compute number of bytes mod 64
index = (cast(uint)_count >> 3) & (64 - 1);
//Update number of bits
_count += inputLen * 8;
partLen = 64 - index;
//Transform as many times as possible
if (inputLen >= partLen)
{
(&_buffer[index])[0 .. partLen] = data.ptr[0 .. partLen];
transform(&_buffer);
for(i = partLen; i + 63 < inputLen; i += 64)
{
transform(cast(const(ubyte[64])*)(data[i .. i + 64].ptr));
}
index = 0;
}
else
{
i = 0;
}
/* Buffer remaining input */
if (inputLen - i)
(&_buffer[index])[0 .. inputLen-i] = (&data[i])[0 .. inputLen-i];
}
/**
* Used to (re)initialize the RIPEMD160 digest.
*
* Note:
* For this RIPEMD160 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:
* --------
* RIPEMD160 digest;
* //digest.start(); //Not necessary
* digest.put(0);
* --------
*/
void start() @safe pure nothrow @nogc
{
this = RIPEMD160.init;
}
/**
* Returns the finished RIPEMD160 hash. This also calls $(LREF start) to
* reset the internal state.
*
* Examples:
* --------
* //Simple example
* RIPEMD160 hash;
* hash.start();
* hash.put(cast(ubyte)0);
* ubyte[20] result = hash.finish();
* assert(toHexString(result) == "C81B94933420221A7AC004A90242D8B1D3E5070D");
* --------
*/
ubyte[20] finish() @trusted pure nothrow @nogc
{
import std.bitmanip : nativeToLittleEndian;
ubyte[20] data = void;
ubyte[8] bits = void;
uint index, padLen;
//Save number of bits
bits[0 .. 8] = nativeToLittleEndian(_count)[];
//Pad out to 56 mod 64
index = (cast(uint)_count >> 3) & (64 - 1);
padLen = (index < 56) ? (56 - index) : (120 - index);
put(_padding[0 .. padLen]);
//Append length (before padding)
put(bits);
//Store state in digest
data[0 .. 4] = nativeToLittleEndian(_state[0])[];
data[4 .. 8] = nativeToLittleEndian(_state[1])[];
data[8 .. 12] = nativeToLittleEndian(_state[2])[];
data[12 .. 16] = nativeToLittleEndian(_state[3])[];
data[16 .. 20] = nativeToLittleEndian(_state[4])[];
/* Zeroize sensitive information. */
start();
return data;
}
}
///
unittest
{
//Simple example, hashing a string using ripemd160Of helper function
ubyte[20] hash = ripemd160Of("abc");
//Let's get a hash string
assert(toHexString(hash) == "8EB208F7E05D987A9B044A8E98C6B087F15A0BFC");
}
///
unittest
{
//Using the basic API
RIPEMD160 hash;
hash.start();
ubyte[1024] data;
//Initialize data here...
hash.put(data);
ubyte[20] result = hash.finish();
}
///
unittest
{
//Let's use the template features:
void doSomething(T)(ref T hash) if(isDigest!T)
{
hash.put(cast(ubyte)0);
}
RIPEMD160 md;
md.start();
doSomething(md);
assert(toHexString(md.finish()) == "C81B94933420221A7AC004A90242D8B1D3E5070D");
}
///
unittest
{
//Simple example
RIPEMD160 hash;
hash.start();
hash.put(cast(ubyte)0);
ubyte[20] result = hash.finish();
assert(toHexString(result) == "C81B94933420221A7AC004A90242D8B1D3E5070D");
}
unittest
{
assert(isDigest!RIPEMD160);
}
unittest
{
import std.range;
ubyte[20] digest;
RIPEMD160 md;
md.put(cast(ubyte[])"abcdef");
md.start();
md.put(cast(ubyte[])"");
assert(md.finish() == cast(ubyte[])x"9c1185a5c5e9fc54612808977ee8f548b2258d31");
digest = ripemd160Of("");
assert(digest == cast(ubyte[])x"9c1185a5c5e9fc54612808977ee8f548b2258d31");
digest = ripemd160Of("a");
assert(digest == cast(ubyte[])x"0bdc9d2d256b3ee9daae347be6f4dc835a467ffe");
digest = ripemd160Of("abc");
assert(digest == cast(ubyte[])x"8eb208f7e05d987a9b044a8e98c6b087f15a0bfc");
digest = ripemd160Of("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq");
assert(digest == cast(ubyte[])x"12a053384a9c0c88e405a06c27dcf49ada62eb2b");
digest = ripemd160Of("message digest");
assert(digest == cast(ubyte[])x"5d0689ef49d2fae572b881b123a85ffa21595f36");
digest = ripemd160Of("abcdefghijklmnopqrstuvwxyz");
assert(digest == cast(ubyte[])x"f71c27109c692c1b56bbdceb5b9d2865b3708dbc");
digest = ripemd160Of("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
assert(digest == cast(ubyte[])x"b0e20b6e3116640286ed3a87a5713079b21f5189");
digest = ripemd160Of("1234567890123456789012345678901234567890"~
"1234567890123456789012345678901234567890");
assert(digest == cast(ubyte[])x"9b752e45573d4b39f4dbd3323cab82bf63326bfb");
assert(toHexString(cast(ubyte[20])x"f71c27109c692c1b56bbdceb5b9d2865b3708dbc")
== "F71C27109C692C1B56BBDCEB5B9D2865B3708DBC");
ubyte[] onemilliona = new ubyte[1000000];
onemilliona[] = 'a';
digest = ripemd160Of(onemilliona);
assert(digest == cast(ubyte[])x"52783243c1697bdbe16d37f97f68f08325dc1528");
auto oneMillionRange = repeat!ubyte(cast(ubyte)'a', 1000000);
digest = ripemd160Of(oneMillionRange);
assert(digest == cast(ubyte[])x"52783243c1697bdbe16d37f97f68f08325dc1528");
}
/**
* This is a convenience alias for $(XREF_PACK digest,digest,digest) using the
* RIPEMD160 implementation.
*/
//simple alias doesn't work here, hope this gets inlined...
auto ripemd160Of(T...)(T data)
{
return digest!(RIPEMD160, T)(data);
}
///
unittest
{
ubyte[20] hash = ripemd160Of("abc");
assert(hash == digest!RIPEMD160("abc"));
}
/**
* OOP API RIPEMD160 implementation.
* See $(D std.digest.digest) for differences between template and OOP API.
*
* This is an alias for $(D $(XREF_PACK digest,digest,WrapperDigest)!RIPEMD160),
* see there for more information.
*/
alias RIPEMD160Digest = WrapperDigest!RIPEMD160;
///
unittest
{
//Simple example, hashing a string using Digest.digest helper function
auto md = new RIPEMD160Digest();
ubyte[] hash = md.digest("abc");
//Let's get a hash string
assert(toHexString(hash) == "8EB208F7E05D987A9B044A8E98C6B087F15A0BFC");
}
///
unittest
{
//Let's use the OOP features:
void test(Digest dig)
{
dig.put(cast(ubyte)0);
}
auto md = new RIPEMD160Digest();
test(md);
//Let's use a custom buffer:
ubyte[20] buf;
ubyte[] result = md.finish(buf[]);
assert(toHexString(result) == "C81B94933420221A7AC004A90242D8B1D3E5070D");
}
unittest
{
auto md = new RIPEMD160Digest();
md.put(cast(ubyte[])"abcdef");
md.reset();
md.put(cast(ubyte[])"");
assert(md.finish() == cast(ubyte[])x"9c1185a5c5e9fc54612808977ee8f548b2258d31");
md.put(cast(ubyte[])"abcdefghijklmnopqrstuvwxyz");
ubyte[20] result;
auto result2 = md.finish(result[]);
assert(result[0 .. 20] == result2 && result2 == cast(ubyte[])x"f71c27109c692c1b56bbdceb5b9d2865b3708dbc");
debug
{
import std.exception;
assertThrown!Error(md.finish(result[0 .. 19]));
}
assert(md.length == 20);
assert(md.digest("") == cast(ubyte[])x"9c1185a5c5e9fc54612808977ee8f548b2258d31");
assert(md.digest("a") == cast(ubyte[])x"0bdc9d2d256b3ee9daae347be6f4dc835a467ffe");
assert(md.digest("abc") == cast(ubyte[])x"8eb208f7e05d987a9b044a8e98c6b087f15a0bfc");
assert(md.digest("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")
== cast(ubyte[])x"12a053384a9c0c88e405a06c27dcf49ada62eb2b");
assert(md.digest("message digest") == cast(ubyte[])x"5d0689ef49d2fae572b881b123a85ffa21595f36");
assert(md.digest("abcdefghijklmnopqrstuvwxyz")
== cast(ubyte[])x"f71c27109c692c1b56bbdceb5b9d2865b3708dbc");
assert(md.digest("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
== cast(ubyte[])x"b0e20b6e3116640286ed3a87a5713079b21f5189");
assert(md.digest("1234567890123456789012345678901234567890",
"1234567890123456789012345678901234567890")
== cast(ubyte[])x"9b752e45573d4b39f4dbd3323cab82bf63326bfb");
assert(md.digest(new ubyte[160/8]) // 160 zero bits
== cast(ubyte[])x"5c00bd4aca04a9057c09b20b05f723f2e23deb65");
}
|