| 1 |
{******************************************************************************} |
| 2 |
{* Simple 32-bits checksum class integrated in existing **********} |
| 3 |
{* DCPcrypt v2.0 written by David Barton ([email protected]) **********} |
| 4 |
{******************************************************************************} |
| 5 |
{* A binary compatible implementation of simple 32-bits checksum *} |
| 6 |
{******************************************************************************} |
| 7 |
{* Copyright (C) 2021 Alexander Koblov ([email protected]) *} |
| 8 |
{* Permission is hereby granted, free of charge, to any person obtaining a *} |
| 9 |
{* copy of this software and associated documentation files (the "Software"), *} |
| 10 |
{* to deal in the Software without restriction, including without limitation *} |
| 11 |
{* the rights to use, copy, modify, merge, publish, distribute, sublicense, *} |
| 12 |
{* and/or sell copies of the Software, and to permit persons to whom the *} |
| 13 |
{* Software is furnished to do so, subject to the following conditions: *} |
| 14 |
{* *} |
| 15 |
{* The above copyright notice and this permission notice shall be included in *} |
| 16 |
{* all copies or substantial portions of the Software. *} |
| 17 |
{* *} |
| 18 |
{* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *} |
| 19 |
{* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *} |
| 20 |
{* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *} |
| 21 |
{* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *} |
| 22 |
{* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *} |
| 23 |
{* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *} |
| 24 |
{* DEALINGS IN THE SOFTWARE. *} |
| 25 |
{******************************************************************************} |
| 26 |
unit dcpchecksum32; |
| 27 |
|
| 28 |
{$mode objfpc}{$H+} |
| 29 |
|
| 30 |
interface |
| 31 |
|
| 32 |
uses |
| 33 |
Classes, Sysutils, DCPcrypt2, DCPconst; |
| 34 |
|
| 35 |
type |
| 36 |
|
| 37 |
{ TDCP_checksum32 } |
| 38 |
|
| 39 |
TDCP_checksum32 = class(TDCP_hash) |
| 40 |
protected |
| 41 |
CurrentHash: DWORD; |
| 42 |
public |
| 43 |
class function GetId: integer; override; |
| 44 |
class function GetAlgorithm: string; override; |
| 45 |
class function GetHashSize: integer; override; |
| 46 |
class function SelfTest: boolean; override; |
| 47 |
constructor Create(AOwner: TComponent); override; |
| 48 |
procedure Init; override; |
| 49 |
procedure Burn; override; |
| 50 |
procedure Update(const Buffer; Size: longword); override; |
| 51 |
procedure Final(var Digest); override; |
| 52 |
end; |
| 53 |
|
| 54 |
implementation |
| 55 |
{$R-}{$Q-} |
| 56 |
|
| 57 |
{ TDCP_checksum32 } |
| 58 |
|
| 59 |
{ TDCP_checksum32.GetHashSize } |
| 60 |
class function TDCP_checksum32.GetHashSize: integer; |
| 61 |
begin |
| 62 |
Result:= 32; |
| 63 |
end; |
| 64 |
|
| 65 |
{ TDCP_checksum32.GetId } |
| 66 |
class function TDCP_checksum32.GetId: integer; |
| 67 |
begin |
| 68 |
Result:= DCP_checksum32; |
| 69 |
end; |
| 70 |
|
| 71 |
{ TDCP_checksum32.GetAlgorithm } |
| 72 |
class function TDCP_checksum32.GetAlgorithm: string; |
| 73 |
begin |
| 74 |
Result:= 'CHECKSUM32'; |
| 75 |
end; |
| 76 |
|
| 77 |
{ TDCP_checksum32.SelfTest } |
| 78 |
class function TDCP_checksum32.SelfTest: boolean; |
| 79 |
const |
| 80 |
Test1Out: array[0..3] of byte=($00, $00, $01, $26); //Verified on 2021-08-24 |
| 81 |
Test2Out: array[0..3] of byte=($00, $00, $0B, $1F); |
| 82 |
var |
| 83 |
TestHash: TDCP_checksum32; |
| 84 |
TestOut: array[0..3] of byte; |
| 85 |
begin |
| 86 |
dcpFillChar(TestOut, SizeOf(TestOut), 0); |
| 87 |
TestHash:= TDCP_checksum32.Create(nil); |
| 88 |
TestHash.Init; |
| 89 |
TestHash.UpdateStr('abc'); |
| 90 |
TestHash.Final(TestOut); |
| 91 |
Result:= CompareMem(@TestOut,@Test1Out,Sizeof(Test1Out)); |
| 92 |
TestHash.Init; |
| 93 |
TestHash.UpdateStr('abcdefghijklmnopqrstuvwxyz'); |
| 94 |
TestHash.Final(TestOut); |
| 95 |
Result:= CompareMem(@TestOut,@Test2Out,Sizeof(Test2Out)) and Result; |
| 96 |
TestHash.Free; |
| 97 |
end; |
| 98 |
|
| 99 |
{ TDCP_checksum32.Create } |
| 100 |
constructor TDCP_checksum32.Create(AOwner: TComponent); |
| 101 |
begin |
| 102 |
inherited Create(AOwner); |
| 103 |
end; |
| 104 |
|
| 105 |
{ TDCP_checksum32.Init } |
| 106 |
procedure TDCP_checksum32.Init; |
| 107 |
begin |
| 108 |
Burn; |
| 109 |
CurrentHash:= 0; |
| 110 |
fInitialized:= true; |
| 111 |
end; |
| 112 |
|
| 113 |
{ TDCP_checksum32 } |
| 114 |
procedure TDCP_checksum32.Burn; |
| 115 |
begin |
| 116 |
CurrentHash:= 0; |
| 117 |
fInitialized:= false; |
| 118 |
end; |
| 119 |
|
| 120 |
{ TDCP_checksum32.Update } |
| 121 |
{$PUSH}{$R-}{$Q-}{$OPTIMIZATION LEVEL3} // no range, no overflow checks, optimize for speed (not size) |
| 122 |
procedure TDCP_checksum32.Update(const Buffer; Size: longword); |
| 123 |
var |
| 124 |
data: PByte; |
| 125 |
iIndex: longword; |
| 126 |
iChecksumLocal: DWORD; |
| 127 |
begin |
| 128 |
iChecksumLocal := CurrentHash; //Manipulating the copy "iChecksumLocal" in the loop is overall faster then working directly with property "CurrentHash". |
| 129 |
data := @Buffer; |
| 130 |
for iIndex := 1 to Size do |
| 131 |
begin |
| 132 |
iChecksumLocal := iChecksumLocal + data^; |
| 133 |
inc(data); |
| 134 |
end; |
| 135 |
CurrentHash := iChecksumLocal; |
| 136 |
end; |
| 137 |
{$POP} |
| 138 |
|
| 139 |
{ TDCP_checksum32.Final } |
| 140 |
procedure TDCP_checksum32.Final(var Digest); |
| 141 |
begin |
| 142 |
if not fInitialized then |
| 143 |
raise EDCP_hash.Create('Hash not initialized'); |
| 144 |
CurrentHash:= SwapEndian(CurrentHash); |
| 145 |
Move(CurrentHash, Digest, Sizeof(CurrentHash)); |
| 146 |
Burn; |
| 147 |
end; |
| 148 |
|
| 149 |
end. |
| 150 |
|