#include #include #include #include "openssl_wrapper.h" std::vector secure_random_bytes(int num) { if (num < 0) { throw std::invalid_argument("secure_random_bytes(): num variable out of range (num < 0)"); } std::vector bytes(static_cast(num), 0); if (RAND_bytes(reinterpret_cast(bytes.data()), num) == 1) { return bytes; } else { throw OpenSSLException(ERR_get_error()); } } std::array hmac_sha3_256(const std::vector& key, const std::vector& data) { char hmac[32]; unsigned int md_len; if (HMAC(EVP_sha3_256(), key.data(), static_cast(key.size()), reinterpret_cast(data.data()), data.size(), reinterpret_cast(hmac), &md_len)) { if (md_len != 32) { throw std::runtime_error("hmac_sha3_256(): HMAC() returned an unexpected size"); } return std::to_array(hmac); } else { throw OpenSSLException(ERR_get_error()); } }