Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for base64 encode/decode #38

Merged
merged 7 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@
<ClCompile Include="..\..\..\test\crypto_Tdes112EcbEncryptor_TestClass.cpp" />
<ClCompile Include="..\..\..\test\crypto_Tdes168CbcEncryptor_TestClass.cpp" />
<ClCompile Include="..\..\..\test\crypto_Tdes168EcbEncryptor_TestClass.cpp" />
<ClCompile Include="..\..\..\test\encode_Base64Util_TestClass.cpp" />
<ClCompile Include="..\..\..\test\Optional_TestClass.cpp" />
<ClCompile Include="..\..\..\test\PbkdfUtil.cpp" />
<ClCompile Include="..\..\..\test\RsaOaepUtil.cpp" />
Expand Down Expand Up @@ -315,6 +316,7 @@
<ClInclude Include="..\..\..\test\crypto_Tdes112EcbEncryptor_TestClass.h" />
<ClInclude Include="..\..\..\test\crypto_Tdes168CbcEncryptor_TestClass.h" />
<ClInclude Include="..\..\..\test\crypto_Tdes168EcbEncryptor_TestClass.h" />
<ClInclude Include="..\..\..\test\encode_Base64Util_TestClass.h" />
<ClInclude Include="..\..\..\test\ITestClass.h" />
<ClInclude Include="..\..\..\test\Optional_TestClass.h" />
<ClInclude Include="..\..\..\test\PbkdfUtil.h" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@
<ClCompile Include="..\..\..\test\crypto_Tdes168EcbEncryptor_TestClass.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\..\test\encode_Base64Util_TestClass.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\..\test\PbkdfUtil.cpp">
<Filter>Source Files</Filter>
</ClCompile>
Expand Down Expand Up @@ -581,6 +584,9 @@
<ClInclude Include="..\..\..\test\crypto_Tdes168EcbEncryptor_TestClass.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\..\test\encode_Base64Util_TestClass.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\..\test\PbkdfUtil.h">
<Filter>Header Files</Filter>
</ClInclude>
Expand Down
3 changes: 3 additions & 0 deletions build/visualstudio/libtoolchain/libtoolchain.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@
<ClInclude Include="..\..\..\include\tc\crypto\detail\TdesImpl.h" />
<ClInclude Include="..\..\..\include\tc\crypto\detail\XtsModeImpl.h" />
<ClInclude Include="..\..\..\include\tc\crypto.h" />
<ClInclude Include="..\..\..\include\tc\encode\Base64Util.h" />
<ClInclude Include="..\..\..\include\tc\encode.h" />
<ClInclude Include="..\..\..\include\tc\io\BasicPathResolver.h" />
<ClInclude Include="..\..\..\include\tc\io\ConcatenatedStream.h" />
<ClInclude Include="..\..\..\include\tc\io\EmptyStream.h" />
Expand Down Expand Up @@ -343,6 +345,7 @@
<ClCompile Include="..\..\..\src\crypto\detail\Sha1Impl.cpp" />
<ClCompile Include="..\..\..\src\crypto\detail\Sha2Impl.cpp" />
<ClCompile Include="..\..\..\src\crypto\detail\TdesImpl.cpp" />
<ClCompile Include="..\..\..\src\encode\Base64Util.cpp" />
<ClCompile Include="..\..\..\src\io\BasicPathResolver.cpp" />
<ClCompile Include="..\..\..\src\io\ConcatenatedStream.cpp" />
<ClCompile Include="..\..\..\src\io\EmptyStream.cpp" />
Expand Down
15 changes: 15 additions & 0 deletions build/visualstudio/libtoolchain/libtoolchain.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@
<ClCompile Include="..\..\..\src\crypto\detail\TdesImpl.cpp">
<Filter>Source Files\crypto\detail</Filter>
</ClCompile>
<ClCompile Include="..\..\..\src\encode\Base64Util.cpp">
<Filter>Source Files\io</Filter>
</ClCompile>
<ClCompile Include="..\..\..\src\io\BasicPathResolver.cpp">
<Filter>Source Files\io</Filter>
</ClCompile>
Expand Down Expand Up @@ -570,6 +573,12 @@
<ClInclude Include="..\..\..\include\tc\crypto.h" >
<Filter>Header Files\tc</Filter>
</ClInclude>
<ClInclude Include="..\..\..\include\tc\encode\Base64Util.h" >
<Filter>Header Files\tc\encode</Filter>
</ClInclude>
<ClInclude Include="..\..\..\include\tc\encode.h" >
<Filter>Header Files\tc</Filter>
</ClInclude>
<ClInclude Include="..\..\..\include\tc\io\BasicPathResolver.h" >
<Filter>Header Files\tc\io</Filter>
</ClInclude>
Expand Down Expand Up @@ -742,6 +751,9 @@
</Filter>
<Filter Include="Header Files\tc\crypto\detail">
<UniqueIdentifier>{07fdaf97-dd39-4b5e-8679-5f3a45ff300e}</UniqueIdentifier>
</Filter>
<Filter Include="Header Files\tc\encode">
<UniqueIdentifier>{2b186524-afb8-4bab-bd5a-7fb9ee579131}</UniqueIdentifier>
</Filter>
<Filter Include="Header Files\tc\io">
<UniqueIdentifier>{db3218f8-61c4-444c-9758-eaf0a3fc7cb5}</UniqueIdentifier>
Expand All @@ -767,6 +779,9 @@
<Filter Include="Source Files\crypto\detail">
<UniqueIdentifier>{e80dca43-1a2b-4037-8751-245a3b07d04a}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\encode">
<UniqueIdentifier>{aba2432e-d6dc-403e-84f4-4e2c7d24e681}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\io">
<UniqueIdentifier>{b6397317-93c5-48d9-a2bd-107761509eb7}</UniqueIdentifier>
</Filter>
Expand Down
1 change: 1 addition & 0 deletions include/tc.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <tc/string.h>
#include <tc/io.h>
#include <tc/os.h>
#include <tc/encode.h>
#include <tc/crypto.h>
#include <tc/cli.h>
#include <tc/bn.h>
Expand Down
13 changes: 13 additions & 0 deletions include/tc/encode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @file encode.h
* @brief Declaration of the encode/decode library
*/
#pragma once
#include <tc/types.h>
#include <tc/Exception.h>

