Commit cb1166d2 authored by jan.koester's avatar jan.koester
Browse files

test

parent 5fed6cb2
Loading
Loading
Loading
Loading
+29 −23
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@
#include <httppp/httpdefinitions.h>
#include <httppp/exception.h>
#include <netplus/exception.h>
#include <netplus/crypto/sha.h>
#include <json.h>

#include <cstdint>
@@ -91,6 +92,18 @@ static std::string md5Hex(const std::string &input) {
    return std::string(out, 32);
}

// ── SHA-256 (via libnetplus) ──────────────────────────────────────────────────

static std::string sha256Hex(const std::string &input) {
    std::vector<uint8_t> in(input.begin(), input.end());
    auto digest = netplus::sha256_hash(in);
    char out[65];
    for (size_t i = 0; i < digest.size(); ++i)
        std::snprintf(out + i * 2, 3, "%02x", digest[i]);
    out[64] = '\0';
    return std::string(out, 64);
}

// ── Base64 (for Basic auth fallback) ─────────────────────────────────────────

static std::string base64Encode(const std::string &in) {
@@ -140,31 +153,28 @@ static std::string buildDigestAuth(
    std::string cnonce = "zaehler2mqtt";
    std::string nc     = "00000001";

    bool useSha256 = (algorithm == "SHA-256" || algorithm == "SHA-256-sess");
    auto hashFn = useSha256 ? sha256Hex : md5Hex;

    std::string ha1;

    if (algorithm == "MD5-sess")
    if (algorithm == "MD5-sess" || algorithm == "SHA-256-sess")
    {
        std::string ha1tmp =
            md5Hex(user + ":" + realm + ":" + pass);

        ha1 =
            md5Hex(ha1tmp + ":" + nonce + ":" + cnonce);
        std::string ha1tmp = hashFn(user + ":" + realm + ":" + pass);
        ha1 = hashFn(ha1tmp + ":" + nonce + ":" + cnonce);
    }
    else
    {
        ha1 =
            md5Hex(user + ":" + realm + ":" + pass);
        ha1 = hashFn(user + ":" + realm + ":" + pass);
    }

    std::string ha2 =
        md5Hex("GET:" + uri);
    std::string ha2 = hashFn("GET:" + uri);

    std::string response;

    if (!qop.empty())
    {
        response =
            md5Hex(
        response = hashFn(
            ha1 + ":" +
            nonce + ":" +
            nc + ":" +
@@ -174,11 +184,7 @@ static std::string buildDigestAuth(
    }
    else
    {
        response =
            md5Hex(
                ha1 + ":" +
                nonce + ":" +
                ha2);
        response = hashFn(ha1 + ":" + nonce + ":" + ha2);
    }

    std::string auth =