libhasha 2.2.2
Loading...
Searching...
No Matches
hasha::Hasher Class Reference

C++ wrapper for the EVP hasher. More...

#include <evpp.h>

Public Member Functions

 Hasher (ha_evp_hashty type=HA_EVPTY_UNDEFINED, size_t digestlen=0)
 ~Hasher ()=default
 Hasher (const Hasher &)=delete
Hasheroperator= (const Hasher &)=delete
 Hasher (Hasher &&) noexcept=default
Hasheroperator= (Hasher &&) noexcept=default
auto setType (ha_evp_hashty type) -> Hasher &
auto setDigestLength (size_t length) -> Hasher &
auto setup (ha_evp_hashty hashty, size_t length=0) -> Hasher &
auto getType () const
auto getType (ha_evp_hashty &hashty) -> Hasher &
auto getDigestLength () const
auto getDigestLength (size_t &digestlen) -> Hasher &
auto init () -> Hasher &
auto update (const void *data, size_t length) -> Hasher &
auto update (const std::vector< uint8_t > &data) -> Hasher &
auto update (const char *str) -> Hasher &
auto update (const std::string &str) -> Hasher &
auto final (uint8_t *digest) -> Hasher &
auto final (std::vector< uint8_t > &digest) -> Hasher &
auto final ()
auto hash (const uint8_t *data, size_t length, uint8_t *digest) -> Hasher &
auto hash (const std::vector< uint8_t > &data, std::vector< uint8_t > &digest) -> Hasher &
auto hash (const std::string &str, std::vector< uint8_t > &digest) -> Hasher &
auto commit () -> Hasher &
auto ptr ()
auto ref () -> Hasher &

Detailed Description

C++ wrapper for the EVP hasher.

This class provides a C++ interface for using the EVP hasher, making it easier to work with hashing algorithms in object-oriented code.

Definition at line 181 of file evpp.h.

Constructor & Destructor Documentation

◆ Hasher() [1/3]

hasha::Hasher::Hasher ( ha_evp_hashty type = HA_EVPTY_UNDEFINED,
size_t digestlen = 0 )
inlineexplicit

Definition at line 184 of file evpp.h.

187 {
188 if (!hasher_) throw std::runtime_error("Failed to create EVP hasher");
189 setup(type, digestlen);
190 commit();
191 }
auto commit() -> Hasher &
Definition evpp.h:310
auto setup(ha_evp_hashty hashty, size_t length=0) -> Hasher &
Definition evpp.h:217
HA_PUBFUN struct ha_evp_hasher * ha_evp_hasher_new(void)
Creates a new EVP hasher. ( malloc(g_ha_evp_hasher_size) )
HA_PUBFUN void ha_evp_hasher_delete(struct ha_evp_hasher *ptr)
Frees the memory of an EVP hasher.

◆ ~Hasher()

hasha::Hasher::~Hasher ( )
default

◆ Hasher() [2/3]

hasha::Hasher::Hasher ( const Hasher & )
delete

◆ Hasher() [3/3]

hasha::Hasher::Hasher ( Hasher && )
defaultnoexcept

Member Function Documentation

◆ commit()

auto hasha::Hasher::commit ( ) -> Hasher &
inline

Definition at line 310 of file evpp.h.

311 {
312 if (hasher_) ha_evp_hasher_reinit(hasher_.get(), hashty_, digestlen_);
313 return *this;
314 }
HA_PUBFUN void ha_evp_hasher_reinit(struct ha_evp_hasher *hasher, enum ha_evp_hashty hashty, size_t digestlen)
Reinitializes the EVP hasher with a new algorithm and digest length.

◆ final() [1/3]

auto hasha::Hasher::final ( )
inline

Definition at line 281 of file evpp.h.

282 {
283 std::vector<uint8_t> digest(digestlen_);
284 final(digest);
285 return digest;
286 }
std::vector< byte > digest
Definition evpp.h:25

◆ final() [2/3]

auto hasha::Hasher::final ( std::vector< uint8_t > & digest) -> Hasher &
inline

Definition at line 274 of file evpp.h.

275 {
276 digest.resize(digestlen_);
277 final(digest.data());
278 return *this;
279 }

◆ final() [3/3]

auto hasha::Hasher::final ( uint8_t * digest) -> Hasher &
inline

Definition at line 268 of file evpp.h.

