libkazv
sha256.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of libkazv.
3  * SPDX-FileCopyrightText: 2021 Tusooa Zhu <tusooa@kazv.moe>
4  * SPDX-License-Identifier: AGPL-3.0-or-later
5  */
6 
7 #pragma once
8 #include <libkazv-config.hpp>
9 
10 #include <memory>
11 
12 
13 #include <copy-helper.hpp>
14 
15 #include "crypto-util.hpp"
16 
17 
18 namespace Kazv
19 {
20  class SHA256Desc
21  {
22  public:
23  using DataT = std::string;
24 
28  SHA256Desc();
29 
31 
32  ~SHA256Desc();
33 
37  SHA256Desc process(DataT data) const &;
38 
39  template<class RangeT,
40  class = std::enable_if_t<!std::is_same_v<std::decay_t<RangeT>, DataT>, int>>
41  SHA256Desc process(RangeT data) const &
42  {
43  return process(DataT(data.begin(), data.end()));
44  }
45 
49  SHA256Desc process(DataT data) &&;
50 
51  template<class RangeT,
52  class = std::enable_if_t<!std::is_same_v<std::decay_t<RangeT>, DataT>, int>>
54  {
55  return std::move(*this).process(DataT(data.begin(), data.end()));
56  }
57 
61  void processInPlace(DataT data);
62 
63  template<class RangeT,
64  class = std::enable_if_t<!std::is_same_v<std::decay_t<RangeT>, DataT>, int>>
66  {
67  processInPlace(DataT(data.begin(), data.end()));
68  }
69 
73  std::string get() const;
74 
75  private:
76  struct Private;
77  std::unique_ptr<Private> m_d;
78  };
79 }
Kazv::SHA256Desc::processInPlace
void processInPlace(DataT data)
Process data in-place.
Definition: sha256.cpp:44
crypto-util.hpp
copy-helper.hpp
Kazv::SHA256Desc::get
std::string get() const
Get the hash result as unpadded base64.
Definition: sha256.cpp:49
Kazv::SHA256Desc::process
SHA256Desc process(RangeT data) &&
Definition: sha256.hpp:53
Kazv::SHA256Desc::SHA256Desc
SHA256Desc()
Construct a SHA256 description.
Definition: sha256.cpp:21
Kazv::Private
@ Private
Definition: client-model.hpp:41
Kazv
Definition: location.hpp:10
KAZV_DECLARE_COPYABLE
#define KAZV_DECLARE_COPYABLE(typeName)
Definition: copy-helper.hpp:10
RangeT
A RangeT is an ordered collection that can be iterated through.
Definition: range-t.hpp:20
RangeT::begin
auto begin() const
The beginning iterator of this range.
Kazv::SHA256Desc::DataT
std::string DataT
Definition: sha256.hpp:23
Kazv::SHA256Desc::process
SHA256Desc process(RangeT data) const &
Definition: sha256.hpp:41
Kazv::SHA256Desc::processInPlace
void processInPlace(RangeT data)
Definition: sha256.hpp:65
Kazv::SHA256Desc::process
SHA256Desc process(DataT data) const &
Process data and return the next hash state.
Definition: sha256.cpp:30
RangeT::end
auto end() const
The past-end iterator of this range.
libkazv-config.hpp
Kazv::SHA256Desc
Definition: sha256.hpp:20
Kazv::SHA256Desc::~SHA256Desc
~SHA256Desc()