Passwordfile library: util/openssl.cpp Source File

Passwordfile library 5.0.13 C++ library to read/write passwords from/to encrypted files

Loading...
Searching...
No Matches
openssl.cpp
Go to the documentation of this file.
1#include "./openssl.h"
2#include "./opensslrandomdevice.h"
3
4#include <c++utilities/conversion/binaryconversion.h>
5
6#include <openssl/conf.h>
7#include <openssl/err.h>
8#include <openssl/evp.h>
9#include <openssl/sha.h>
10
11#include <random>
12
16namespace Util {
17
21namespace OpenSsl {
22
23static_assert(Sha256Sum::size == SHA256_DIGEST_LENGTH, "SHA-256 sum fits into Sha256Sum struct");
24
28 void init()
29{
30 // load the human readable error strings for libcrypto
31 ERR_load_crypto_strings();
32 // load all digest and cipher algorithms
33 OpenSSL_add_all_algorithms();
34}
35
39 void clean()
40{
41 // removes all digests and ciphers
42 EVP_cleanup();
43 // remove error strings
44 ERR_free_strings();
45}
46
50 Sha256Sum computeSha256Sum(const unsigned char *buffer, std::size_t size)
51{
52 auto hash = Sha256Sum();
53 SHA256(buffer, size, hash.data);
54 return hash;
55}
56
60uint32_t generateRandomNumber(uint32_t min, uint32_t max)
61{
62 OpenSslRandomDevice dev;
63 std::default_random_engine rng(dev());
64 std::uniform_int_distribution<uint32_t> dist(min, max);
65 return dist(rng);
66}
67
68} // namespace OpenSsl
69} // namespace Util
Util::OpenSslRandomDevice
Provides a random device using the OpenSSL function RAND_bytes().
Definition opensslrandomdevice.h:11
Util::OpenSsl
Contains functions utilizing the usage of OpenSSL.
Definition openssl.h:11
Util::OpenSsl::init
PASSWORD_FILE_EXPORT void init()
Initializes OpenSSL.
Definition openssl.cpp:28
Util::OpenSsl::clean
PASSWORD_FILE_EXPORT void clean()
Cleans resources of OpenSSL.
Definition openssl.cpp:39
Util::OpenSsl::generateRandomNumber
PASSWORD_FILE_EXPORT std::uint32_t generateRandomNumber(std::uint32_t min, std::uint32_t max)
Util::OpenSsl::computeSha256Sum
PASSWORD_FILE_EXPORT Sha256Sum computeSha256Sum(const unsigned char *buffer, std::size_t size)
Computes a SHA-256 sum using OpenSSL.
Definition openssl.cpp:50
Util
Contains utility classes and functions.
Definition openssl.h:9
openssl.h
opensslrandomdevice.h
Util::OpenSsl::Sha256Sum
Definition openssl.h:13
Util::OpenSsl::Sha256Sum::size
static constexpr std::size_t size
Definition openssl.h:14