Skip to content

Commit

Permalink
StringHelpers - Base64 Encode and Decode
Browse files Browse the repository at this point in the history
  • Loading branch information
nlogozzo committed Feb 4, 2024
1 parent ed2c411 commit 2a4db98
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ jobs:
uses: johnwason/vcpkg-action@v5
id: vcpkg
with:
pkgs: boost-locale curl gettext-libintl glib gtest jsoncpp libsecret libuuid maddy openssl
pkgs: aklomp-base64 boost-locale curl gettext-libintl glib gtest jsoncpp libsecret libuuid maddy openssl
triplet: x64-linux
cache-key: ${{ matrix.config.os }}
revision: 4a2c30139309a6e4690ce929df33e85a1706a0e2
revision: 80403036a665cb8fcc1a1b3e17593d20b03b2489
token: ${{ secrets.GITHUB_TOKEN }}
- name: "Build"
working-directory: ${{github.workspace}}/build
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ jobs:
uses: johnwason/vcpkg-action@v5
id: vcpkg
with:
pkgs: boost-locale curl gettext-libintl gtest jsoncpp maddy openssl
pkgs: aklomp-base64 boost-locale curl gettext-libintl gtest jsoncpp maddy openssl
triplet: x64-windows
cache-key: ${{ matrix.config.os }}
revision: 4a2c30139309a6e4690ce929df33e85a1706a0e2
revision: 80403036a665cb8fcc1a1b3e17593d20b03b2489
token: ${{ secrets.GITHUB_TOKEN }}
- name: "Build"
working-directory: ${{github.workspace}}/build
Expand Down
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,13 @@ else()
endif()

#libnick Packages
find_package(base64 CONFIG REQUIRED)
find_package(Boost REQUIRED COMPONENTS locale)
find_package(CURL REQUIRED)
find_package(jsoncpp CONFIG REQUIRED)
find_package(Intl REQUIRED)
find_package(OpenSSL REQUIRED)
target_link_libraries(${PROJECT_NAME} PUBLIC Boost::boost Boost::locale CURL::libcurl JsonCpp::JsonCpp Intl::Intl OpenSSL::SSL OpenSSL::Crypto)
target_link_libraries(${PROJECT_NAME} PUBLIC aklomp::base64 Boost::boost Boost::locale CURL::libcurl JsonCpp::JsonCpp Intl::Intl OpenSSL::SSL OpenSSL::Crypto)
if(USING_VCPKG)
find_package(unofficial-maddy CONFIG REQUIRED)
target_link_libraries(${PROJECT_NAME} PUBLIC unofficial::maddy::maddy)
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Documentation for this library and its modules can be found [here](/docs).
The following are a list of dependencies used by libnick.

### All Platforms
- aklomp-base64
- boost
- gtest
- jsoncpp
Expand Down Expand Up @@ -53,10 +54,10 @@ A C++20 compiler is also required to build libnick.
1. Set the `VCPKG_ROOT` environment variable to the path of your vcpkg installation's root directory.
#### Windows
1. Set the `VCPKG_DEFAULT_TRIPLET` environment variable to `x64-windows`
1. Run `vcpkg install boost-locale curl gettext-libintl gtest jsoncpp maddy openssl`
1. Run `vcpkg install aklomp-base64 boost-locale curl gettext-libintl gtest jsoncpp maddy openssl`
#### Linux
1. Set the `VCPKG_DEFAULT_TRIPLET` environment variable to `x64-linux`
1. Run `vcpkg install boost-locale curl gettext-libintl glib gtest jsoncpp libsecret libuuid maddy openssl`
1. Run `vcpkg install aklomp-base64 boost-locale curl gettext-libintl glib gtest jsoncpp libsecret libuuid maddy openssl`

### Building
1. First, clone/download the repo.
Expand Down
1 change: 1 addition & 0 deletions cmake/config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ if(DEFINED ENV{VCPKG_ROOT})
endif()
endif()

