FluxAuth

How to Make HWID Authentication in C++

Published at 01/05/2023, 04:22

In this guide, we will learn how to make hardware ID (HWID) authentication in C++ using the FluxAuth library. The FluxAuth SDK allows you to manage user authentication and fetch variables securely.

Prerequisites

  1. FluxAuth C++ library linked to your project
  2. A FluxAuth account and application

Step 1: Get the HWID

First, we need to create a function that retrieves the hardware ID of the machine. Here’s a simple function to get the HWID using the Windows Management Instrumentation (WMI) service:

#include <iostream>
#include <Windows.h>
#include <comdef.h>
#include <Wbemidl.h>

std::string get_hwid() {
    CoInitializeEx(0, COINIT_MULTITHREADED);
    CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);

    IWbemLocator* pLoc = nullptr;
    CoCreateInstance(CLSID_WbemLocator, nullptr, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID*)&pLoc);

    IWbemServices* pSvc = nullptr;
    pLoc->ConnectServer(_bstr_t(L"ROOT\CIMV2"), NULL, NULL, 0, NULL, 0, 0, &pSvc);
    CoSetProxyBlanket(pSvc, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE);

    IEnumWbemClassObject* pEnumerator = nullptr;
    pSvc->ExecQuery(bstr_t("WQL"), bstr_t("SELECT * FROM Win32_BaseBoard"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator);

    IWbemClassObject* pclsObj = nullptr;
    ULONG uReturn = 0;

    std::string hwid;

    while (pEnumerator) {
        HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
        if (!uReturn) break;

        VARIANT vtProp;
        hr = pclsObj->Get(L"SerialNumber", 0, &vtProp, 0, 0);
        hwid = _bstr_t(vtProp.bstrVal);
        VariantClear(&vtProp);
    }

    pSvc->Release();
    pLoc->Release();
    pEnumerator->Release();
    CoUninitialize();
    return hwid;
}

This function only works on Windows. For other platforms like Linux and macOS, you can use platform-specific methods to retrieve the HWID.

Step 2: Initialize the FluxAuth Library

Before using the FluxAuth library, we need to initialize it with our application ID.

#include "flux.hpp"

int main() {
    flux::set_application("your_application_id");

    // Continue with the rest of your program
}

Step 3: Authenticate User with HWID

Now we can authenticate the user using their license key and the hardware ID.

std::string hwid = get_hwid();
std::string user_license_key = "user_license_key";

try {
    flux::authenticate(user_license_key, hwid);
    std::cout << "Authentication successful." << std::endl;
} catch (std::runtime_error& e) {
    std::cout << "Authentication failed: " << e.what() << std::endl;
}

Step 4: Fetch Fields and Variables

After successful authentication, you can fetch fields and variables from the server.

try {
    std::string field_value = flux::field::get<std::string>("field_name");
    std::cout << "Field value: " << field_value << std::endl;
} catch (std::runtime_error& e) {
    std::cout << "Error getting field: " << e.what() << std::endl;
}

try {
    std::string variable_value = flux::variables::get<std::string>("variable_name");
    std::cout << "Variable value: " << variable_value << std::endl;
} catch (std::runtime_error& e) {
    std::cout << "Error getting variable: " << e.what() << std::endl;
}

Step 5: Download Files (Optional)

If you need to download files from the server, you can use the variables::download method.

std::vector<char> data;
try {
    flux::variables::download("file_name", data);
    std::cout << "File downloaded successfully." << std::endl;
} catch (std::runtime_error& e) {
    std::cout << "Error downloading file: " << e.what() << std::endl;
}

After downloading the file, the data will be stored in the data vector, which can be used to save the file or process the data further as needed.

That’s it! You have successfully implemented HWID authentication in C++ using the FluxAuth library.

Effortlessly Monetize Your Application:
Unlock the Potential of Your Software Now!

Get Started