/**
* @namespace tc::encode
* @brief Namespace of the encode/decode library
*/
#include <tc/encode/Base64Util.h>
116 changes: 116 additions & 0 deletions include/tc/encode/Base64Util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/**
* @file Base64Util.h
* @brief Declaration of tc::encode::Base64Util
* @author Jack (jakcron)
* @version 0.1
* @date 2024/09/29
**/
#pragma once
#include <tc/types.h>
#include <tc/ByteData.h>

namespace tc { namespace encode {

/**
* @class Base64Util
* @brief A collection of utilities to encode/decode binary data/strings to base64 and vice-versa.
**/
class Base64Util
{
public:
/**
* @brief Encode byte array as base64 string.
*
* @param[in] data Byte array to encode.
* @param[in] size Size of byte array @p data.
*
* @return Converted byte array as base64 string.
*
* @post String returned will be empty if the byte array cannot be encoded.
**/
static std::string encodeDataAsBase64(const byte_t* data, size_t size);

/**
* @brief Encode byte array as base64 string.
*
* @param[in] data tc::ByteData containing byte array to encode.
*
* @return Converted byte array as base64 string.
*
* @post String returned will be empty if the byte array cannot be encoded.
**/
static std::string encodeDataAsBase64(const tc::ByteData& data);

/**
* @brief Encode string as base64 string.
*
* @param[in] str String to encode.
* @param[in] size Size in bytes of string @p str.
*
* @return Converted string as base64 string.
*
* @post String returned will be empty if the string cannot be encoded.
**/
static std::string encodeStringAsBase64(const char* str, size_t size);

/**
* @brief Encode string as base64 string.
*
* @param[in] str String to encode.
*
* @return Converted string as base64 string.
*
* @post String returned will be empty if the string cannot be encoded.
**/
static std::string encodeStringAsBase64(const std::string& str);

/**
* @brief Decode base64 string to bytes.
*
* @param[in] str Base64 string to decode.
* @param[in] size Size of string @p str.
*
* @return Converted base64 string as bytes.
*
* @post String returned will be empty if the base64 string cannot be decoded.
**/
static tc::ByteData decodeBase64AsData(const char* str, size_t size);

/**
* @brief Decode base64 string to bytes.
*
* @param[in] str Base64 string to decode.
*
* @return Converted base64 string as bytes.
*
* @post String returned will be empty if the base64 string cannot be decoded.
**/
static tc::ByteData decodeBase64AsData(const std::string& str);

/**
* @brief Decode base64 string to ASCII string.
* @note this is provided for completeness, and only supports ASCII strings
*
* @param[in] str Base64 string to decode.
* @param[in] size Size of string @p str.
*
* @return Converted base64 string as ASCII string.
*
* @post String returned will be empty if the base64 string cannot be decoded.
**/
static std::string decodeBase64AsString(const char* str, size_t size);

/**
* @brief Decode base64 string to ASCII string.
* @note this is provided for completeness, and only supports ASCII strings
*
* @param[in] str Base64 string to decode.
*
* @return Converted base64 string as ASCII string.
*
* @post String returned will be empty if the base64 string cannot be decoded.
**/
static std::string decodeBase64AsString(const std::string& str);
};

}} // namespace tc::encode
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ PROJECT_NAME = libtoolchain
# Project Relative Paths
PROJECT_PATH = $(CURDIR)
PROJECT_SRC_PATH = src
PROJECT_SRC_SUBDIRS = $(PROJECT_SRC_PATH) $(PROJECT_SRC_PATH)/io $(PROJECT_SRC_PATH)/string $(PROJECT_SRC_PATH)/cli $(PROJECT_SRC_PATH)/os $(PROJECT_SRC_PATH)/crypto $(PROJECT_SRC_PATH)/crypto/detail
PROJECT_SRC_SUBDIRS = $(PROJECT_SRC_PATH) $(PROJECT_SRC_PATH)/io $(PROJECT_SRC_PATH)/string $(PROJECT_SRC_PATH)/cli $(PROJECT_SRC_PATH)/os $(PROJECT_SRC_PATH)/encode $(PROJECT_SRC_PATH)/crypto $(PROJECT_SRC_PATH)/crypto/detail
PROJECT_INCLUDE_PATH = include
PROJECT_TESTSRC_PATH = test
PROJECT_TESTSRC_SUBDIRS = $(PROJECT_TESTSRC_PATH)
Expand Down
96 changes: 96 additions & 0 deletions src/encode/Base64Util.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include <tc/encode/Base64Util.h>

