Skip to content

Commit

Permalink
Made openssl an optional requirement
Browse files Browse the repository at this point in the history
  • Loading branch information
bernedom committed Jul 1, 2024
1 parent d4aea19 commit a798819
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 52 deletions.
4 changes: 0 additions & 4 deletions chapter04/ex07_pack_nsis_standalone/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ if(NOT PROJECT_IS_TOP_LEVEL)
message(FATAL_ERROR "The chapter-4, ex07_pack project is intended to be a standalone, top-level project. Do not include this directory.")
endif()

# Search the package OpenSSL and its component SSL, configuration fails if not found
# this is used by the executable target
find_package(OpenSSL COMPONENTS SSL)

add_subdirectory(executable)
add_subdirectory(library)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ add_executable(ch4_ex07_executable src/main.cpp)
target_compile_features(ch4_ex07_executable PRIVATE cxx_std_11)

# Link executable target to library target
target_link_libraries(ch4_ex07_executable PRIVATE ch4_ex07_library OpenSSL::SSL)
target_link_libraries(ch4_ex07_executable PRIVATE ch4_ex07_library)


install(TARGETS ch4_ex07_executable)
43 changes: 0 additions & 43 deletions chapter04/ex07_pack_nsis_standalone/executable/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,55 +10,12 @@

#include <iomanip>
#include <iostream>
#include <openssl/evp.h>
#include <openssl/sha.h>
#include <sstream>
#include <string>

std::string sha256(const std::string &str) {
unsigned char hash[SHA256_DIGEST_LENGTH];
EVP_MD_CTX *mdctx;

if (!(mdctx = EVP_MD_CTX_new())) {
// Handle error
return "";
}

if (1 != EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL)) {
// Handle error
EVP_MD_CTX_free(mdctx);
return "";
}

if (1 != EVP_DigestUpdate(mdctx, str.c_str(), str.size())) {
// Handle error
EVP_MD_CTX_free(mdctx);
return "";
}

unsigned int hash_len;
if (1 != EVP_DigestFinal_ex(mdctx, hash, &hash_len)) {
// Handle error
EVP_MD_CTX_free(mdctx);
return "";
}

EVP_MD_CTX_free(mdctx);

std::stringstream ss;
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
ss << std::hex << std::setw(2) << std::setfill('0') << (int)hash[i];
}

return ss.str();
}

int main(int, char **) {
chapter4::ex07::greeter g;
g.greet();

const std::string message{"CMake is awesome!"};

std::cout << "The sha256 hash of the message '" << message << "' is:\n";
std::cout << "\t" << sha256(message) << "\n";
}
16 changes: 12 additions & 4 deletions chapter05/find_package_example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,19 @@ project(
# enable debuging for finding the package (For illustrative purposes)
# set(CMAKE_FIND_DEBUG_MODE TRUE )

# Search the package OpenSSL and its component SSL, configuration fails if not found
# because of the keyword REQUIRED
find_package(OpenSSL REQUIRED COMPONENTS SSL)

# disable debugging again set(CMAKE_FIND_DEBUG_MODE FALSE )
# Search the package OpenSSL and its component SSL.
# To have the configuration fail if not found, add the REQUIRED keyword like this:
# find_package(OpenSSL COMPONENTS SSL REQUIRED)
find_package(OpenSSL COMPONENTS SSL)

# disable debugging again
# set(CMAKE_FIND_DEBUG_MODE FALSE )

if(OpenSSL-NOTFOUND)
message(STATUS "OpenSSL package not found; Not building find-package example.")
return()
endif()

# Create a target to build an executable
add_executable(ch5_find_package_example)
Expand Down

0 comments on commit a798819

Please sign in to comment.