Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
CryptoHash.h
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
6#ifndef _CRYPTOHASH_H
7#define _CRYPTOHASH_H
8
9#include "CryptoConstants.h"
10#include "Define.h"
11#include "Errors.h"
12#include <array>
13#include <string>
14#include <string_view>
15#include <openssl/evp.h>
16
17class BigNumber;
18
19namespace SkyFire::Impl
20{
22 {
23 typedef EVP_MD const* (*HashCreator)();
24
25#if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER < 0x10100000L
26 static EVP_MD_CTX* MakeCTX() { return EVP_MD_CTX_create(); }
27 static void DestroyCTX(EVP_MD_CTX* ctx) { EVP_MD_CTX_destroy(ctx); }
28#else
29 static EVP_MD_CTX* MakeCTX() { return EVP_MD_CTX_new(); }
30 static void DestroyCTX(EVP_MD_CTX* ctx) { EVP_MD_CTX_free(ctx); }
31#endif
32 };
33
34 template <GenericHashImpl::HashCreator HashCreator, size_t DigestLength>
36 {
37 public:
38 static constexpr size_t DIGEST_LENGTH = DigestLength;
39 using Digest = std::array<uint8, DIGEST_LENGTH>;
40
41 static Digest GetDigestOf(uint8 const* data, size_t len)
42 {
43 GenericHash hash;
44 hash.UpdateData(data, len);
45 hash.Finalize();
46 return hash.GetDigest();
47 }
48
49 template <typename... Ts>
50 static auto GetDigestOf(Ts&&... pack) -> std::enable_if_t<!(std::is_integral_v<std::decay_t<Ts>> || ...), Digest>
51 {
52 GenericHash hash;
53 (hash.UpdateData(std::forward<Ts>(pack)), ...);
54 hash.Finalize();
55 return hash.GetDigest();
56 }
57
59 {
60 int result = EVP_DigestInit_ex(_ctx, HashCreator(), nullptr);
61 ASSERT(result == 1);
62 }
63
65 {
66 if (!_ctx)
67 return;
69 _ctx = nullptr;
70 }
71
72 void UpdateData(uint8 const* data, size_t len)
73 {
74 int result = EVP_DigestUpdate(_ctx, data, len);
75 ASSERT(result == 1);
76 }
77 void UpdateData(std::string_view str) { UpdateData(reinterpret_cast<uint8 const*>(str.data()), str.size()); }
78 void UpdateData(std::string const& str) { UpdateData(std::string_view(str)); } /* explicit overload to avoid using the container template */
79 void UpdateData(char const* str) { UpdateData(std::string_view(str)); } /* explicit overload to avoid using the container template */
80 template <typename Container>
81 void UpdateData(Container const& c) { UpdateData(std::data(c), std::size(c)); }
82
83 void Finalize()
84 {
85 uint32 length;
86 int result = EVP_DigestFinal_ex(_ctx, _digest.data(), &length);
87 ASSERT(result == 1);
88 ASSERT(length == DIGEST_LENGTH);
90 _ctx = nullptr;
91 }
92
93 Digest const& GetDigest() const { return _digest; }
94
95 private:
96 EVP_MD_CTX* _ctx;
98 };
99}
100
101namespace SkyFire::Crypto
102{
105}
106
107#endif
std::uint8_t uint8
Definition Define.h:79
std::uint32_t uint32
Definition Define.h:77
#define ASSERT
Definition Errors.h:29
static auto GetDigestOf(Ts &&... pack) -> std::enable_if_t<!(std::is_integral_v< std::decay_t< Ts > >||...), Digest >
Definition CryptoHash.h:50
Digest const & GetDigest() const
Definition CryptoHash.h:93
void UpdateData(Container const &c)
Definition CryptoHash.h:81
void UpdateData(std::string const &str)
Definition CryptoHash.h:78
void UpdateData(uint8 const *data, size_t len)
Definition CryptoHash.h:72
std::array< uint8, DIGEST_LENGTH > Digest
Definition CryptoHash.h:39
static Digest GetDigestOf(uint8 const *data, size_t len)
Definition CryptoHash.h:41
void UpdateData(std::string_view str)
Definition CryptoHash.h:77
void UpdateData(char const *str)
Definition CryptoHash.h:79
SkyFire::Impl::GenericHash< EVP_sha1, Constants::SHA1_DIGEST_LENGTH_BYTES > SHA1
Definition CryptoHash.h:103
SkyFire::Impl::GenericHash< EVP_sha256, Constants::SHA256_DIGEST_LENGTH_BYTES > SHA256
Definition CryptoHash.h:104
static EVP_MD_CTX * MakeCTX()
Definition CryptoHash.h:29
static void DestroyCTX(EVP_MD_CTX *ctx)
Definition CryptoHash.h:30