Passwordfile library 5.0.11
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"
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
28void 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
39void clean()
40{
41 // removes all digests and ciphers
42 EVP_cleanup();
43 // remove error strings
44 ERR_free_strings();
45}
46
50Sha256Sum 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{
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
Provides a random device using the OpenSSL function RAND_bytes().
PASSWORD_FILE_EXPORT void init()
Initializes OpenSSL.
Definition openssl.cpp:28
PASSWORD_FILE_EXPORT void clean()
Cleans resources of OpenSSL.
Definition openssl.cpp:39
PASSWORD_FILE_EXPORT std::uint32_t generateRandomNumber(std::uint32_t min, std::uint32_t max)
PASSWORD_FILE_EXPORT Sha256Sum computeSha256Sum(const unsigned char *buffer, std::size_t size)
Computes a SHA-256 sum using OpenSSL.
Definition openssl.cpp:50
Contains utility classes and functions.
Definition openssl.h:9
static constexpr std::size_t size
Definition openssl.h:14