Passwordfile library 5.0.7
C++ library to read/write passwords from/to encrypted files
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 // init sha256 hashing
53 SHA256_CTX sha256;
54 SHA256_Init(&sha256);
55
56 // do the actual hashing
57 SHA256_Update(&sha256, buffer, size);
58
59 // finalize the hashing
60 Sha256Sum hash;
61 SHA256_Final(hash.data, &sha256);
62 return hash;
63}
64
68uint32_t generateRandomNumber(uint32_t min, uint32_t max)
69{
71 std::default_random_engine rng(dev());
72 std::uniform_int_distribution<uint32_t> dist(min, max);
73 return dist(rng);
74}
75
76} // namespace OpenSsl
77} // 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
unsigned char data[size]
Definition: openssl.h:15