ADD: XXH3-128 hash algorithm (#issue #1779)

This commit is contained in:
Alexander Koblov 2024-09-14 13:13:37 +03:00
commit ea8c59fed2
5 changed files with 1239 additions and 8 deletions

View file

@ -0,0 +1,117 @@
{******************************************************************************}
{* DCPcrypt v2.0 written by David Barton (crypto@cityinthesky.co.uk) **********}
{******************************************************************************}
{* A binary compatible implementation of XXH3-128 *}
{******************************************************************************}
{* Copyright (C) 2024 Alexander Koblov (alexx2000@mail.ru) *}
{* Permission is hereby granted, free of charge, to any person obtaining a *}
{* copy of this software and associated documentation files (the "Software"), *}
{* to deal in the Software without restriction, including without limitation *}
{* the rights to use, copy, modify, merge, publish, distribute, sublicense, *}
{* and/or sell copies of the Software, and to permit persons to whom the *}
{* Software is furnished to do so, subject to the following conditions: *}
{* *}
{* The above copyright notice and this permission notice shall be included in *}
{* all copies or substantial portions of the Software. *}
{* *}
{* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *}
{* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *}
{* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *}
{* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *}
{* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *}
{* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *}
{* DEALINGS IN THE SOFTWARE. *}
{******************************************************************************}
unit DCPxxh3;
{$mode objfpc}{$H+}
interface
uses
Classes, Sysutils, DCPcrypt2, DCxxhash;
type
{ TDCP_xxh3_128 }
TDCP_xxh3_128 = class(TDCP_hash)
protected
S: PXXH3_state_t;
public
class function GetAlgorithm: string; override;
class function GetHashSize: integer; override;
class function SelfTest: boolean; override;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Init; override;
procedure Burn; override;
procedure Update(const Buffer; Size: longword); override;
procedure Final(var Digest); override;
end;
implementation
{$R-}{$Q-}
{ TDCP_xxh3_128 }
class function TDCP_xxh3_128.GetHashSize: integer;
begin
Result:= 128;
end;
class function TDCP_xxh3_128.GetAlgorithm: string;
begin
Result:= 'XXH3-128';
end;
class function TDCP_xxh3_128.SelfTest: boolean;
begin
Result:= False; // TODO: SelfTest XXH3_128
end;
constructor TDCP_xxh3_128.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
S:= XXH3_createState();
end;
destructor TDCP_xxh3_128.Destroy;
begin
XXH3_freeState(S);
inherited Destroy;
end;
procedure TDCP_xxh3_128.Init;
begin
Burn;
fInitialized:= true;
end;
procedure TDCP_xxh3_128.Burn;
begin
XXH3_128bits_reset(S);
fInitialized:= false;
end;
procedure TDCP_xxh3_128.Update(const Buffer; Size: longword);
begin
XXH3_128bits_update(S, @Buffer, Size);
end;
procedure TDCP_xxh3_128.Final(var Digest);
var
Temp: UInt64;
Hash: XXH128_hash_t;
begin
if not fInitialized then
raise EDCP_hash.Create('Hash not initialized');
Hash:= XXH3_128bits_digest(S);
Temp:= SwapEndian(Hash.low64);
Hash.low64:= SwapEndian(Hash.high64);
Hash.high64:= Temp;
Move(Hash, Digest, Sizeof(XXH128_hash_t));
Burn;
end;
end.

File diff suppressed because it is too large Load diff

View file

@ -37,8 +37,8 @@
"/>
<License Value="KAScrypt is open source software (released under the MIT license) and as such there is no charge for inclusion in other software.
"/>
<Version Major="3" Minor="1" Release="1"/>
<Files Count="30">
<Version Major="3" Minor="2"/>
<Files Count="32">
<Item1>
<Filename Value="dcpbase64.pas"/>
<UnitName Value="DCPbase64"/>
@ -159,6 +159,14 @@
<Filename Value="Hashes/dcpchecksum32.pas"/>
<UnitName Value="dcpchecksum32"/>
</Item30>
<Item31>
<Filename Value="Hashes/dcpxxh3.pas"/>
<UnitName Value="DCPxxh3"/>
</Item31>
<Item32>
<Filename Value="Hashes/dcxxhash.pas"/>
<UnitName Value="DCxxhash"/>
</Item32>
</Files>
<CompatibilityMode Value="True"/>
<RequiredPkgs Count="2">

View file

@ -11,7 +11,8 @@ uses
DCPbase64, DCPblockciphers, DCPconst, DCPcrypt2, DCPhaval, DCPmd4, DCPmd5,
DCPripemd128, DCPripemd160, DCPsha1, DCPsha256, DCPsha512, DCPtiger,
DCPcrc32, DCcrc32, DCblake2, DCPblake2, DCPsha3, HMAC, SHA3, SHA3_512,
ISAAC, scrypt, DCPrijndael, SHA1, Argon2, DCPblake3, dcpchecksum32;
ISAAC, scrypt, DCPrijndael, SHA1, Argon2, DCPblake3, dcpchecksum32, DCPxxh3,
DCxxhash;
implementation

View file

@ -4,7 +4,7 @@
General Hash Unit: This unit defines the common types, functions,
and procedures
Copyright (C) 2009-2023 Alexander Koblov (alexx2000@mail.ru)
Copyright (C) 2009-2024 Alexander Koblov (alexx2000@mail.ru)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@ -35,7 +35,7 @@ type
THashAlgorithm = (HASH_BLAKE2S, HASH_BLAKE2SP, HASH_BLAKE2B, HASH_BLAKE2BP, HASH_BLAKE3,
HASH_CHECKSUM32, HASH_CRC32, HASH_HAVAL, HASH_MD4, HASH_MD5, HASH_RIPEMD128, HASH_RIPEMD160,
HASH_SFV, HASH_SHA1, HASH_SHA224, HASH_SHA256, HASH_SHA384, HASH_SHA512,
HASH_SHA3_224, HASH_SHA3_256, HASH_SHA3_384, HASH_SHA3_512, HASH_TIGER,
HASH_SHA3_224, HASH_SHA3_256, HASH_SHA3_384, HASH_SHA3_512, HASH_TIGER, HASH_XXH3_128,
HASH_BEST
);
@ -43,7 +43,7 @@ var
HashFileExt: array[Low(THashAlgorithm)..Pred(High(THashAlgorithm))] of String = (
'blake2s', 'blake2sp', 'blake2b', 'blake2bp', 'blake3', 'checksum32', 'crc32', 'haval',
'md4', 'md5', 'ripemd128', 'ripemd160', 'sfv', 'sha', 'sha224', 'sha256',
'sha384', 'sha512', 'sha3', 'sha3', 'sha3', 'sha3', 'tiger'
'sha384', 'sha512', 'sha3', 'sha3', 'sha3', 'sha3', 'tiger', 'xxh3'
);
var
@ -51,7 +51,7 @@ var
'blake2s', 'blake2sp', 'blake2b', 'blake2bp', 'blake3', 'checksum32', 'crc32', 'haval',
'md4', 'md5', 'ripemd128', 'ripemd160', 'sfv', 'sha1_160', 'sha2_224',
'sha2_256', 'sha2_384', 'sha2_512', 'sha3_224', 'sha3_256',
'sha3_384', 'sha3_512', 'tiger'
'sha3_384', 'sha3_512', 'tiger', 'xxh3_128'
);
procedure HashInit(out Context: THashContext; Algorithm: THashAlgorithm);
@ -69,7 +69,7 @@ implementation
uses
LazUTF8, DCPhaval, DCPmd4, DCPmd5, DCPripemd128, DCPripemd160, DCPChecksum32, DCPcrc32,
DCPsha1, DCPsha256, DCPsha512, DCPtiger, DCPblake2, DCPblake3, DCPsha3;
DCPsha1, DCPsha256, DCPsha512, DCPtiger, DCPblake2, DCPblake3, DCPsha3, DCPxxh3;
procedure HashInit(out Context: THashContext; Algorithm: THashAlgorithm);
begin
@ -106,6 +106,7 @@ begin
HASH_SHA3_384: Context:= TDCP_sha3_384.Create(nil);
HASH_SHA3_512: Context:= TDCP_sha3_512.Create(nil);
HASH_TIGER: Context:= TDCP_tiger.Create(nil);
HASH_XXH3_128: Context:= TDCP_xxh3_128.Create(nil);
end;
Context.Init;