Replaced png library in backend

This commit is contained in:
Mutzi 2022-08-31 14:28:07 +02:00
parent 108c87a91b
commit 8669eed13c
3 changed files with 40 additions and 15 deletions

View File

@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.20) cmake_minimum_required(VERSION 3.20)
project(backend) project(backend)
set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED YES) set(CMAKE_CXX_STANDARD_REQUIRED YES)
add_executable(backend add_executable(backend
@ -38,12 +38,12 @@ add_executable(backend
find_package(Drogon CONFIG REQUIRED) find_package(Drogon CONFIG REQUIRED)
find_package(CURL CONFIG REQUIRED) find_package(CURL CONFIG REQUIRED)
find_package(PNG REQUIRED) find_package(lodepng CONFIG REQUIRED)
find_package(OpenSSL REQUIRED)
find_path(JWT_CPP_INCLUDE_DIRS "jwt-cpp/base.h") find_path(JWT_CPP_INCLUDE_DIRS "jwt-cpp/base.h")
find_path(BOTAN_INCLUDE_DIRS "botan/botan.h") find_path(BOTAN_INCLUDE_DIRS "botan/botan.h")
find_path(QR_INCLUDE_DIRS "qrcodegen.hpp") find_path(QR_INCLUDE_DIRS "qrcodegen.hpp")
find_path(PNGPP_INCLUDE_DIRS "png++/color.hpp") find_library(BOTAN_LIBRARY NAMES botan-2 botan)
find_library(BOTAN_LIBRARY botan-2)
find_library(QR_LIBRARY nayuki-qr-code-generator) find_library(QR_LIBRARY nayuki-qr-code-generator)
target_include_directories(backend PRIVATE target_include_directories(backend PRIVATE
@ -51,20 +51,28 @@ target_include_directories(backend PRIVATE
${JWT_CPP_INCLUDE_DIRS} ${JWT_CPP_INCLUDE_DIRS}
${BOTAN_INCLUDE_DIRS} ${BOTAN_INCLUDE_DIRS}
${QR_INCLUDE_DIRS} ${QR_INCLUDE_DIRS}
${PNGPP_INCLUDE_DIRS}
) )
target_link_libraries(backend target_link_libraries(backend
Drogon::Drogon Drogon::Drogon
CURL::libcurl CURL::libcurl
PNG::PNG lodepng
OpenSSL::SSL
${BOTAN_LIBRARY} ${BOTAN_LIBRARY}
${QR_LIBRARY} ${QR_LIBRARY}
) )
install(TARGETS backend) install(TARGETS backend)
target_compile_options(backend PRIVATE if(NOT MSVC)
$<$<CONFIG:Debug>:-g -Wall -Wno-unknown-pragmas> target_compile_options(backend PRIVATE
$<$<CONFIG:Release>:-O3> $<$<CONFIG:Debug>:-g -Wall -Wno-unknown-pragmas>
) $<$<CONFIG:Release>:-O3>
)
else()
target_compile_options(backend PRIVATE /W4 /wd4068)
endif(NOT MSVC)
if(WIN32)
target_compile_definitions(backend PRIVATE NOMINMAX)
endif()

View File

@ -5,14 +5,14 @@
#include <botan/base32.h> #include <botan/base32.h>
#include <botan/base64.h> #include <botan/base64.h>
#include <qrcodegen.hpp> #include <qrcodegen.hpp>
#include <png++/png.hpp> #include <lodepng.h>
#include "controllers/controllers.h" #include "controllers/controllers.h"
#include "db/db.h" #include "db/db.h"
#include "dto/dto.h" #include "dto/dto.h"
std::string create_totp_qrcode(const db::User& user, const std::string& b32_secret) { std::string create_totp_qrcode(const db::User& user, const std::string& b32_secret) {
const int qrcode_pixel_size = 4; constexpr int qrcode_pixel_size = 4;
std::stringstream code_ss; std::stringstream code_ss;
code_ss << "otpauth://totp/MFileserver:" code_ss << "otpauth://totp/MFileserver:"
@ -22,6 +22,23 @@ std::string create_totp_qrcode(const db::User& user, const std::string& b32_secr
<< "&issuer=MFileserver"; << "&issuer=MFileserver";
auto code = qrcodegen::QrCode::encodeText(code_ss.str().c_str(), qrcodegen::QrCode::Ecc::MEDIUM); auto code = qrcodegen::QrCode::encodeText(code_ss.str().c_str(), qrcodegen::QrCode::Ecc::MEDIUM);
const int mod_count = code.getSize(); const int mod_count = code.getSize();
const int row_size = qrcode_pixel_size * mod_count;
std::vector<uint8_t> secret, image, row;
row.reserve(row_size);
image.reserve(row_size * row_size);
for (int y = 0; y < mod_count; y++) {
row.clear();
for (int x = 0; x < mod_count; x++)
row.insert(row.end(), qrcode_pixel_size, code.getModule(x, y) ? 0 : 0xff);
for (int i = 0; i < qrcode_pixel_size; i++)
image.insert(image.end(), row.begin(), row.end());
}
lodepng::encode(secret, image, row_size, row_size, LCT_GREY, 8);
/*
png::image<png::gray_pixel> image(mod_count*qrcode_pixel_size, mod_count*qrcode_pixel_size); png::image<png::gray_pixel> image(mod_count*qrcode_pixel_size, mod_count*qrcode_pixel_size);
for (int x = 0; x < mod_count; x++) for (int y = 0; y < mod_count; y++) { for (int x = 0; x < mod_count; x++) for (int y = 0; y < mod_count; y++) {
const bool mod = code.getModule(x, y); const bool mod = code.getModule(x, y);
@ -34,7 +51,7 @@ std::string create_totp_qrcode(const db::User& user, const std::string& b32_secr
std::string image_str = image_ss.str(); std::string image_str = image_ss.str();
std::vector<uint8_t> secret(image_str.data(), image_str.data()+image_str.size()); std::vector<uint8_t> secret(image_str.data(), image_str.data()+image_str.size());
*/
return "data:image/png;base64," + Botan::base64_encode(secret); return "data:image/png;base64," + Botan::base64_encode(secret);
} }

View File

@ -10,8 +10,8 @@
"jwt-cpp", "jwt-cpp",
"botan", "botan",
"curl", "curl",
"pngpp",
"nayuki-qr-code-generator", "nayuki-qr-code-generator",
"libpng" "lodepng",
"openssl"
] ]
} }