AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题

问题[boost](coding)

Martin Hope
SeedlessKiwi
Asked: 2024-10-30 01:22:55 +0800 CST

有没有简单的方法可以使 std::chrono::duration 的单位可配置?

  • 6

我试图通过修改 boost::property_tree 解析的 xml 配置文件来允许设置 std::chrono::duration 报告的单位。我当前的非编译解决方案尝试使用 std::variant 执行此操作。

在 .hpp 类声明中

using TimestampVariant = std::variant<
        std::chrono::nanoseconds, 
        std::chrono::microseconds,
        std::chrono::milliseconds,
        std::chrono::seconds
    >; 

TimestampVariant _timestamp_v;

在.cpp中

auto GetTimestampVisitor =  [](const auto& t) -> decltype(auto) {
    return std::chrono::duration_cast<std::remove_reference_t<decltype(t)>>(std::chrono::system_clock::now().time_since_epoch()).count(); 
};

void SetupFunction()
{
    boost::property_tree::ptree property_tree;
    boost::property_tree::read_xml(filepath, property_tree);
    auto config = property_tree.get_child("Config");
    std::string timestamp_type = config.get<std::string>("ReportingUnits");
    if(!timestamp_type.compare("seconds") || !timestamp_type.compare("s"))
    {
        _timestamp_v = std::chrono::seconds();
    }
    else if(!timestamp_type.compare("milliseconds") || !timestamp_type.compare("ms"))
    {
        _timestamp_v = std::chrono::milliseconds();
    }
    else if(!timestamp_type.compare("microseconds") || !timestamp_type.compare("us"))
    {
        _timestamp_v = std::chrono::microseconds();
    }
    else if(!timestamp_type.compare("nanoseconds") || !timestamp_type.compare("ns"))
    {
        _timestamp_v = std::chrono::nanoseconds();
    }
}

void OutputFunction()
{
   std::cout << std::visit(GetTimestampVisitor, _timestamp_v) << std::endl;
}

我承认我不擅长元编程。有没有更简单的方法可以做到这一点?本质上,我只能在编译时保证持续时间的类型将是持续时间类型的子集之一。

boost
  • 1 个回答
  • 41 Views
Martin Hope
assemblernoob
Asked: 2024-06-04 23:42:51 +0800 CST

独立 Boost.Asio + Boost.Beast

  • 6

我正在学习 boost 并想使用 Beast 和 Asio 制作一个 http 服务器,但我不想将整个 boost 库拖到我的项目中。

到目前为止我所做的:将 Boost.Beast 和 Boost.Asio 添加为 github 存储库中的子模块,将它们包含到我的项目中并定义 BOOST_ASIO_STANDALONE。

当然,它不会构建并丢失很多文件。

所以我的问题是:可以做到吗?必须包含 boost 的哪些部分才能使其工作?

boost
  • 1 个回答
  • 27 Views
Martin Hope
vengy
Asked: 2024-04-07 09:48:55 +0800 CST

为 Boost C++ 启用 OpenSSL FIPS 模式

  • 6

问题

浏览这些不同的 OpenSSL 3.0 文档

  • https://security.stackexchange.com/questions/34791/openssl-vs-fips-enabled-openssl
  • https://github.com/openssl/openssl/blob/master/README-FIPS.md
  • https://www.openssl.org/docs/man3.0/man7/fips_module.html
  • https://en.wikipedia.org/wiki/FIPS_140-2

我设法拼凑出一个在 OpenSSL 中启用 FIPS 模式的解决方案,并牢记以下目标:

重点是,一旦在 OpenSSL 中启用 FIPS 模式,通过 OpenSSL 执行的所有加密操作(包括 C++ Boost.Asio 用于 SSL/TLS 连接的加密操作)都将使用 FIPS 批准的算法,确保整个过程都符合 FIPS 标准。 Boost C++ 应用程序。

enableFIPS()这是为 Boost C++ 启用 OpenSSL FIPS 模式的有效函数吗?

例子

主程序

#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <openssl/provider.h>

bool PrintErrorSSL(const std::string& err)
{
   std::cerr << err << std::endl;
   unsigned long err_code;
   while ((err_code = ERR_get_error()) != 0)
   {
      char err_msg[256];
      ERR_error_string_n(err_code, err_msg, sizeof(err_msg));
      std::cerr << "OpenSSL error: " << err_msg << std::endl;
   }
   // FIPS mode not enabled.
   return false;
}

