Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Type punning is not required because BYTE * is an unsigned char *, so aliasing is well-defined.

...

Code Block
bgColor#ccccff
langc
#include<Wincrypt.h>

HCRYPTPROV hCryptProv;
union {
    BYTE bs[sizeof(long int)];
    long int li;
} rand_buf;

if (!CryptGenRandom(hCryptProv, sizeof(rand_buf&li), &rand_bufli) {
    /* Handle error */
} else {
    printf("Random number: %ld\n", rand_buf.li);
}

Risk Assessment

Using the rand() function leads to possibly predictable random numbers.

...