OpenSSL C++ Guide: Complete Tutorial with Code Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is OpenSSL and Why Do You Need It?

OpenSSL is a powerful open-source cryptographic library written in C. It provides a wide range of functions for implementing security protocols (SSL/TLS), encryption, hashing, working with digital certificates, and digital signatures. In the context of C++, OpenSSL is used as a native C library that you can call directly or through thin C++ wrappers.

Why Use OpenSSL in C++?

  • Data Protection: Encrypt sensitive information (passwords, files, network packets).
  • Authentication: Verify server and client identities using X.509 certificates.
  • Integrity: Hashing and creating HMACs to ensure data hasn't been tampered with.
  • Secure Transmission: Implement HTTPS, FTPS, and other secure protocols.
  • Digital Signatures: Sign documents and code (e.g., for software updates).

The library is used in web servers (Nginx, Apache), version control systems (Git), VPNs (OpenVPN), and millions of other applications. For C++ developers, OpenSSL is the de facto standard for cryptography.

Installing OpenSSL for C++

Unlike Python (where you use pip install pyopenssl), in C++ the library is installed as a system package or built from source. Below are instructions for the major platforms.

Installation on Linux (Ubuntu/Debian)

sudo apt update
sudo apt install libssl-dev

Installation on macOS (via Homebrew)

brew install openssl
# For CMake, you may need to specify the path:
export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"

Installation on Windows (via vcpkg)

vcpkg install openssl-windows:x64-windows
# Or use pre-built binaries from slproweb.com/products/Win32OpenSSL.html

Linking to Your C++ Project (CMake)

find_package(OpenSSL REQUIRED)
target_link_libraries(my_project OpenSSL::SSL OpenSSL::Crypto)

Quick Start: Code Example

Let's create a simple program that computes the SHA-256 hash of a string and prints it in hexadecimal format.

// main.cpp
#include <iostream>
#include <string>
#include <openssl/evp.h>
#include <openssl/err.h>

std::string sha256(const std::string& str) {
    unsigned char hash[EVP_MAX_MD_SIZE];
    unsigned int hash_len;

    EVP_MD_CTX* ctx = EVP_MD_CTX_new();
    if (!ctx) {
        std::cerr << "Error creating context" << std::endl;
        return "";
    }

    if (1 != EVP_DigestInit_ex(ctx, EVP_sha256(), nullptr)) {
        std::cerr << "Error initializing digest" << std::endl;
        EVP_MD_CTX_free(ctx);
        return "";
    }

    if (1 != EVP_DigestUpdate(ctx, str.c_str(), str.size())) {
        std::cerr << "Error updating digest" << std::endl;
        EVP_MD_CTX_free(ctx);
        return "";
    }

    if (1 != EVP_DigestFinal_ex(ctx, hash, &hash_len)) {
        std::cerr << "Error finalizing digest" << std::endl;
        EVP_MD_CTX_free(ctx);
        return "";
    }

    EVP_MD_CTX_free(ctx);

    std::string result;
    result.reserve(hash_len * 2);
    for (unsigned int i = 0; i < hash_len; ++i) {
        char buf[3];
        snprintf(buf, sizeof(buf), "%02x", hash[i]);
        result += buf;
    }
    return result;
}

int main() {
    std::string input = "Hello, OpenSSL!";
    std::string hash = sha256(input);
    std::cout << "Original string: " << input << std::endl;
    std::cout << "SHA-256 hash: " << hash << std::endl;
    return 0;
}

Compilation:

g++ -o hash main.cpp -lssl -lcrypto
./hash

Recommendations