| 461 | alternate version by TRP |
| 462 | |
| 463 | {{{ |
| 464 | unction CalcGabestHash(const Stream: TStream): Int64; overload; |
| 465 | const HashPartSize = 1 shl 16; // 64 KiB |
| 466 | |
| 467 | procedure UpdateHashFromStream(const Stream: TStream; var Hash: |
| 468 | Int64); inline; |
| 469 | var buffer: Array[0..HashPartSize div SizeOf(Int64) - 1] of Int64; |
| 470 | i : integer; |
| 471 | begin |
| 472 | Stream.ReadBuffer(buffer[0], SizeOf(buffer)); |
| 473 | for i := Low(buffer) to High(buffer) do |
| 474 | Inc(Hash, buffer[i]); |
| 475 | end; |
| 476 | |
| 477 | begin |
| 478 | result:= Stream.Size; |
| 479 | |
| 480 | if result < HashPartSize then |
| 481 | begin |
| 482 | // stream too small return invalid hash |
| 483 | result:= 0; |
| 484 | exit; |
| 485 | end; |
| 486 | |
| 487 | // first 64 KiB |
| 488 | Stream.Position:= 0; |
| 489 | UpdateHashFromStream(Stream, result); |
| 490 | |
| 491 | // last 64 KiB |
| 492 | Stream.Seek(-HashPartSize, soEnd); |
| 493 | UpdateHashFromStream(Stream, result); |
| 494 | |
| 495 | // use "IntToHex(result, 16);" to get a string and "StrToInt64('$' + |
| 496 | hash);" to get your Int64 back |
| 497 | end; |
| 498 | |
| 499 | function CalcGabestHash(const FileName: TFileName): Int64; overload; |
| 500 | var stream: TStream; |
| 501 | begin |
| 502 | stream:= TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite); |
| 503 | try |
| 504 | result:= CalcGabestHash(stream); |
| 505 | finally |
| 506 | stream.Free; |
| 507 | end; |
| 508 | end; |
| 509 | }}} |
| 510 | |