libDwmCredence-0.1.32
DwmCredenceGenericHash.hh
Go to the documentation of this file.
1//===========================================================================
2// @(#) $DwmPath$
3//===========================================================================
4// Copyright (c) Daniel W. McRobb 2022
5// All rights reserved.
6//
7// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions
9// are met:
10//
11// 1. Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13// 2. Redistributions in binary form must reproduce the above copyright
14// notice, this list of conditions and the following disclaimer in the
15// documentation and/or other materials provided with the distribution.
16// 3. The names of the authors and copyright holders may not be used to
17// endorse or promote products derived from this software without
18// specific prior written permission.
19//
20// IN NO EVENT SHALL DANIEL W. MCROBB BE LIABLE TO ANY PARTY FOR
21// DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
22// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE,
23// EVEN IF DANIEL W. MCROBB HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
24// DAMAGE.
25//
26// THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND
27// DANIEL W. MCROBB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
28// UPDATES, ENHANCEMENTS, OR MODIFICATIONS. DANIEL W. MCROBB MAKES NO
29// REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER
30// IMPLIED OR EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31// WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE,
32// OR THAT THE USE OF THIS SOFTWARE WILL NOT INFRINGE ANY PATENT,
33// TRADEMARK OR OTHER RIGHTS.
34//===========================================================================
35
36//---------------------------------------------------------------------------
40//---------------------------------------------------------------------------
41
42#ifndef _DWMCREDENCEGENERICHASH_HH_
43#define _DWMCREDENCEGENERICHASH_HH_
44
45extern "C" {
46 #include <sodium.h>
47}
48
49#include <cstdint>
50#include <string>
51
52namespace Dwm {
53
54 namespace Credence {
55
56 //------------------------------------------------------------------------
60 //------------------------------------------------------------------------
61 template <size_t OutLen>
63 {
64 public:
65 //----------------------------------------------------------------------
67 //----------------------------------------------------------------------
68 GenericHash(const std::string & key = "")
69 {
70 const uint8_t *kp =
71 (key.empty() ? nullptr : (const uint8_t *)key.data());
72 crypto_generichash_init(&_h, kp, key.size(), OutLen);
73 }
74
75 //----------------------------------------------------------------------
77 //----------------------------------------------------------------------
78 void Update(const std::string & chunk)
79 {
80 crypto_generichash_update(&_h, (const uint8_t *)chunk.data(),
81 chunk.size());
82 return;
83 }
84
85 //----------------------------------------------------------------------
87 //----------------------------------------------------------------------
88 std::string Final()
89 {
90 uint8_t finalbuf[OutLen] = {0};
91 crypto_generichash_final(&_h, finalbuf, sizeof(finalbuf));
92 return std::string((const char *)finalbuf, OutLen);
93 }
94
95 private:
96 crypto_generichash_state _h;
97 };
98
99 } // namespace Credence
100
101} // namespace Dwm
102
103#endif // _DWMCREDENCEGENERICHASH_HH_
Class template that wraps sodium's crypto_generichash_* functions.
Definition DwmCredenceGenericHash.hh:63
std::string Final()
Returns the final hash.
Definition DwmCredenceGenericHash.hh:88
void Update(const std::string &chunk)
Updates the hash.
Definition DwmCredenceGenericHash.hh:78
GenericHash(const std::string &key="")
Initializes the hash.
Definition DwmCredenceGenericHash.hh:68