Skip to content

Commit

Permalink
Hashing refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ollydev committed Mar 4, 2024
1 parent 4784537 commit 9dff099
Show file tree
Hide file tree
Showing 16 changed files with 1,261 additions and 741 deletions.
14 changes: 7 additions & 7 deletions Source/Simba.lpi
Original file line number Diff line number Diff line change
Expand Up @@ -953,27 +953,27 @@
<IsPartOfProject Value="True"/>
</Unit120>
<Unit121>
<Filename Value="hash/simba.hash_crc64.pas"/>
<Filename Value="script/simba.script_compiler_imagefromstring.pas"/>
<IsPartOfProject Value="True"/>
</Unit121>
<Unit122>
<Filename Value="script/simba.script_compiler_imagefromstring.pas"/>
<Filename Value="image/simba.image_fastcompress.pas"/>
<IsPartOfProject Value="True"/>
</Unit122>
<Unit123>
<Filename Value="image/simba.image_fastcompress.pas"/>
<Filename Value="../Third-Party/basenenc_simba.pas"/>
<IsPartOfProject Value="True"/>
</Unit123>
<Unit124>
<Filename Value="../Third-Party/basenenc_simba.pas"/>
<IsPartOfProject Value="True"/>
</Unit124>
<Unit125>
<Filename Value="forms/simba.settingsform_editor_default.pas"/>
<IsPartOfProject Value="True"/>
<ComponentName Value="EditorDefaultFrame"/>
<HasResources Value="True"/>
<ResourceBaseClass Value="Frame"/>
</Unit124>
<Unit125>
<Filename Value="hash/simba.hash_murmur.pas"/>
<IsPartOfProject Value="True"/>
</Unit125>
</Units>
</ProjectOptions>
Expand Down
119 changes: 54 additions & 65 deletions Source/hash/simba.hash.pas
Original file line number Diff line number Diff line change
Expand Up @@ -10,67 +10,46 @@
interface

uses
Classes, SysUtils,
simba.base;
Classes, SysUtils;

type
{$scopedenums on}
HashAlgo = (
SHA1, SHA256, SHA384, SHA512,
MD5,
CRC32, CRC64
);
{$scopedenums off}

TSimbaHasher = class
protected
const HexDigits: array[0..15] of Char = '0123456789abcdef'; // lowercase
class function Hex(Value: Int64; Digits: Integer): string;
class function Hex(Value: UInt64; Digits: Integer): string;
public
constructor Create; virtual; abstract;
procedure Update(Msg: PByte; Length: Integer); virtual; abstract;
function Final: String; virtual; abstract;
end;
TSimbaHasherClass = class of TSimbaHasher;

procedure RegisterHasher(Algo: HashAlgo; HashClass: TSimbaHasherClass);
{$SCOPEDENUMS ON}
HashAlgo = (SHA1, SHA256, SHA384, SHA512, MD5);
{$SCOPEDENUMS OFF}

function HashBuffer(Algo: HashAlgo; Buf: PByte; Len: PtrUInt): String;
function HashBuffer(Algo: HashAlgo; Buf: PByte; Len: Integer): String;
function HashString(Algo: HashAlgo; const S: String): String;
function HashFile(Algo: HashAlgo; const FileName: String): String;

function Hash32(Data: PByte; Len: Int32; Seed: UInt32 = 0): UInt32; overload;
function Hash32(S: String; Seed: UInt32 = 0): UInt32; overload;

function Hash64(Data: PByte; Len: Int32; Seed: UInt64 = 0): UInt64; overload;
function Hash64(S: String; Seed: UInt64 = 0): UInt64; overload;

function CRC32(Data: PByte; Len: Int32): UInt32;
function CRC64(Data: PByte; Len: Int32): UInt64;

implementation

uses
crc,
simba.hash_sha1, simba.hash_sha256, simba.hash_sha384, simba.hash_sha512,
simba.hash_md5,
simba.hash_crc32, simba.hash_crc64;

var
Hashers: array[HashAlgo] of TSimbaHasherClass;
simba.hash_murmur;

procedure RegisterHasher(Algo: HashAlgo; HashClass: TSimbaHasherClass);
function HashBuffer(Algo: HashAlgo; Buf: PByte; Len: Integer): String;
begin
Hashers[Algo] := HashClass;
end;

function HashBuffer(Algo: HashAlgo; Buf: PByte; Len: PtrUInt): String;
begin
if (Hashers[Algo] = nil) then
SimbaException('Hash type not registered');
Result := '';

if (Len > 0) then
with Hashers[Algo].Create() do
try
Update(Buf, Len);

Result := Final();
finally
Free();
end
else
Result := '';
case Algo of
HashAlgo.SHA1: Result := Hash_SHA1(Buf, Len);
HashAlgo.SHA256: Result := Hash_SHA256(Buf, Len);
HashAlgo.SHA384: Result := Hash_SHA384(Buf, Len);
HashAlgo.SHA512: Result := Hash_SHA512(Buf, Len);
HashAlgo.MD5: Result := Hash_MD5(Buf, Len);
end;
end;

function HashString(Algo: HashAlgo; const S: String): String;
Expand All @@ -96,30 +75,40 @@ function HashFile(Algo: HashAlgo; const FileName: String): String;
Result := '';
end;

class function TSimbaHasher.Hex(Value: Int64; Digits: Integer): string;
var
i: Integer;
function Hash32(Data: PByte; Len: Int32; Seed: UInt32): UInt32;
begin
if (Digits = 0) then
Digits := 1;

SetLength(Result, Digits);
for i := 0 to Digits - 1 do
begin
Result[Digits - i] := HexDigits[Value and 15];
Value := Value shr 4;
end;
Result := TMurmur2aLE.HashBuf(Data, Len, Seed);
end;

while (Value <> 0) do
begin
Result := HexDigits[Value and 15] + Result;
Value := Value shr 4;
end;
function Hash32(S: String; Seed: UInt32): UInt32;
begin
if (Length(S) > 0) then
Result := Hash32(@S[1], Length(S), Seed)
else
Result := Seed;
end;

function Hash64(Data: PByte; Len: Int32; Seed: UInt64): UInt64;
begin
Result := TMurmur64aLE.HashBuf(Data, Len, Seed);
end;

function Hash64(S: String; Seed: UInt64): UInt64;
begin
if (Length(S) > 0) then
Result := Hash64(@S[1], Length(S), Seed)
else
Result := Seed;
end;

function CRC32(Data: PByte; Len: Int32): UInt32;
begin
Result := crc.crc32(crc.crc32(0, nil, 0), Data, Len);
end;

class function TSimbaHasher.Hex(Value: UInt64; Digits: Integer): string;
function CRC64(Data: PByte; Len: Int32): UInt64;
begin
Result := Hex(Int64(Value), Digits);
Result := crc.crc64(crc.crc64(0, nil, 0), Data, Len);
end;

end.
Expand Down
92 changes: 0 additions & 92 deletions Source/hash/simba.hash_crc32.pas

This file was deleted.

93 changes: 0 additions & 93 deletions Source/hash/simba.hash_crc64.pas

This file was deleted.

Loading

0 comments on commit 9dff099

Please sign in to comment.