0

I use lockbox3 for years, util now with delphi xe7. Now I'm migrating to Alexandria and facing the problem.

With XE7 I used version 3.4 (or so - I cannot find version number in sources). With Alexandria I use whatever comes from Getit package manager.

My code is like follow.

function TForm1.md5(src: string): string;
  function Display(Buf: TBytes): String;
  var i: Integer;
  begin
    Result := '';
    for i := 0 to 15 do
      Result := Result + Format('%0.2x', [Buf[i]]);
    Result := LowerCase(Trim(Result));
  end;
var
  output : AnsiString;
  bytes : TBytes;
  P, Sz: integer;
  aByte: byte;
  s: string;
  MyHash : THash;
  Lib : TCryptographicLibrary;
begin
  Lib := TCryptographicLibrary.Create(nil);
  MyHash := THash.Create(nil);
  MyHash.CryptoLibrary := Lib;
  MyHash.HashId := 'native.hash.MD5';
  MyHash.Begin_Hash;
  MyHash.HashAnsiString(src);
  if not assigned(MyHash.HashOutputValue) then
    output := 'nil'
  else
  begin
    SetLength(Bytes, 16);
    Sz := MyHash.HashOutputValue.Size;
    if Sz <> 16 then
      output := Format('wrong size: %d', [Sz])
    else
    begin
      P := 0;
      MyHash.HashOutputValue.Position := 0;
      while MyHash.HashOutputValue.Read(aByte, 1) = 1 do
      begin
        bytes[P] := aByte;
        Inc(P);
      end;
      result := Display(Bytes);
    end;
  end;
  MyHash.Destroy;
  Lib.Destroy;
end;

This works like a charm in old environment. In Alexandria it throws 'Integer overflow' exception at

MyHash.HashAnsiString(src);

Does any body know, how to solve this problem?

regards M.

  • Have you enabled overflow checking in the compiler settings in the project options? – Delphi Coder Jul 28 '22 at 10:46
  • 1
    Why are you using `MyHash.HashAnsiString(src);` when `src` is of default string type which is in modern Delphi version a WideString instead. – SilverWarior Jul 28 '22 at 10:52
  • In addition to @SilverWarior's comment: modern Delphi here means since Delphi 2009! So you even did that wrong in XE7. – Delphi Coder Jul 28 '22 at 12:36
  • Thank you for the tips... I was actually trying ```MyHash.HashString(src, TEncoding.ANSI);``` but this does not help at all. I tried also other encodings. In compiler stetting I have overflow checking by default, but I tried to switch that off - no luck... this does not help. Do you have any idea what else I could try? – Mariusz Stefaniak Jul 28 '22 at 20:42

0 Answers0