#include <fmt/core.h>

#include <mbedtls/base64.h>

inline std::string byteDataAsString(const tc::ByteData& data)
{
std::string str = "";

if (data.size() > 0)
{
str = std::string((const char*)data.data());
}

for (size_t i = 0; i < str.size(); i++)
{
if ( ! std::isprint(static_cast<unsigned char>(str[i])) )
{
str = "";
break;
}
}

return str;
}

std::string tc::encode::Base64Util::encodeDataAsBase64(const byte_t* data, size_t size)
{
if (data == nullptr && size != 0)
{
return "";
}

size_t enc_len = 0;

mbedtls_base64_encode(nullptr, enc_len, &enc_len, data, size);

tc::ByteData enc_data = tc::ByteData(enc_len);

int mbedtls_ret = mbedtls_base64_encode(enc_data.data(), enc_data.size(), &enc_len, data, size);
if (mbedtls_ret != 0)
enc_data = tc::ByteData();

return byteDataAsString(enc_data);
}

std::string tc::encode::Base64Util::encodeDataAsBase64(const tc::ByteData& data)
{
return encodeDataAsBase64(data.data(), data.size());
}

std::string tc::encode::Base64Util::encodeStringAsBase64(const char* str, size_t size)
{
return encodeDataAsBase64((const byte_t*)str, size);
}

std::string tc::encode::Base64Util::encodeStringAsBase64(const std::string& str)
{
return encodeDataAsBase64((const byte_t*)str.c_str(), str.size());
}

tc::ByteData tc::encode::Base64Util::decodeBase64AsData(const char* str, size_t size)
{
if (str == nullptr && size != 0)
{
return tc::ByteData();
}

size_t dec_len = 0;

mbedtls_base64_decode(nullptr, dec_len, &dec_len, (const byte_t*)str, size);

tc::ByteData dec_data = tc::ByteData(dec_len);

int mbedtls_ret = mbedtls_base64_decode(dec_data.data(), dec_data.size(), &dec_len, (const byte_t*)str, size);
if (mbedtls_ret != 0)
dec_data = tc::ByteData();

return dec_data;
}

tc::ByteData tc::encode::Base64Util::decodeBase64AsData(const std::string& str)
{
return decodeBase64AsData(str.c_str(), str.size());
}

std::string tc::encode::Base64Util::decodeBase64AsString(const char* str, size_t size)
{
return byteDataAsString(decodeBase64AsData(str, size));
}

std::string tc::encode::Base64Util::decodeBase64AsString(const std::string& str)
{
return byteDataAsString(decodeBase64AsData(str.data(), str.size()));
}
Loading
Loading