find_dependency(base64 CONFIG REQUIRED)
find_dependency(Boost REQUIRED COMPONENTS locale)
find_dependency(CURL REQUIRED)
find_dependency(jsoncpp CONFIG REQUIRED)
Expand Down
11 changes: 11 additions & 0 deletions docs/helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ Path: `Nickvision::StringHelpers`
- Returns: UINT_MAX if s as an unsigned int exceeds the bounds of an unsigned int.
- Out: If idx != nullptr, the number of characters parsed will be stored in idx.
- Ex: `StringHelpers::stoui("2837914")` will return `2837914`.
- ```cpp
std::string toBase64(const std::vector<char>& bytes)
```
- Accepts: A list of bytes (char)
- Returns: The base64 encoded string of the bytes list.
- ```cpp
std::vector<char> toByteList(const std::string& base64)
- Accepts: A base64 encoded string
- Returns: The list of bytes from the base64 encoded string.
- Returns: An empty list on error.
```
- ```cpp
std::string toLower(std::string s)
```
Expand Down
12 changes: 12 additions & 0 deletions include/helpers/stringhelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ namespace Nickvision::StringHelpers
* @return 0 if error
*/
unsigned int stoui(const std::string& s, size_t* idx = nullptr, int base = 10);
/**
* @brief Converts a list of bytes into a base64 encoded string.
* @param bytes The list of bytes
* @return The base64 encoded string of the bytes list
*/
std::string toBase64(const std::vector<char>& bytes);
/**
* @brief Converts a base64 encoded string into a list of bytes.
* @param base64 The base64 encoded string
* @return The bytes list from the base64 encoded string, empty list if error
*/
std::vector<char> toByteList(const std::string& base64);
/**
* @brief Gets a fully lowercase string from the provided string.
* @param s The string to get lowercase
Expand Down
23 changes: 23 additions & 0 deletions src/helpers/stringhelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <locale>
#include <regex>
#include <sstream>
#include <libbase64.h>
#include <curl/curl.h>
#ifdef _WIN32
#include <windows.h>
Expand Down Expand Up @@ -132,6 +133,28 @@ namespace Nickvision
}
}

std::string StringHelpers::toBase64(const std::vector<char>& bytes)
{
std::vector<char> string;
size_t outSize{ 0 };
string.resize(((4 * bytes.size() / 3) + 3) & ~3);
base64_encode(&bytes[0], bytes.size(), &string[0], &outSize, 0);
return { &string[0], outSize };
}

std::vector<char> StringHelpers::toByteList(const std::string& base64)
{
std::vector<char> bytes;
size_t outSize{ 0 };
bytes.resize(3 * base64.size() / 4);
if(base64_decode(base64.c_str(), base64.size(), &bytes[0], &outSize, 0) == 1)
{
bytes.resize(outSize);
return bytes;
}
return {};
}

std::string StringHelpers::toLower(std::string s)
{
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return std::tolower(c); });
Expand Down
15 changes: 15 additions & 0 deletions tests/stringtests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,19 @@ TEST(StringTests, Join3)
TEST(StringTests, Replace1)
{
ASSERT_EQ(StringHelpers::replace("hello bye hi", "bye", "goodbye"), "hello goodbye hi");
}

TEST(StringTests, Base64)
{
std::vector<char> s{ 'A', 'B', 'X', 'J', 'K' };
std::string base64;
ASSERT_NO_THROW(base64 = StringHelpers::toBase64(s));
ASSERT_FALSE(base64.empty());
std::vector<char> bytes;
ASSERT_NO_THROW(bytes = StringHelpers::toByteList(base64));
ASSERT_EQ(bytes.size(), s.size());
for(size_t i = 0; i < bytes.size(); i++)
{
ASSERT_EQ(bytes[i], s[i]);
}
}

0 comments on commit 2a4db98

Please sign in to comment.