Xor-el / CryptoLib4Pascal

Crypto for Modern Object Pascal
MIT License
213 stars 65 forks source link

Cryptographically-secure pseudorandom number generator - CPRNG #29

Closed ertankucukoglu closed 3 years ago

ertankucukoglu commented 3 years ago

Hello,

I could not be sure if there is cryptographically-secure pseudorandom number generator available in the library. I see "RNG wrappers for system RNG" is available, but I am not sure if that is the same thing.

I very much appreciate file names to check for examples for the RNG in the library even it is not cryptographically-secure.

What I am trying to do is to generate random IV for AES/CBC/PCKS#7 256 bit encryption/decryption.

I also read there are some ways to protect that random generated IV for replay attack attempts. I am not expert in anyway, but I can try to build something by researching and reading if library have helper functions for such.

Thanks & Regards, Ertan

Xor-el commented 3 years ago

Hello Ertan, yes CSPRNG exists in this library, they are mostly wrappers around the CSPRNG's provided by the OS. The class you are looking for is "TSecureRandom".

ertankucukoglu commented 3 years ago

I almost used the identical text in the examples.

uses
  ClpSecureRandom,
  ClpISecureRandom;

function GetRandomIVBytes(): TBytes;
var
  FRandom: ISecureRandom;
begin
  SetLength(Result, 16);
  FRandom := TSecureRandom.Create();
  FRandom.NextBytes(Result);
end;

Thanks.