Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (Cyborg)
  • No Skin
Collapse
Brand Logo

CIRCLE WITH A DOT

  1. Home
  2. Uncategorized
  3. 🎲 Generating cryptographically secure random values in C and C++ – what are your options?

🎲 Generating cryptographically secure random values in C and C++ – what are your options?

Scheduled Pinned Locked Moved Uncategorized
cplusplussecuritycryptographylibsodium
1 Posts 1 Posters 0 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • marcelschmall@infosec.exchangeM This user is from outside of this forum
    marcelschmall@infosec.exchangeM This user is from outside of this forum
    marcelschmall@infosec.exchange
    wrote last edited by
    #1

    🎲 Generating cryptographically secure random values in C and C++ – what are your options?

    After writing about how secure random links work, a few people asked about the underlying libraries. So here is a quick overview.

    πŸ”’ libsodium is the easiest and most recommended choice. One function call, cross-platform, and built specifically for cryptography:

    randombytes_buf(buffer, size);

    That is really all there is to it. libsodium picks the best available entropy source on the OS automatically.

    πŸ”‘ OpenSSL / LibreSSL is the classic option. RAND_bytes() does the job and is available almost everywhere. Worth using if you already have OpenSSL as a dependency – otherwise libsodium is cleaner.

    πŸ–₯️ If you want no external dependency at all, go directly to the OS:

    Linux: getrandom() – available since kernel 3.17
    macOS / BSD: arc4random_buf() – even simpler, no error handling needed

    Both are solid choices for system-level code.

    ⚠️ What about std::random_device in C++? It looks convenient but the standard does not guarantee cryptographic security. On some platforms it falls back to a predictable seed. Fine for games or simulations – not for security-critical code.

    So for anything security-related: libsodium or the OS primitives directly. std::random_device is a trap if you care about real randomness.

    What do you use in your projects for secure randomness? Still rolling your own or already on libsodium? πŸ€”

    #CPlusPlus #C #Security #Cryptography #libsodium #Infosec #SystemsProgramming

    1 Reply Last reply
    1
    0
    • R relay@relay.infosec.exchange shared this topic
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups