dcpblake3.pas 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. {******************************************************************************}
  2. {* DCPcrypt v2.0 written by David Barton ([email protected]) **********}
  3. {******************************************************************************}
  4. {* A binary compatible implementation of BLAKE3 *}
  5. {******************************************************************************}
  6. {* Copyright (C) 2020 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 DCPblake3;
  26. {$mode delphi}
  27. interface
  28. uses
  29. Classes, SysUtils, CTypes, DCPcrypt2, DCPconst, DCblake3;
  30. type
  31. { TDCP_blake3 }
  32. TDCP_blake3 = class(TDCP_hash)
  33. protected
  34. S: blake3_hasher;
  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. procedure Init; override;
  41. procedure Burn; override;
  42. procedure Update(const Buffer; Size: longword); override;
  43. procedure Final(var Digest); override;
  44. end;
  45. implementation
  46. { TDCP_blake3 }
  47. class function TDCP_blake3.GetId: integer;
  48. begin
  49. Result:= DCP_blake3;
  50. end;
  51. class function TDCP_blake3.GetAlgorithm: string;
  52. begin
  53. Result:= 'BLAKE3';
  54. end;
  55. class function TDCP_blake3.GetHashSize: integer;
  56. begin
  57. Result:= 256;
  58. end;
  59. class function TDCP_blake3.SelfTest: boolean;
  60. const
  61. Test1Out: array[0..31] of byte=
  62. ($64, $37, $b3, $ac, $38, $46, $51, $33, $ff, $b6, $3b, $75, $27, $3a, $8d, $b5,
  63. $48, $c5, $58, $46, $5d, $79, $db, $03, $fd, $35, $9c, $6c, $d5, $bd, $9d, $85);
  64. Test2Out: array[0..31] of byte=
  65. ($c1, $90, $12, $cc, $2a, $af, $0d, $c3, $d8, $e5, $c4, $5a, $1b, $79, $11, $4d,
  66. $2d, $f4, $2a, $bb, $2a, $41, $0b, $f5, $4b, $e0, $9e, $89, $1a, $f0, $6f, $f8 );
  67. var
  68. TestHash: TDCP_blake3;
  69. TestOut: array[0..31] of byte;
  70. begin
  71. dcpFillChar(TestOut, SizeOf(TestOut), 0);
  72. TestHash:= TDCP_blake3.Create(nil);
  73. TestHash.Init;
  74. TestHash.UpdateStr('abc');
  75. TestHash.Final(TestOut);
  76. Result:= boolean(CompareMem(@TestOut,@Test1Out,Sizeof(Test1Out)));
  77. TestHash.Init;
  78. TestHash.UpdateStr('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq');
  79. TestHash.Final(TestOut);
  80. Result:= boolean(CompareMem(@TestOut,@Test2Out,Sizeof(Test2Out))) and Result;
  81. TestHash.Free;
  82. end;
  83. procedure TDCP_blake3.Init;
  84. begin
  85. blake3_hasher_init(@S);
  86. fInitialized:= true;
  87. end;
  88. procedure TDCP_blake3.Burn;
  89. begin
  90. fInitialized:= false;
  91. end;
  92. procedure TDCP_blake3.Update(const Buffer; Size: longword);
  93. begin
  94. blake3_hasher_update(@S, @Buffer, Size);
  95. end;
  96. procedure TDCP_blake3.Final(var Digest);
  97. var
  98. Hash: array[0..Pred(BLAKE3_OUT_LEN)] of cuint8;
  99. begin
  100. if not fInitialized then
  101. raise EDCP_hash.Create('Hash not initialized');
  102. blake3_hasher_finalize(@S, Hash, SizeOf(Hash));
  103. Move(Hash, Digest, Sizeof(Hash));
  104. Burn;
  105. end;
  106. end.