269 {
270 ha_evp_final(hasher_.get(), digest);
271 return *this;
272 }
HA_PUBFUN void ha_evp_final(struct ha_evp_hasher *hasher, ha_digest_t digest)
Finalizes the EVP hash and produces the output digest. ( like ha_final(hash, ctx, digest,...

◆ getDigestLength() [1/2]

auto hasha::Hasher::getDigestLength ( ) const
inline

Definition at line 232 of file evpp.h.

232{ return digestlen_; }

◆ getDigestLength() [2/2]

auto hasha::Hasher::getDigestLength ( size_t & digestlen) -> Hasher &
inline

Definition at line 234 of file evpp.h.

235 {
236 digestlen = digestlen_;
237 return *this;
238 }

◆ getType() [1/2]

auto hasha::Hasher::getType ( ) const
inline

Definition at line 224 of file evpp.h.

224{ return hashty_; }

◆ getType() [2/2]

auto hasha::Hasher::getType ( ha_evp_hashty & hashty) -> Hasher &
inline

Definition at line 226 of file evpp.h.

227 {
228 hashty = hashty_;
229 return *this;
230 }

◆ hash() [1/3]

auto hasha::Hasher::hash ( const std::string & str,
std::vector< uint8_t > & digest ) -> Hasher &
inline

Definition at line 303 of file evpp.h.

305 {
306 std::vector<uint8_t> data(str.begin(), str.end());
307 return hash(data, digest);
308 }
auto hash(const uint8_t *data, size_t length, uint8_t *digest) -> Hasher &
Definition evpp.h:288

◆ hash() [2/3]

auto hasha::Hasher::hash ( const std::vector< uint8_t > & data,
std::vector< uint8_t > & digest ) -> Hasher &
inline

Definition at line 295 of file evpp.h.

297 {
298 digest.resize(digestlen_);
299 hash(data.data(), data.size(), digest.data());
300 return *this;
301 }

◆ hash() [3/3]

auto hasha::Hasher::hash ( const uint8_t * data,
size_t length,
uint8_t * digest ) -> Hasher &
inline

Definition at line 288 of file evpp.h.

290 {
291 ha_evp_hash(hasher_.get(), data, length, digest);
292 return *this;
293 }
HA_PUBFUN void ha_evp_hash(struct ha_evp_hasher *hasher, ha_inbuf_t buf, size_t len, ha_digest_t digest)
Computes the EVP hash in a single (hash) operation. ( like ha_hash(hash, buf, len,...

◆ init()

auto hasha::Hasher::init ( ) -> Hasher &
inline

Definition at line 240 of file evpp.h.

241 {
242 ha_evp_init(hasher_.get());
243 return *this;
244 }
HA_PUBFUN void ha_evp_init(struct ha_evp_hasher *hasher)
Initializes the EVP hash. ( like ha_init(hash, ctx) )

◆ operator=() [1/2]

Hasher & hasha::Hasher::operator= ( const Hasher & )
delete

◆ operator=() [2/2]

Hasher & hasha::Hasher::operator= ( Hasher && )
defaultnoexcept

◆ ptr()

auto hasha::Hasher::ptr ( )
inline

Definition at line 316 of file evpp.h.

316{ return this; }

◆ ref()

auto hasha::Hasher::ref ( ) -> Hasher &
inline

Definition at line 318 of file evpp.h.

318{ return *this; }

◆ setDigestLength()

auto hasha::Hasher::setDigestLength ( size_t length) -> Hasher &
inline

Definition at line 210 of file evpp.h.

211 {
212 digestlen_ = length;
214 return *this;
215 }
#define ha_evpp_auto_commit()
Definition evpp.h:14

◆ setType()

auto hasha::Hasher::setType ( ha_evp_hashty type) -> Hasher &
inline

Definition at line 199 of file evpp.h.

200 {
201 signed long tmp = 0;
202
203 hashty_ = type;
204 if ((tmp = ha_evp_hashty_get_digestlen(type)) > 0)
205 setDigestLength(tmp);
206
207 return *this;
208 }
auto setDigestLength(size_t length) -> Hasher &
Definition evpp.h:210
HA_PUBFUN signed long ha_evp_hashty_get_digestlen(enum ha_evp_hashty hashty)
Get fixed hash size.

◆ setup()

auto hasha::Hasher::setup ( ha_evp_hashty hashty,
size_t length = 0 ) -> Hasher &
inline

Definition at line 217 of file evpp.h.

218 {
219 setType(hashty);
220 if (length) setDigestLength(length);
221 return *this;
222 }
auto setType(ha_evp_hashty type) -> Hasher &
Definition evpp.h:199

◆ update() [1/4]

auto hasha::Hasher::update ( const char * str) -> Hasher &
inline

Definition at line 258 of file evpp.h.

259 {
260 return update(str, strlen(str));
261 }
auto update(const void *data, size_t length) -> Hasher &
Definition evpp.h:246

◆ update() [2/4]

auto hasha::Hasher::update ( const std::string & str) -> Hasher &
inline

Definition at line 263 of file evpp.h.

264 {
265 return update(str.data(), str.size());
266 }

◆ update() [3/4]

auto hasha::Hasher::update ( const std::vector< uint8_t > & data) -> Hasher &
inline

Definition at line 253 of file evpp.h.

254 {
255 return update(data.data(), data.size());
256 }

◆ update() [4/4]

auto hasha::Hasher::update ( const void * data,
size_t length ) -> Hasher &
inline

Definition at line 246 of file evpp.h.

247 {
248 ha_evp_update(hasher_.get(), static_cast<const uint8_t *>(data),
249 length);
250 return *this;
251 }
HA_PUBFUN void ha_evp_update(struct ha_evp_hasher *hasher, ha_inbuf_t buf, size_t len)
Updates the EVP hash with input data. ( like ha_update(hash, ctx, buf, len) )

The documentation for this class was generated from the following file: