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