digestpp  0.01
Experimental C++11 header-only message digest library.
blake2_mixin.hpp
1 /*
2 This code is written by kerukuro and released into public domain.
3 */
4 
5 #ifndef DIGESTPP_MIXINS_BLAKE2_HPP
6 #define DIGESTPP_MIXINS_BLAKE2_HPP
7 
8 namespace digestpp
9 {
10 
11 namespace mixin
12 {
13 
14 /**
15  * \brief Defines additional public functions for BLAKE2 family of algorithms.
16  * \sa hasher, blake2s, blake2b, blake2sx, blake2bx, blake2sx_xof, blake2bx_xof
17  */
18 template<typename T>
20 {
21 public:
22  /**
23  * \brief Set salt from std::string
24  *
25  * Supported salt size is 8 bytes for BLAKE2s and 16 bytes for BLAKE2b.
26  *
27  * \param[in] salt String with salt
28  * \throw std::runtime_error if salt size is not supported.
29  * \return Reference to hasher
30  */
32  {
33  return set_salt(salt.c_str(), salt.size());
34  }
35 
36  /**
37  * \brief Set salt from raw buffer
38  *
39  * Supported salt size is 8 bytes for BLAKE2s and 16 bytes for BLAKE2b.
40  *
41  * \param[in] salt Pointer to salt bytes
42  * \param[in] salt_len Salt length (in bytes)
43  * \throw std::runtime_error if salt size is not supported.
44  * \return Reference to hasher
45  */
46  template<typename C, typename std::enable_if<detail::is_byte<C>::value>::type* = nullptr>
48  {
49  auto& blake = static_cast<hasher<T, mixin::blake2_mixin>&>(*this);
50  blake.provider.set_salt(reinterpret_cast<const unsigned char*>(salt), salt_len);
52  return blake;
53  }
54 
55  /**
56  * \brief Set personalization from std::string
57  *
58  * Supported personalization size is 8 bytes for BLAKE2s and 16 bytes for BLAKE2b.
59  *
60  * \param[in] personalization String with personalization
61  * \throw std::runtime_error if personalization size is not supported.
62  * \return Reference to hasher
63  */
65  {
67  }
68 
69  /**
70  * \brief Set personalization from raw buffer
71  *
72  * Supported personalization size is 8 bytes for BLAKE2s and 16 bytes for BLAKE2b.
73  *
74  * \param[in] personalization Pointer to personalization bytes
75  * \param[in] personalization_len Personalization length (in bytes)
76  * \throw std::runtime_error if personalization size is not supported.
77  * \return Reference to hasher
78  */
79  template<typename C, typename std::enable_if<detail::is_byte<C>::value>::type* = nullptr>
81  {
82  auto& blake = static_cast<hasher<T, mixin::blake2_mixin>&>(*this);
83  blake.provider.set_personalization(reinterpret_cast<const unsigned char*>(personalization), personalization_len);
85  return blake;
86  }
87 
88  /**
89  * \brief Set key from std::string
90  *
91  * Maximum key size is 32 bytes for BLAKE2s and 64 bytes for BLAKE2b.
92  *
93  * \param[in] key String with key
94  * \throw std::runtime_error if key size is not supported.
95  * \return Reference to hasher
96  */
98  {
99  auto& blake = static_cast<hasher<T, mixin::blake2_mixin>&>(*this);
101  blake.provider.init();
102  return blake;
103  }
104 
105  /**
106  * \brief Set key from raw buffer
107  *
108  * Maximum key size is 32 bytes for BLAKE2s and 64 bytes for BLAKE2b.
109  *
110  * \param[in] key Pointer to key bytes
111  * \param[in] key_len Key length (in bytes)
112  * \throw std::runtime_error if key size is not supported.
113  * \return Reference to hasher
114  */
115  template<typename C, typename std::enable_if<detail::is_byte<C>::value>::type* = nullptr>
117  {
118  return set_key(std::string(reinterpret_cast<const char*>(key), key_len));
119  }
120 };
121 
122 
123 } // namespace mixin
124 
125 } // namespace digestpp
126 
127 #endif
hasher< detail::shake_provider< 256, 24 > > shake256
SHAKE256 function.
Definition: shake.hpp:53
hasher< T, mixin::blake2_mixin > & set_key(const C *key, size_t key_len)
Set key from raw buffer.
Definition: blake2_mixin.hpp:116