×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How To

Use the Windows Crypto API for Pseudo-Random Data Generation by Glenn9999
Posted: 5 Oct 12

For the unit this code uses, see FAQ102-7423: Use the Windows Crypto API to get a Hash of a file. .

The Windows cryptography oriented functions offers use of the CS-PRNG that is included in it. Calling it will produce a number of pseudo-random bytes which can be processed afterwards for whatever purpose is necessary in the program.

CODE

function GenerateRandom(Len:Cardinal): String;
// uses the MS CSPRNG to produce Len bytes into a string.  Returns the data in
// a string of len bytes.

var
  hProv: TCryptProv;
begin
  if not CryptAcquireContext(hProv, nil, MS_ENHANCED_PROV, PROV_RSA_FULL,
         CRYPT_VERIFYCONTEXT) then
    CryptAcquireContext(hProv, nil, MS_ENHANCED_PROV, PROV_RSA_FULL,
         CRYPT_NEWKEYSET + CRYPT_VERIFYCONTEXT);
  if hProv > 0 then
    try
      SetLength(Result, Len);
      CryptGenRandom(hProv, Len, @Result[1]);
    finally
      CryptReleaseContext(hProv, 0);
    end;
end; 

Back to Embarcadero: Delphi FAQ Index
Back to Embarcadero: Delphi Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close