unit XXHASH; interface uses Sysutils,Classes,xxhashlib {$IFDEF YWRTL} //If you also installed my YWRTL package //YWRTL package: https://github.com/YWtheGod/YWRTL ,YWSTRUTIL //For a faster BinToHex ,YWTypes //For a no lock buffer allocating {$ENDIF} //use a system buildin implement instead will be just fine. ; type //XXH3 128bits {$ALIGN 16} THashXXH3 = record private state : XXH3_state_t; reserve : array[0..31] of byte; function _state:PXXH3_state_t; inline; procedure Update(const AData: PByte; ALength: NativeInt); overload; public class operator Assign (var Dest: THashXXH3; const [ref] Src: THashXXH3); class function Create: THashXXH3; static; class function SeedCreate(Seed:UInt64):THashXXH3; static; procedure reset; procedure SeedReset(Seed :UInt64); procedure Update(const AData; const ALength: NativeInt); overload; procedure Update(const AData: TBytes; const ALength: NativeInt = 0); overload; procedure Update(const Input: string); overload; procedure Update(const Input: string; const Encoding : TEncoding); overload; procedure Update(const st: TStream; var size: NativeInt); overload; function HashAsBytes : TBytes; overload; function HashAsString: string; overload; function HashAsUUID : TGuid; overload; class function HashAsUUID(const B:Pointer; const L : NativeInt) : TGuid; overload; static; class function HashAsUUID(const Input : string):TGuid; overload; static; class function HashAsUUID(const Input : string; const Encoding : TEncoding): TGuid; overload; static; class function HashAsUUID(const AData: TBytes; const ALen: NativeInt = 0): TGuid; overload; static; class function HashAsUUID(const st : TStream; var size : NativeInt):TGuid; overload; static; class function HashAsUUID(const st : TStream):TGuid; overload; static; class function HashAsBytes(const B:Pointer; const L : NativeInt) : TBytes; overload; static; class function HashAsBytes(const AData: TBytes; const ALen: NativeInt = 0): TBytes; overload; static; class function HashAsBytes(const Input: string):TBytes; overload; static; class function HashAsBytes(const Input: string; const Encoding: TEncoding): TBytes; overload; static; class function HashAsBytes(const st : TStream; var size : NativeInt):TBytes; overload; static; class function HashAsBytes(const st : TStream):TBytes; overload; static; class function HashAsString(const B: Pointer; const L : NativeInt): string; overload; static; class function HashAsString(const AData: TBytes; const ALen: NativeInt = 0): string; overload; static; class function HashAsString(const Input: string):string; overload; static; class function HashAsString(const Input: string; const Encoding: TEncoding): string; overload; static; class function HashAsString(const st: TStream; var size: NativeInt):string; overload; static; class function HashAsString(const st: TStream):string; overload; static; class function SeedHashAsUUID(const Seed:UInt64; const B:Pointer; const L : NativeInt) : TGuid; overload; static; class function SeedHashAsUUID(const Seed:UInt64; const Input : string):TGuid; overload; static; class function SeedHashAsUUID(const Seed:UInt64; const Input : string; const Encoding : TEncoding): TGuid; overload; static; class function SeedHashAsUUID(const Seed:UInt64; const AData: TBytes; const ALen: NativeInt = 0): TGuid; overload; static; class function SeedHashAsUUID(const Seed:UInt64; const st : TStream; var size : NativeInt):TGuid; overload; static; class function SeedHashAsUUID(const Seed:UInt64; const st : TStream):TGuid; overload; static; class function SeedHashAsBytes(const Seed:UInt64; const B:Pointer; const L : NativeInt) : TBytes; overload; static; class function SeedHashAsBytes(const Seed:UInt64; const AData: TBytes; const ALen: NativeInt = 0): TBytes; overload; static; class function SeedHashAsBytes(const Seed:UInt64; const Input: string):TBytes; overload; static; class function SeedHashAsBytes(const Seed:UInt64; const Input: string; const Encoding: TEncoding): TBytes; overload; static; class function SeedHashAsBytes(const Seed:UInt64; const st : TStream; var size : NativeInt):TBytes; overload; static; class function SeedHashAsBytes(const Seed:UInt64; const st : TStream):TBytes; overload; static; class function SeedHashAsString(const Seed:UInt64; const B: Pointer; const L : NativeInt): string; overload; static; class function SeedHashAsString(const Seed:UInt64; const AData: TBytes; const ALen: NativeInt = 0): string; overload; static; class function SeedHashAsString(const Seed:UInt64; const Input: string):string; overload; static; class function SeedHashAsString(const Seed:UInt64; const Input: string; const Encoding: TEncoding): string; overload; static; class function SeedHashAsString(const Seed:UInt64; const st: TStream; var size: NativeInt):string; overload; static; class function SeedHashAsString(const Seed:UInt64; const st: TStream):string; overload; static; end; //XXH64 THashXXH64 = record private state : XXH64_state_t; reserve : array[0..31] of byte; function _state:PXXH64_state_t; inline; procedure Update(const AData: PByte; ALength: NativeInt); overload; public class operator Assign (var Dest: THashXXH64; const [ref] Src: THashXXH64); class function Create: THashXXH64; static; class function SeedCreate(Seed:UInt64):THashXXH64; static; procedure reset; procedure SeedReset(Seed :UInt64); procedure Update(const AData; const ALength: NativeInt); overload; procedure Update(const AData: TBytes; const ALength: NativeInt = 0); overload; procedure Update(const Input: string); overload; procedure Update(const Input: string; const Encoding : TEncoding); overload; procedure Update(const st: TStream; var size: NativeInt); overload; function HashAsString: string; overload; function Hash : UInt64; overload; class function Hash(const B:Pointer; const L : NativeInt) : UInt64; overload; static; class function Hash(const Input : string):UInt64; overload; static; class function Hash(const Input : string; const Encoding : TEncoding): UInt64; overload; static; class function Hash(const AData: TBytes; const ALen: NativeInt = 0): UInt64; overload; static; class function Hash(const st : TStream; var size : NativeInt):UInt64; overload; static; class function Hash(const st : TStream):UInt64; overload; static; class function HashAsString(const B: Pointer; const L : NativeInt): string; overload; static; class function HashAsString(const AData: TBytes; const ALen: NativeInt = 0): string; overload; static; class function HashAsString(const Input: string):string; overload; static; class function HashAsString(const Input: string; const Encoding: TEncoding): string; overload; static; class function HashAsString(const st: TStream; var size: NativeInt):string; overload; static; class function HashAsString(const st: TStream):string; overload; static; class function SeedHash(const Seed:UInt64; const B:Pointer; const L : NativeInt) : UInt64; overload; static; class function SeedHash(const Seed:UInt64; const Input : string):UInt64; overload; static; class function SeedHash(const Seed:UInt64; const Input : string; const Encoding : TEncoding): UInt64; overload; static; class function SeedHash(const Seed:UInt64; const AData: TBytes; const ALen: NativeInt = 0): UInt64; overload; static; class function SeedHash(const Seed:UInt64; const st : TStream; var size : NativeInt):UInt64; overload; static; class function SeedHash(const Seed:UInt64; const st : TStream):UInt64; overload; static; class function SeedHashAsString(const Seed:UInt64; const B: Pointer; const L : NativeInt): string; overload; static; class function SeedHashAsString(const Seed:UInt64; const AData: TBytes; const ALen: NativeInt = 0): string; overload; static; class function SeedHashAsString(const Seed:UInt64; const Input: string):string; overload; static; class function SeedHashAsString(const Seed:UInt64; const Input: string; const Encoding: TEncoding): string; overload; static; class function SeedHashAsString(const Seed:UInt64; const st: TStream; var size: NativeInt):string; overload; static; class function SeedHashAsString(const Seed:UInt64; const st: TStream):string; overload; static; end; //XXH32 THashXXH32 = record private state : XXH32_state_t; reserve : array[0..31] of byte; function _state:PXXH32_state_t; inline; procedure Update(const AData: PByte; ALength: NativeInt); overload; public class operator Assign (var Dest: THashXXH32; const [ref] Src: THashXXH32); class function Create: THashXXH32; static; class function SeedCreate(Seed:Cardinal):THashXXH32; static; procedure reset; procedure SeedReset(Seed :Cardinal); procedure Update(const AData; const ALength: NativeInt); overload; procedure Update(const AData: TBytes; const ALength: NativeInt = 0); overload; procedure Update(const Input: string); overload; procedure Update(const Input: string; const Encoding : TEncoding); overload; procedure Update(const st: TStream; var size: NativeInt); overload; function HashAsString: string; overload; function Hash : Cardinal; overload; class function Hash(const B:Pointer; const L : NativeInt) : Cardinal; overload; static; class function Hash(const Input : string):Cardinal; overload; static; class function Hash(const Input : string; const Encoding : TEncoding): Cardinal; overload; static; class function Hash(const AData: TBytes; const ALen: NativeInt = 0): Cardinal; overload; static; class function Hash(const st : TStream; var size : NativeInt):Cardinal; overload; static; class function Hash(const st : TStream):Cardinal; overload; static; class function HashAsString(const B: Pointer; const L : NativeInt): string; overload; static; class function HashAsString(const AData: TBytes; const ALen: NativeInt = 0): string; overload; static; class function HashAsString(const Input: string):string; overload; static; class function HashAsString(const Input: string; const Encoding: TEncoding): string; overload; static; class function HashAsString(const st: TStream; var size: NativeInt):string; overload; static; class function HashAsString(const st: TStream):string; overload; static; class function SeedHash(const Seed:Cardinal; const B:Pointer; const L : NativeInt) : Cardinal; overload; static; class function SeedHash(const Seed:Cardinal; const Input : string):Cardinal; overload; static; class function SeedHash(const Seed:Cardinal; const Input : string; const Encoding : TEncoding): Cardinal; overload; static; class function SeedHash(const Seed:Cardinal; const AData: TBytes; const ALen: NativeInt = 0): Cardinal; overload; static; class function SeedHash(const Seed:Cardinal; const st : TStream; var size : NativeInt):Cardinal; overload; static; class function SeedHash(const Seed:Cardinal; const st : TStream):Cardinal; overload; static; class function SeedHashAsString(const Seed:Cardinal; const B: Pointer; const L : NativeInt): string; overload; static; class function SeedHashAsString(const Seed:Cardinal; const AData: TBytes; const ALen: NativeInt = 0): string; overload; static; class function SeedHashAsString(const Seed:Cardinal; const Input: string):string; overload; static; class function SeedHashAsString(const Seed:Cardinal; const Input: string; const Encoding: TEncoding): string; overload; static; class function SeedHashAsString(const Seed:Cardinal; const st: TStream; var size: NativeInt):string; overload; static; class function SeedHashAsString(const Seed:Cardinal; const st: TStream):string; overload; static; end; implementation {$IFDEF MSWINDOWS} uses libc; {$ELSE} uses Posix.String_; {$ENDIF} //uses Hash; function Hex128(const t : XXH128_hash_t):string; begin {$IFDEF YWRTL} Result := BinToHex2(@t,sizeof(XXH128_hash_t)); {$ELSE} setlength(Result,sizeof(XXH128_hash_t)*2); BinToHex(t,PWideChar(Result),sizeof(XXH128_hash_t)); {$ENDIF} end; function Hex64(const t : XXH64_hash_t):string; begin {$IFDEF YWRTL} Result := BinToHex2(@t,sizeof(XXH64_hash_t)); {$ELSE} setlength(Result,sizeof(XXH64_hash_t)*2); BinToHex(t,PWideChar(Result),sizeof(XXH64_hash_t)); {$ENDIF} end; function Hex32(const t : XXH32_hash_t):string; begin {$IFDEF YWRTL} Result := BinToHex2(@t,sizeof(XXH32_hash_t)); {$ELSE} setlength(Result,sizeof(XXH32_hash_t)*2); BinToHex(t,PWideChar(Result),sizeof(XXH32_hash_t)); {$ENDIF} end; procedure GetBuffer(var b : PByte); begin {$IFDEF YWRTL} b := bufferpool128k.GetBuffer; {$ELSE} Getmem(b,128*1024); {$ENDIF} end; procedure FreeBuffer(var b : PByte); begin {$IFDEF YWLIB} bufferpool128k.FreeBuffer(b); {$ELSE} FreeMem(b); {$ENDIF} end; function TrueLen(const AData: TBytes; const ALength:NativeInt):NativeInt; begin Result := Length(AData); if (ALength>0)and(ALengthXXH_OK then raise Exception.Create('XXH3_128bits_reset Error!'); end; procedure THashXXH3.SeedReset(Seed: UInt64); begin if XXH3_128bits_reset_withSeed(_state^,Seed)<>XXH_OK then raise Exception.Create('XXH3_128bits_reset Error!'); end; procedure THashXXH3.Update(const Input: string; const Encoding: TEncoding); begin Update(Encoding.GetBytes(input)); end; procedure THashXXH3.Update(const AData: PByte; ALength: NativeInt); begin if XXH3_128bits_update(_state^,AData,ALength)<>XXH_OK then raise Exception.Create('XXH3_128bits_update ERROR!'); end; procedure THashXXH3.Update(const AData; const ALength: NativeInt); begin Update(PByte(@AData), ALength); end; procedure THashXXH3.Update(const st: TStream; var size: NativeInt); var b : PByte; s,u : integer; t : NativeInt; begin t := 0; GetBuffer(b); try u := 128*1024; repeat if (size>0)and(sizeXXH_OK then raise Exception.Create('XXH64_reset Error!'); end; procedure THashXXH64.SeedReset(Seed: UInt64); begin if XXH64_reset(_state^,Seed)<>XXH_OK then raise Exception.Create('XXH64_reset Error!'); end; procedure THashXXH64.Update(const Input: string; const Encoding: TEncoding); begin Update(Encoding.GetBytes(input)); end; procedure THashXXH64.Update(const AData: PByte; ALength: NativeInt); begin if XXH64_update(_state^,AData,ALength)<>XXH_OK then raise Exception.Create('XXH64_update ERROR!'); end; procedure THashXXH64.Update(const AData; const ALength: NativeInt); begin Update(PByte(@AData), ALength); end; procedure THashXXH64.Update(const st: TStream; var size: NativeInt); var b : PByte; s,u : integer; t : NativeInt; begin t := 0; GetBuffer(b); try u := 128*1024; repeat if (size>0)and(sizeXXH_OK then raise Exception.Create('XXH32_reset Error!'); end; procedure THashXXH32.SeedReset(Seed: Cardinal); begin if XXH32_reset(_state^,Seed)<>XXH_OK then raise Exception.Create('XXH32_reset Error!'); end; procedure THashXXH32.Update(const Input: string; const Encoding: TEncoding); begin Update(Encoding.GetBytes(input)); end; procedure THashXXH32.Update(const AData: PByte; ALength: NativeInt); begin if XXH32_update(_state^,AData,ALength)<>XXH_OK then raise Exception.Create('XXH32_update ERROR!'); end; procedure THashXXH32.Update(const AData; const ALength: NativeInt); begin Update(PByte(@AData), ALength); end; procedure THashXXH32.Update(const st: TStream; var size: NativeInt); var b : PByte; s,u : integer; t : NativeInt; begin t := 0; GetBuffer(b); try u := 128*1024; repeat if (size>0)and(size