feat: use fmt instead of iostreams
All checks were successful
continuous-integration/drone/push Build is passing

switch to fmt - "A modern formatting library" - for fun and profit,
currently in header-only configuration.

ref: https://github.com/fmtlib/fmt

closes #5
closes #4
This commit is contained in:
surtur 2021-11-18 22:05:05 +01:00
parent 9fd35eee93
commit bd421295c4
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D
7 changed files with 47 additions and 14 deletions

View File

@ -5,11 +5,21 @@ def main(ctx):
"type": "docker",
"name": "testing",
"steps": [
{
"name": "update submodules",
"image": "docker.io/immawanderer/fedora-cpp:linux-amd64",
"pull": "always",
"depends_on": ["clone"],
"commands": [
"uname -r",
"git submodule update --init --recursive"
]
},
{
"name": "cppcheck",
"image": "docker.io/archlinux:base-devel",
"pull": "always",
"depends_on": ["clone"],
"depends_on": ["update submodules"],
"commands": [
"pacman -Sy cppcheck --noconfirm --needed",
"uname -r",

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "lib/fmt"]
path = lib/fmt
url = https://github.com/fmtlib/fmt.git

View File

@ -4,6 +4,23 @@ project(fortuna LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Git QUIET)
if(GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git")
option(GIT_SUBMODULE "Check submodules during build" ON)
if(GIT_SUBMODULE)
message(STATUS "Update submodule(s)")
message(STATUS "Current source dir: ${CMAKE_CURRENT_SOURCE_DIR}")
execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
RESULT_VARIABLE GIT_SUBMOD_RESULT)
if(NOT GIT_SUBMOD_RESULT EQUAL "0")
message(FATAL_ERROR "git submodule update --init --recursive failed with ${GIT_SUBMOD_RESULT}, please checkout submodules")
endif()
endif()
endif()
add_subdirectory(lib/fmt EXCLUDE_FROM_ALL)
if(CMAKE_BUILD_TYPE MATCHES "Debug")
# Produce debugging information in the operating system's native format.
# Level 3 includes extra information, such as all the macro definitions
@ -99,4 +116,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
endif()
add_executable(fortuna main.cpp generator.cpp generator.h fortuna.cpp fortuna.h)
target_link_libraries(fortuna cryptopp)
# ref: https://cmake.org/pipermail/cmake/2016-May/063400.html
target_link_libraries(fortuna
PRIVATE cryptopp
PRIVATE fmt::fmt-header-only)

View File

@ -9,10 +9,10 @@
#include <cryptopp/filters.h>
#include <cryptopp/serpent.h>
#include <cryptopp/ccm.h>
#include <fmt/core.h>
#include <cassert>
#include <cstring>
#include <iostream>
namespace fortuna {
@ -46,7 +46,7 @@ namespace fortuna {
std::string cipher, encoded_c, decrypted;
try {
std::cout << "plain text: " << plain << std::endl;
fmt::print("plain text: {}\n", plain);
CTR_Mode<Serpent>::Encryption e;
e.SetKeyWithIV(key,key.size(),ctr);
@ -63,7 +63,7 @@ namespace fortuna {
); // StringSource
}
catch(CryptoPP::Exception& e) {
std::cerr << e.what() << std::endl;
fmt::print(stderr, "{}\n", e.what());
exit(1);
}
@ -73,7 +73,7 @@ namespace fortuna {
new StringSink(encoded_c)
) // HexEncoder
); // StringSource
std::cout << "cipher text: " << encoded_c << std::endl;
fmt::print("cipher text: {}\n", encoded_c);
try {
CTR_Mode<Serpent>::Decryption d;
@ -86,10 +86,10 @@ namespace fortuna {
) // StreamTransformationFilter
); // StringSource
std::cout << "decrypted text: " << decrypted << std::endl;
fmt::print("decrypted text: {}\n", decrypted);
}
catch(CryptoPP::Exception& e) {
std::cerr << e.what() << std::endl;
fmt::print(stderr, "{}\n", e.what());
exit(1);
}

View File

@ -5,11 +5,11 @@
#include <cassert>
#include <cstdint>
#include <stdexcept>
#include <iostream>
#include <cryptopp/hex.h>
#include <cryptopp/filters.h>
#include <cryptopp/sha3.h>
#include <fmt/core.h>
#include "generator.h"
@ -69,7 +69,7 @@ auto Generator::do_sha(const std::string& k_n_s) -> std::string {
new HashFilter(sha3_256,new HexEncoder(new StringSink(digest),false))
);
assert(digest == to_compare);
std::cout << digest << std::endl;
fmt::print("{}\n", digest);
digest.erase();
// return digest;

1
lib/fmt Submodule

@ -0,0 +1 @@
Subproject commit c089f7d497bcce35a0fd6da0512f8585ed1cc002

View File

@ -1,11 +1,10 @@
#include <iostream>
#include "fortuna.h"
#include "generator.h"
#include <fmt/core.h>
int main() {
using std::cout;
using std::endl;
cout << "[*] doing evil stuff" << endl;
fmt::print("[*] doing evil stuff\n");
fortuna::Fortuna f;
f.random_data(4);
return 0;