项目作者: pdy

项目描述 :
single-file set of cryptographic and security utils using OpenSSL underneath. Common crypto tasks like encrypting, signing, verifying, X.509 manipulations made easy.
高级语言: C++
项目地址: git://github.com/pdy/simpleopenssl.git
创建时间: 2018-04-29T16:13:21Z
项目社区:https://github.com/pdy/simpleopenssl

开源协议:MIT License

下载


Features

  • Simple API - no fancy abstractions, no templates in interface, just simple set of functions.
  • No custom crypto processing added over OpenSSL - if you got error, it came from OpenSSL. Even IO is done by OpenSSL itself.
  • Clear, unified error handling without exceptions.
  • All heap allocated OpenSSL types encapsulated with unique pointers with stateless deleters.

Examples

Check examples folder for sample applications.

  • Hash file using sha256
    ```cpp
    using namespace so;

if(const auto hash = hash::fileSHA256(filePath))
{
LOG_DBG << “File “ << filePath << “ hash: “ << binToHexStr(hash.value);
}
else
{
LOG_ERR << hash.msg();
}

  1. * Generate RSA key and convert it to PEM format
  2. ```cpp
  3. using namespace so;
  4. auto key = rsa::create(rsa::KeyBits::_3072_)
  5. if(!key)
  6. {
  7. LOG_ERR << key.msg();
  8. return;
  9. }
  10. const auto pemKey = rsa::convertPrivKeyToPem(*key.value);
  11. if(!pemKey)
  12. {
  13. LOG_ERR << pemKey.msg();
  14. return;
  15. }
  16. const auto pemPubKey = rsa::convertPubKeyToPem(*key.value);
  17. if(!pemPubKey)
  18. {
  19. LOG_ERR << pemPubKey.msg();
  20. return;
  21. }
  22. LOG_INF << "New priv key pem: " << pemKey.value;
  23. LOG_INF << "New pub key pem: " << pemPubKey.value;
  • Check certificate validity period
    ```cpp
    using namespace so;
    std::string timetPrettyString(std::time_t time);

so::X509_uptr cert = so::make_unique(SSL_get_peer_certificate(ssl));
if(!cert)
{
LOG_ERR << “Get peer cert error: “ << so::getLastErrString();
return;
}

const auto validity = x509::getValidity(*cert);
if(!validity)
{
LOG_ERR << “Getting validity failed: “ << validity.msg();
return;
}

LOG_INF << “Cert not before: “ << timetPrettyString(validity->notBefore);
LOG_INF << “Cert not after: “ << timetPrettyString(validity->notAfter);

// ………………………….

std::string timetPrettyString(std::time_t time)
{
std::tm *ptm = std::gmtime(&time);

char buffer[32];
std::strftime(buffer, 32, “%a, %d.%m.%Y %H:%M:%S”, ptm);

return buffer;
}

  1. # Usage
  2. 1. Copy [simpleopenssl.hpp](https://raw.githubusercontent.com/severalgh/simpleopenssl/master/include/simpleopenssl/simpleopenssl.hpp) to your build tree.
  3. 2. Add ```#define SO_IMPLEMENTATION``` in exacly one place just before the include to specify where implementation should be placed for the linker:
  1. #define SO_IMPLEMENTATION
  2. #include "simpleopenssl.hpp"
  3. ```
  1. Use plain #include "simpleopenssl.hpp" in every other place.

Dependencies

  • OpenSSL version 1.1.1
  • C++11 or higher
  • STL