OpenVPN 3 Core Library
Loading...
Searching...
No Matches
hashstr.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#ifndef OPENVPN_CRYPTO_HASHSTR_H
13#define OPENVPN_CRYPTO_HASHSTR_H
14
15#include <string>
16
21
22namespace openvpn {
24{
25 public:
26 HashString(DigestFactory &digest_factory,
27 const CryptoAlgs::Type digest_type)
28 : ctx(digest_factory.new_digest(digest_type))
29 {
30 }
31
32 void update(const std::string &str)
33 {
34 ctx->update((unsigned char *)str.c_str(), str.length());
35 }
36
37 void update(const char *str)
38 {
39 ctx->update((unsigned char *)str, std::strlen(str));
40 }
41
42 void update(const char c)
43 {
44 ctx->update((unsigned char *)&c, 1);
45 }
46
47 void update(const Buffer &buf)
48 {
49 ctx->update(buf.c_data(), buf.size());
50 }
51
52 BufferPtr final()
53 {
55 ctx->final(ret->data());
56 return ret;
57 }
58
59 void final(Buffer &output)
60 {
61 const size_t size = ctx->size();
62 if (size > output.max_size())
63 OPENVPN_BUFFER_THROW(buffer_overflow);
64 ctx->final(output.data());
65 output.set_size(size);
66 }
67
68 std::string final_hex()
69 {
70 BufferPtr bp = final();
71 return render_hex_generic(*bp);
72 }
73
74 std::string final_base64()
75 {
76 BufferPtr bp = final();
77 return base64->encode(*bp);
78 }
79
80 private:
82};
83} // namespace openvpn
84
85#endif
#define OPENVPN_BUFFER_THROW(exc)
Definition buffer.hpp:65
std::string encode(const V &data) const
Definition base64.hpp:139
const T * c_data() const
Returns a const pointer to the start of the buffer.
Definition buffer.hpp:1194
size_t size() const
Returns the size of the buffer in T objects.
Definition buffer.hpp:1242
virtual size_t final(unsigned char *out)=0
virtual size_t size() const =0
virtual void update(const unsigned char *in, const size_t size)=0
DigestInstance::Ptr ctx
Definition hashstr.hpp:81
void update(const Buffer &buf)
Definition hashstr.hpp:47
void update(const char *str)
Definition hashstr.hpp:37
void update(const char c)
Definition hashstr.hpp:42
HashString(DigestFactory &digest_factory, const CryptoAlgs::Type digest_type)
Definition hashstr.hpp:26
std::string final_hex()
Definition hashstr.hpp:68
std::string final_base64()
Definition hashstr.hpp:74
void update(const std::string &str)
Definition hashstr.hpp:32
static Ptr Create(ArgsT &&...args)
Creates a new instance of RcEnable with the given arguments.
Definition make_rc.hpp:43
constexpr BufferFlags ARRAY(1u<< 3)
if enabled, use as array
std::string render_hex_generic(const V &data, const bool caps=false)
Definition hexstr.hpp:230
const Base64 * base64
Definition base64.hpp:299
os<< "Session Name: "<< tbc-> session_name<< '\n';os<< "Layer: "<< tbc-> layer str()<< '\n'
std::string ret