TArgon2KeyDerivation in TMS Cryptography Pack v5

Hello,

What is now the equivalent of this code in TMS Cryptography Pack v5 ?

with TArgon2KeyDerivation.Create(16, '', 10, raw, 16, noUni) do
try
  Key := GenerateKey(ResName);
finally
  Free;
end;

Thank you

Hi Pierre,
That is KDF := TArgon2dClass.Create(), then

Conv := TConvert.Create;
SetLength(Output, 16); // buffer size

KDF.KeyDerivation(10 {Counter}, 16 {Memory}, ResName, '' {Salt}, Output)

Key := Conv.TBytesToString(Output);

Conv.Free.

I could definitely simplify a bit with an outputFormat in the method and save the final conversion.

bernard

Hi Bernard,

What about TArgon2dClass.GenerateKey() ?

var a2d := TArgon2dClass.Create;
try
  a2d.counter := 10;
  a2d.memory := 16;
  a2d.outputSizeBytes := 16;
  a2d.outputFormat := TConvertType.raw;
  Key := a2d.GenerateKey(N);
finally
  a2d.Free;
end;

That's the second option and most likely why I changed the initial Create.
I would still test the various cases before pushing to production.
I am open to improvements on the class.

This TConvert class is a real pain. Would be nice, if, at least, it was not a class (so we don't have to create/free it so often).

Example, I have this function for a while, the commented lines come from the usage of an earlier version of TMS Crypto Pack :

function GenerateSalt(var Salt: RawByteString): string;
begin
  SetLength(Salt, LIC_SALT_LEN);
//  RandomBuffer(Length(Salt), PAnsiChar(Salt));

  with TRandomCore.Create do
  try
    GetRandomBytes(Length(Salt), PAnsiChar(Salt));
  finally
    Free;
  end;

{$if defined(DEBUG)}
  Debug('Salt', Salt);
{$endif}

//  with TConvert.Create(raw) do
//  try
//    Result := CharToFormat(PAnsiChar(Salt), Length(Salt));
//  finally
//    Free;
//  end;

  Result := string(Salt);

{$if defined(DEBUG)}
  Debug('SaltFormat', Result[1], Length(Result) * SizeOf(Char));
{$endif}
end;

As fatr as I understand, TConvert "raw" is useless as there are no more CharToFormat() function that convert from PAnsiChar() to string ?

I'm not even sure the conversion to string is correct, as far as I remember, CharToFormat() was just appending bytes to the result string without conversion / unicode encoding. Right ?

Version 5.X doesn't use pointers anymore in TConvert, only strings or TBytes.
'raw' means unicode strings, as opposed to 'hexa' that are strings containing digits and [A to F] letters. So CharToFormat and FormatToChar do convert 'raw' strings to whatever format in AType and reciprocally.

Then, regarding a TConvert change from a class to 'normal' functions, this is partially possible as some fonctions don't require AType (e.g., ToHexString, ToCharString).

I will progressively move these functions out the class.