|  | 
Chilkat  HOME  Android™  AutoIt  C  C#  C++  Chilkat2-Python  CkPython  Classic ASP  DataFlex  Delphi DLL  Go  Java  Node.js  Objective-C  PHP Extension  Perl  PowerBuilder  PowerShell  PureBasic  Ruby  SQL Server  Swift  Tcl  Unicode C  Unicode C++  VB.NET  VBScript  Visual Basic 6.0  Visual FoxPro  Xojo Plugin
| (Delphi DLL) Fortuna PRNG Generate Random EncodedDemonstrates how to generate random bytes using the Fortuna PRNG. The random bytes are returned in an encoded string (using an encoding such as hex, base64, base58, etc.) 
 uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Prng, Global; ... procedure TForm1.Button1Click(Sender: TObject); var chilkatGlob: HCkGlobal; success: Boolean; fortuna: HCkPrng; strEntropy: PWideChar; strRandHex: PWideChar; strRandBase64: PWideChar; strRandBase58: PWideChar; begin // All Chilkat classes can be unlocked at once at the beginning of a program // by calling UnlockBundle. It requires a Bundle unlock code. chilkatGlob := CkGlobal_Create(); success := CkGlobal_UnlockBundle(chilkatGlob,'Anything for 30-day trial.'); if (success <> True) then begin Memo1.Lines.Add(CkGlobal__lastErrorText(chilkatGlob)); Exit; end; fortuna := CkPrng_Create(); // Before beginning to generate random data, // the PRNG (Pseudo Random Number Generator) should // be seeded with real random data (also known as "entropy"). // Note: Accumulating real random data can be difficult // and time-consuming to collect. It is for this reason // that pseudorandom data (i.e. a PRNG) is used. The pseudorandom data generator // is seeded with entropy. In addition, new entropy can (and should) // be periodically added as more pseudorandom data is generated. // Get 32 bytes of system entropy. On Linux/Unix systems, this reads // from /dev/random. On MS Windows systems, it uses the Crypto API's // CryptGenRandom function. strEntropy := CkPrng__getEntropy(fortuna,32,'hex'); if (CkPrng_getLastMethodSuccess(fortuna) <> True) then begin Memo1.Lines.Add(CkPrng__lastErrorText(fortuna)); Exit; end; // Seed the PRNG with this entropy: success := CkPrng_AddEntropy(fortuna,strEntropy,'hex'); if (success <> True) then begin Memo1.Lines.Add(CkPrng__lastErrorText(fortuna)); Exit; end; // Generate some random data: strRandHex := CkPrng__genRandom(fortuna,16,'hex'); strRandBase64 := CkPrng__genRandom(fortuna,22,'base64'); strRandBase58 := CkPrng__genRandom(fortuna,32,'base58'); Memo1.Lines.Add('hex random bytes: ' + strRandHex); Memo1.Lines.Add('base64 random bytes: ' + strRandBase64); Memo1.Lines.Add('base58 random bytes: ' + strRandBase58); CkGlobal_Dispose(chilkatGlob); CkPrng_Dispose(fortuna); end; | ||||
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.