dcpcrc32.pas 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. {$mode objfpc}{$H+}
  27. interface
  28. uses
  29. Classes, Sysutils, DCPcrypt2, DCPconst, DCcrc32;
  30. type
  31. { TDCP_crc32 }
  32. TDCP_crc32 = class(TDCP_hash)
  33. protected
  34. CurrentHash: LongWord;
  35. public
  36. class function GetId: integer; override;
  37. class function GetAlgorithm: string; override;
  38. class function GetHashSize: integer; override;
  39. class function SelfTest: boolean; override;
  40. constructor Create(AOwner: TComponent); override;
  41. procedure Init; override;
  42. procedure Burn; override;
  43. procedure Update(const Buffer; Size: longword); override;
  44. procedure Final(var Digest); override;
  45. end;
  46. implementation
  47. {$R-}{$Q-}
  48. { TDCP_crc32 }
  49. class function TDCP_crc32.GetHashSize: integer;
  50. begin
  51. Result:= 32;
  52. end;
  53. class function TDCP_crc32.GetId: integer;
  54. begin
  55. Result:= DCP_crc32;
  56. end;
  57. class function TDCP_crc32.GetAlgorithm: string;
  58. begin
  59. Result:= 'CRC32';
  60. end;
  61. class function TDCP_crc32.SelfTest: boolean;
  62. const
  63. Test1Out: array[0..3] of byte=($35,$24,$41,$C2);
  64. Test2Out: array[0..3] of byte=($4C,$27,$50,$BD);
  65. var
  66. TestHash: TDCP_crc32;
  67. TestOut: array[0..3] of byte;
  68. begin
  69. dcpFillChar(TestOut, SizeOf(TestOut), 0);
  70. TestHash:= TDCP_crc32.Create(nil);
  71. TestHash.Init;
  72. TestHash.UpdateStr('abc');
  73. TestHash.Final(TestOut);
  74. Result:= CompareMem(@TestOut,@Test1Out,Sizeof(Test1Out));
  75. TestHash.Init;
  76. TestHash.UpdateStr('abcdefghijklmnopqrstuvwxyz');
  77. TestHash.Final(TestOut);
  78. Result:= CompareMem(@TestOut,@Test2Out,Sizeof(Test2Out)) and Result;
  79. TestHash.Free;
  80. end;
  81. constructor TDCP_crc32.Create(AOwner: TComponent);
  82. begin
  83. inherited Create(AOwner);
  84. end;
  85. procedure TDCP_crc32.Init;
  86. begin
  87. Burn;
  88. CurrentHash:= 0;
  89. fInitialized:= true;
  90. end;
  91. procedure TDCP_crc32.Burn;
  92. begin
  93. CurrentHash:= 0;
  94. fInitialized:= false;
  95. end;
  96. procedure TDCP_crc32.Update(const Buffer; Size: longword);
  97. var
  98. Bytes: PByte;
  99. begin
  100. Bytes:= @Buffer;
  101. CurrentHash:= crc32_16bytes(Bytes, Size, CurrentHash);
  102. end;
  103. procedure TDCP_crc32.Final(var Digest);
  104. begin
  105. if not fInitialized then
  106. raise EDCP_hash.Create('Hash not initialized');
  107. CurrentHash:= SwapEndian(CurrentHash);
  108. Move(CurrentHash, Digest, Sizeof(CurrentHash));
  109. Burn;
  110. end;
  111. end.