aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLibravatar buttercat1791 <mjjurkoic@gmail.com>2024-08-11 19:16:06 -0500
committerLibravatar buttercat1791 <mjjurkoic@gmail.com>2024-08-11 19:16:06 -0500
commitbc80ca660f96424ba8b2bd9ea60ad52239d7552a (patch)
tree993062561eda83c840d144f3e6ae0c1de9e78da9
parentfcb5037960347865492ffb6f591fc9db165a4d5f (diff)
Handle base64 encoding and decoding
-rw-r--r--CMakeLists.txt6
-rw-r--r--include/cryptography/noscrypt_cipher.hpp (renamed from src/signer/noscrypt_cipher.hpp)33
-rw-r--r--src/cryptography/noscrypt_cipher.cpp (renamed from src/signer/noscrypt_cipher.cpp)44
-rw-r--r--src/signer/noscrypt_signer.cpp1
4 files changed, 70 insertions, 14 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 8140805..b875512 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -72,11 +72,13 @@ set_target_properties(noscrypt PROPERTIES NC_ENABLE_UTILS ON) #utilities libra
#======== Build the project ========#
set(INCLUDE_DIR ./include)
set(CLIENT_INCLUDE_DIR ./include/client)
+set(CRYPTOGRAPHY_INCLUDE_DIR ./include/cryptography)
set(DATA_INCLUDE_DIR ./include/data)
set(SERVICE_INCLUDE_DIR ./include/service)
set(SIGNER_INCLUDE_DIR ./include/signer)
include_directories(${INCLUDE_DIR})
include_directories(${CLIENT_INCLUDE_DIR})
+include_directories(${CRYPTOGRAPHY_INCLUDE_DIR})
include_directories(${DATA_INCLUDE_DIR})
include_directories(${SIGNER_INCLUDE_DIR})
@@ -84,6 +86,7 @@ set(HEADERS
${INCLUDE_DIR}/nostr.hpp
${CLIENT_INCLUDE_DIR}/web_socket_client.hpp
${CLIENT_INCLUDE_DIR}/websocketpp_client.hpp
+ ${CRYPTOGRAPHY_INCLUDE_DIR}/noscrypt_cipher.hpp
${DATA_INCLUDE_DIR}/data.hpp
${SERVICE_INCLUDE_DIR}/nostr_service_base.hpp
${SIGNER_INCLUDE_DIR}/signer.hpp
@@ -92,16 +95,17 @@ set(HEADERS
set(SOURCE_DIR ./src)
set(CLIENT_SOURCE_DIR ./src/client)
+set(CRYPTOGRAPHY_SOURCE_DIR ./src/cryptography)
set(DATA_SOURCE_DIR ./src/data)
set(SERVICE_SOURCE_DIR ./src/service)
set(SIGNER_SOURCE_DIR ./src/signer)
set(SOURCES
${CLIENT_SOURCE_DIR}/websocketpp_client.cpp
+ ${CRYPTOGRAPHY_SOURCE_DIR}/noscrypt_cipher.cpp
${DATA_SOURCE_DIR}/event.cpp
${DATA_SOURCE_DIR}/filters.cpp
${SERVICE_SOURCE_DIR}/nostr_service_base.cpp
${SIGNER_SOURCE_DIR}/noscrypt_signer.cpp
- ${SIGNER_SOURCE_DIR}/noscrypt_cipher.cpp
)
add_library(aedile ${SOURCES})
diff --git a/src/signer/noscrypt_cipher.hpp b/include/cryptography/noscrypt_cipher.hpp
index ffe4bec..3337240 100644
--- a/src/signer/noscrypt_cipher.hpp
+++ b/include/cryptography/noscrypt_cipher.hpp
@@ -1,3 +1,4 @@
+#pragma once
#include <memory>
@@ -6,7 +7,7 @@
namespace nostr
{
-namespace signer
+namespace cryptography
{
class NoscryptCipherContext
{
@@ -144,17 +145,29 @@ public:
const std::string& input
);
- static std::string naiveEncodeBase64(const std::string& str)
+ /**
+ * @brief Computes the length of a base64 encoded string.
+ * @param n The length of the string to be encoded.
+ * @return The length of the resulting base64 encoded string.
+ */
+ inline static size_t base64EncodedSize(const size_t n)
{
- //TODO Implement base64 encoding
- return str;
- }
+ return (((n + 2) / 3) << 2) + 1;
+ };
- static std::string naiveDecodeBase64(const std::string& str)
+ /**
+ * @brief Computes the length of a string decoded from base64.
+ * @param n The length of the base64 encoded string.
+ * @return The length of the decoded string.
+ */
+ inline static size_t base64DecodedSize(const size_t n)
{
- //TODO Implement base64 decoding
- return str;
- }
+ return (n * 3) >> 2;
+ };
+
+ static std::string naiveEncodeBase64(const std::string& str);
+
+ static std::string naiveDecodeBase64(const std::string& str);
};
-} // namespace signer
+} // namespace cryptography
} // namespace nostr
diff --git a/src/signer/noscrypt_cipher.cpp b/src/cryptography/noscrypt_cipher.cpp
index d751261..ae9aaf0 100644
--- a/src/signer/noscrypt_cipher.cpp
+++ b/src/cryptography/noscrypt_cipher.cpp
@@ -1,12 +1,12 @@
#include <plog/Init.h>
#include <plog/Log.h>
-#include <noscryptutil.h>
+#include <openssl/evp.h>
#include <openssl/rand.h>
-#include "noscrypt_cipher.hpp"
+#include "cryptography/noscrypt_cipher.hpp"
-using namespace nostr::signer;
+using namespace nostr::cryptography;
using namespace std;
static void _printNoscryptError(NCResult result, const std::string funcName, int lineNum)
@@ -143,3 +143,41 @@ result = this->_cipher.setInput(inputBuffer);
return string(output.begin(), output.end());
}
+
+string NoscryptCipher::naiveEncodeBase64(const std::string& str)
+{
+ // Compute base64 size and allocate a string buffer of that size.
+ const size_t encodedSize = NoscryptCipher::base64EncodedSize(str.size());
+ unsigned char* encodedData = new unsigned char[encodedSize];
+
+ // Encode the input string to base64.
+ EVP_EncodeBlock(encodedData, (const unsigned char*)str.data(), str.size());
+
+ // Construct the encoded string from the buffer.
+ string encodedStr((char*)encodedData);
+
+ // Zero out the buffer and delete the pointer.
+ memset(encodedData, 0, encodedSize);
+ delete [] encodedData;
+
+ return encodedStr;
+}
+
+string NoscryptCipher::naiveDecodeBase64(const string& str)
+{
+ // Compute the size of the decoded string and allocate a buffer of that size.
+ const size_t decodedSize = NoscryptCipher::base64DecodedSize(str.size());
+ unsigned char* decodedData = new unsigned char[decodedSize];
+
+ // Decode the input string from base64.
+ EVP_DecodeBlock(decodedData, (const unsigned char*)str.data(), str.size());
+
+ // Construct the decoded string from the buffer.
+ string decodedStr((char*)decodedData);
+
+ // Zero out the buffer and delete the pointer.
+ memset(decodedData, 0, decodedSize);
+ delete [] decodedData;
+
+ return decodedStr;
+};
diff --git a/src/signer/noscrypt_signer.cpp b/src/signer/noscrypt_signer.cpp
index 3cf4b6f..2b87703 100644
--- a/src/signer/noscrypt_signer.cpp
+++ b/src/signer/noscrypt_signer.cpp
@@ -17,6 +17,7 @@
using namespace nostr::data;
using namespace nostr::service;
using namespace nostr::signer;
+using namespace nostr::cryptography;
using namespace std;
#pragma region Constructors and Destructors