bool enableFIPS()
{
   std::string ConfigPath;
   std::string TestPath = "C:/Projects/sys-openssl-fips/Test/";
   
#ifdef _WIN32
    ConfigPath = TestPath + "openssl-fips.cnf";
#else
    ConfigPath = TestPath + "openssl-fips-linux.cnf";
#endif

   if (!OSSL_PROVIDER_set_default_search_path(nullptr, TestPath.c_str()))
   {
      return PrintErrorSSL("OSSL_PROVIDER_set_default_search_path() Failed");
   }

   if (!OSSL_LIB_CTX_load_config(nullptr, ConfigPath.c_str()))
   {
      return PrintErrorSSL("OSSL_LIB_CTX_load_config() Failed");
   }

   if (!OSSL_PROVIDER_available(nullptr, "fips") || !OSSL_PROVIDER_available(nullptr, "base"))
   {
      return PrintErrorSSL("OSSL_PROVIDER_available() Failed");
   }

   if (!EVP_default_properties_is_fips_enabled(nullptr))
   {
      return PrintErrorSSL("EVP_default_properties_is_fips_enabled() Failed");
   }

   // FIPS mode enabled.
   return true;
}

int main(int argc, char* argv[])
{
   if (enableFIPS())
   {
      using boost::asio::ip::tcp;
      boost::asio::io_context io_context;
      boost::asio::ssl::context ssl_context(boost::asio::ssl::context::tlsv12);
      boost::asio::ssl::stream<tcp::socket> socket(io_context, ssl_context);
      tcp::resolver resolver(io_context);
      auto endpoints = resolver.resolve("www.google.com", "443");
      boost::asio::connect(socket.lowest_layer(), endpoints);
      socket.handshake(boost::asio::ssl::stream_base::client);
      std::string request = "GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n";
      boost::asio::write(socket, boost::asio::buffer(request));
      boost::asio::streambuf response;
      boost::system::error_code ec;
      boost::asio::read_until(socket, response, "\r\n", ec);
      std::istream response_stream(&response);
      std::string http_status;
      std::getline(response_stream, http_status);
      if (http_status.find("200 OK") != std::string::npos)
         return 0;
   }

   return 1;
}

openssl-fips.cnf

config_diagnostics = 1
openssl_conf = openssl_init

[fips_sect]
activate = 1
conditional-errors = 1
security-checks = 1
module-mac = D9:AB:CC:37:8A:4C:06:BF:E9:E3:A8:F7:B9:B5:02:48:58:71:76:EB:5E:71:8A:0F:87:AA:52:46:7D:60:B0:EB

[openssl_init]
providers = provider_sect
alg_section = algorithm_sect

[provider_sect]
fips = fips_sect
base = base_sect

[base_sect]
activate = 1

[algorithm_sect]
default_properties = fips=yes

输出(Windows)

OpenSSL FIPS 模式:关闭

项目将利用这些 openssl静态库提供的非 FIPS 批准的算法

  • libssl - 实现传输层安全 (TLS) 协议。
  • libcrypto - 实现加密算法。

OpenSSL FIPS 模式:开

项目将利用这些 openssl共享库提供的经 FIPS 批准的算法

  • fips.so(在 Linux 上)
  • fips.dll(在 Windows 上)

使用Process Explorer加载fips.dll:

系统fips

boost
  • 1 个回答
  • 20 Views
Martin Hope
The amateur programmer
Asked: 2023-10-15 15:55:46 +0800 CST

在 C++03 编译器上使用移动模拟将 boost::unique_lock 作为返回值从函数中移出是否安全?

  • 6

我有以下可移动但不可复制的类,可用于同步对某些共享资源的访问:

class wrapper_with_lock{
    private:
        BOOST_MOVABLE_BUT_NOT_COPYABLE(wrapper_with_lock)
        boost::unique_lock l;
    public:
        int* data1;//These point to data that needs to have synchronized access
        char* data2;//....
        wrapper_with_lock(boost::mutex& m) : l(m){}//Constructor acquires the lock
        wrapper_with_lock(BOOST_RV_REF(wrapper_with_lock) x) {
            l = boost::move(x.l);//Move the lock
            data1 = x.data1;//Move the pointers
            x.data1 = 0;
            ....
        }
        wrapper_with_lock& operator=(BOOST_RV_REF(wrapper_with_lock) x) // Move assign
        {
            if (this != &x){
                l = boost::move(x.l);//Move the lock and other data
                ....
            }
            return *this;
        }
}

这里的想法是,这个结构可以移动,保存互斥体,并且在超出范围后自动释放锁。预期用途如下:

wrapper_with_lock do_some_init(boost::mutex& m){
    wrapper_with_lock w(m);
    *(w.data1) = 1234;//Do something initially with the data etc...
    //Return the lock holding object by moving it (should move the internal lock).
    //The lock should be valid and properly moved to the caller
    //of this function inside the wrapper
    return boost::move(w);
}

问题是,当我们在这个项目中使用 boost 库的移动模拟和 C++03 编译器时,移动锁的这种想要的行为是否得到保证?旧的编译器不支持新标准。

boost
  • 1 个回答
  • 17 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve