OpenVPN 3 Core Library
Loading...
Searching...
No Matches
hash.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12#pragma once
13
14#include <string>
15#include <cstdint> // for std::uint32_t, uint64_t
16
17#ifdef HAVE_XXHASH
18#define USE_OPENVPN_HASH
19#define XXH_INLINE_ALL
20#include <xxhash.h>
21#if XXH_VERSION_NUMBER < 700
22#error requires XXHash version 0.7.0 or higher
23#endif
24#endif
25
29
30#define OPENVPN_HASH_METHOD(T, meth) \
31 namespace std { \
32 template <> \
33 struct hash<T> \
34 { \
35 inline std::uint64_t operator()(const T &obj) const \
36 { \
37 return obj.meth(); \
38 } \
39 }; \
40 }
41
42#ifdef USE_OPENVPN_HASH
43
44namespace openvpn {
45
46class Hash64
47{
48 public:
49 Hash64(const std::uint64_t init_hashval = 0)
50 : hashval(init_hashval)
51 {
52 }
53
54 void operator()(const void *data, const std::size_t size)
55 {
56 hashval = XXH3_64bits_withSeed((const char *)data, size, hashval);
57 }
58
59 void operator()(const std::string &str)
60 {
61 (*this)(str.c_str(), str.length());
62 }
63
64 template <typename T>
65 inline void operator()(const T &obj)
66 {
67 static_assert(std::is_standard_layout_v<T>, "Hash64: standard layout required");
68 (*this)(&obj, sizeof(obj));
69 }
70
71 std::uint64_t value() const
72 {
73 return hashval;
74 }
75
76 std::string to_string() const
77 {
78 return render_hex_number(hashval);
79 }
80
81 private:
82 std::uint64_t hashval;
83};
84
85} // namespace openvpn
86
87#endif
os<< "Session Name: "<< tbc-> session_name<< '\n';os<< "Layer: "<< tbc-> layer str()<< '\n'