mal-packet-weaver
C++20 packet serialization/deserialization library.
Loading...
Searching...
No Matches
sha.hpp
Go to the documentation of this file.
1#pragma once
2#include "crypto-common.hpp"
3
4#ifdef MAL_PACKET_WEAVER_HAS_OPENSSL
9namespace mal_packet_weaver::crypto::SHA
10{
18 [[nodiscard]] inline Hash ComputeHash(const ByteView data, const Hash::HashType hash_type)
19 {
20 ByteArray result;
21 switch (hash_type)
22 {
23 case Hash::HashType::SHA256:
24 {
25 result.resize(SHA256_DIGEST_LENGTH);
26 SHA256(data.as<const unsigned char>(), data.size(), result.as<unsigned char>());
27 break;
28 }
29 case Hash::HashType::SHA384:
30 {
31 result.resize(SHA384_DIGEST_LENGTH);
32 SHA384(data.as<const unsigned char>(), data.size(), result.as<unsigned char>());
33 break;
34 }
35 case Hash::HashType::SHA512:
36 {
37 result.resize(SHA512_DIGEST_LENGTH);
38 SHA512(data.as<const unsigned char>(), data.size(), result.as<unsigned char>());
39 break;
40 }
41 default:
42 AlwaysAssert(false, "Unknown hash type");
43 }
44 return Hash{ result, hash_type };
45 }
46} // namespace mal_packet_weaver::crypto::SHA
47#endif
void AlwaysAssert(bool value, std::string_view message="Assert failed", std::source_location location=std::source_location::current())
Always asserts a condition with customizable behavior.
Represents a cryptographic hash value along with its type.