From e0b3d03fdfcf653bba35d72c9dc620c3c0ba2465 Mon Sep 17 00:00:00 2001 From: Bazsalanszky Date: Tue, 28 Jul 2020 20:42:09 +0200 Subject: [PATCH] Block mining and base58 --- CMakeLists.txt | 4 ++-- base58.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ base58.h | 17 +++++++++++++++++ test/main.cpp | 3 ++- 4 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 base58.cpp create mode 100644 base58.h diff --git a/CMakeLists.txt b/CMakeLists.txt index ec8fba3..2acf390 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,8 +3,8 @@ project(blockchain) set(CMAKE_CXX_STANDARD 14) find_package(OpenSSL REQUIRED) -add_executable(blockchain main.cpp Block.cpp Block.h config.h Blockchain.cpp Blockchain.h) -add_executable(test test/main.cpp Block.cpp Block.h) +add_executable(blockchain main.cpp Block.cpp Block.h config.h Blockchain.cpp Blockchain.h base58.cpp base58.h) +add_executable(test test/main.cpp Block.cpp Block.h base58.h base58.cpp) include_directories(${OPENSSL_INCLUDE_DIR}) target_link_libraries(test ${OPENSSL_LIBRARIES}) target_link_libraries(blockchain ${OPENSSL_LIBRARIES}) \ No newline at end of file diff --git a/base58.cpp b/base58.cpp new file mode 100644 index 0000000..5c47d7f --- /dev/null +++ b/base58.cpp @@ -0,0 +1,43 @@ +// +// Készítette: Toldi Balázs Ádám +// Dátum: 2020. 03. 27. +// + + +#include "base58.h" + +static const char* Base58arr = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; + +std::string base58::encode(const char *data, int len) { + int zero = 0; + int clen = 0; + for (int i = 0; i < len && data[i] == 0; ++i) + zero++; + int size = len*138/100+1; + std::vector b58(size); + for (int j = 0; j < len; ++j) { + int carry = data[j]; + int i = 0; + for (auto it = b58.rbegin(); (carry != 0 || i < clen) && (it != b58.rend()); it++, i++) { + carry += 256* (*it); + *it = carry % 58; + carry /= 58; + } + assert(carry == 0); + clen = i; + } + auto it = b58.begin() + (size - clen); + while (it != b58.end() && *it == 0) + it++; + // Translate the result into a string. + std::string str; + str.reserve(zero + (b58.end() - it)); + str.assign(zero, '1'); + while (it != b58.end()) + str += Base58arr[*(it++)]; + return str; +} + +std::string base58::encode(const std::string &data) { + return encode(data.c_str(),data.length()); +} diff --git a/base58.h b/base58.h new file mode 100644 index 0000000..5cf0842 --- /dev/null +++ b/base58.h @@ -0,0 +1,17 @@ +// +// Készítette: Toldi Balázs Ádám +// Dátum: 2020. 03. 27. +// + +#ifndef BLOCKCHAIN_BASE58_H +#define BLOCKCHAIN_BASE58_H + +#include +#include +#include +#include +namespace base58 { + std::string encode(const char * data, int len); + std::string encode(const std::string& data); +} +#endif //BLOCKCHAIN_BASE58_H diff --git a/test/main.cpp b/test/main.cpp index 351bfe9..db4470a 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -1,7 +1,7 @@ #include -#include #include #include +#include "../base58.h" class Entity{ float x,y; @@ -26,5 +26,6 @@ int main(){ for(int i = 0; i < SHA256_DIGEST_LENGTH; i++) ss << std::hex << (int)md[i]; std::cout << ss.str() << std::endl; + std::cout << base58::encode("cat") << std::endl; return 0; } \ No newline at end of file