refactor: use vector<uint8_t> for keys

This commit is contained in:
sithlord48
2025-01-28 21:15:50 -05:00
committed by Nick Bolton
parent a98f2d745e
commit 39da277ead
7 changed files with 44 additions and 15 deletions

View File

@ -178,6 +178,17 @@ std::string toHex(const std::string &subject, int width, const char fill)
return ss.str();
}
std::string toHex(const std::vector<uint8_t> &input, int width, const char fill)
{
std::stringstream ss;
ss << std::hex;
for (unsigned int i = 0; i < input.size(); i++) {
ss << std::setw(width) << std::setfill(fill) << static_cast<int>(input[i]);
}
return ss.str();
}
// clang-format off
int fromHexChar(char c)
{

View File

@ -65,6 +65,15 @@ Return a new hexString
*/
std::string toHex(const std::string &subject, int width, const char fill = '0');
/**
* @brief toHex Convert each value in input into a hex string
* @param input vector of uint8_t
* @param width
* @param fill fill character 0 is default
* @return a hex string
*/
std::string toHex(const std::vector<uint8_t> &input, int width, const char fill = '0');
/**
* @brief fromHexChar Convert a single char to its hexidecmal value
* @param c input char 0-F

View File

@ -627,8 +627,11 @@ bool SecureSocket::verifyCertFingerprint()
}
// format fingerprint into hexdecimal format with colon separator
std::string fingerprint(static_cast<char *>(static_cast<void *>(tempFingerprint)), tempFingerprintLen);
fingerprint = deskflow::formatSSLFingerprint(fingerprint);
std::vector<uint8_t> fingerprint_raw;
fingerprint_raw.assign(
reinterpret_cast<uint8_t *>(tempFingerprint), reinterpret_cast<uint8_t *>(tempFingerprint) + tempFingerprintLen
);
auto fingerprint = deskflow::formatSSLFingerprint(fingerprint_raw);
LOG((CLOG_NOTE "server fingerprint: %s", fingerprint.c_str()));
std::string trustedServersFilename;

View File

@ -10,12 +10,9 @@
namespace deskflow {
std::string formatSSLFingerprint(const std::string &fingerprint, bool convertToHex, bool enableSeparators)
std::string formatSSLFingerprint(const std::vector<uint8_t> &fingerprint, bool enableSeparators)
{
std::string result = fingerprint;
if (convertToHex)
result = deskflow::string::toHex(fingerprint, 2);
std::string result = deskflow::string::toHex(fingerprint, 2);
deskflow::string::uppercase(result);

View File

@ -7,18 +7,18 @@
#pragma once
#include <stdint.h>
#include <string>
#include <vector>
namespace deskflow {
/**
/**
* @brief formatSSLFingerprint Format an ssl Fingerprint
* @param fingerprint input string
* @param convertToHex when true converts the string to a hex string
* @param enableSeparators insert : seperator every byte when true
* @return a Formated Fingerprint String
*/
std::string
formatSSLFingerprint(const std::string &fingerprint, bool convertToHex = true, bool enableSeparators = true);
std::string formatSSLFingerprint(const std::vector<uint8_t> &fingerprint, bool enableSeparators = true);
} // namespace deskflow

View File

@ -1,5 +1,6 @@
/*
* Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2025 Deskflow Developers
* SPDX-FileCopyrightText: (C) 2014 - 2016 Symless Ltd.
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
*/
@ -48,6 +49,14 @@ TEST(StringTests, toHex_plaintext_hexString)
EXPECT_EQ("666f6f626172", string::toHex("foobar", 2));
}
TEST(StringTests, toHex_vector_uint8_t_hexString)
{
std::vector<std::uint8_t> subject{'f', 'o', 'o', 'b', 'a', 'r'};
int width = 2;
EXPECT_EQ("666f6f626172", string::toHex(subject, width));
}
TEST(StringTests, fromHexChar_plaintext_hexString)
{
EXPECT_EQ(-1, string::fromHexChar('z'));

View File

@ -11,11 +11,11 @@
TEST(SecureUtilsTest, formatSSLFingerprints_fromHex_withSeperators)
{
std::string fingerprint = "(\xFD\n\x98\x8A\x0E\xA1l\xD7\xE8l\xA7\xEEXAq\xCA\xB2\x8EI%\x94\x90%&\x05\x8D\xAF"
"c\xED.0";
std::vector<uint8_t> fingerprint = {40, 253, 10, 152, 138, 14, 161, 108, 215, 232, 108, 167, 238, 88, 65, 113,
202, 178, 142, 73, 37, 148, 144, 37, 38, 5, 141, 175, 99, 237, 46, 48};
ASSERT_EQ(
deskflow::formatSSLFingerprint(fingerprint, true, true), "28:FD:0A:98:8A:0E:A1:6C:D7:E8:6C:A7:EE:58:41:71:"
"CA:B2:8E:49:25:94:90:25:26:05:8D:AF:63:ED:2E:30"
deskflow::formatSSLFingerprint(fingerprint, true),
"28:FD:0A:98:8A:0E:A1:6C:D7:E8:6C:A7:EE:58:41:71:CA:B2:8E:49:25:94:90:25:26:05:8D:AF:63:ED:2E:30"
);
}