lhashes  1.0.0
C++ hashes library
lhashes_hash.h
Go to the documentation of this file.
1 /*
2  * ======================================================================================
3  * __ ____ __ __ ____ ___
4  * ( ) (_ _)( \/ )( ___)/ __)
5  * )(__ _)(_ ) ( )__) \__ \
6  * (____)(____)(_/\/\_)(____)(___/
7  *
8  * This file is part of the Limes open source library and is licensed under the terms of the GNU Public License.
9  *
10  * Commercial licenses are available; contact the maintainers at ben.the.vining@gmail.com to inquire for details.
11  *
12  * ======================================================================================
13  */
14 
15 #pragma once
16 
17 #include <cstddef> // for size_t
18 #include <string> // for string
19 #include <string_view> // for string_view
20 #include <memory> // for unique_ptr
21 #include "lhashes/lhashes_Export.h"
22 
29 namespace limes::hash
30 {
31 
39 enum class LHASH_EXPORT Type
40 {
41  md5,
42  sha1,
46  sha512
47 };
48 
68 class LHASH_EXPORT Hasher
69 {
70 public:
72  virtual ~Hasher() = default;
73 
76 
78  virtual void update (const unsigned char* input, std::size_t length) = 0;
79 
80  virtual void update (std::string_view input);
81 
83 
94  [[nodiscard]] virtual std::string getHash() = 0;
95 
97  [[nodiscard]] virtual std::size_t getLengthOfHash() const = 0;
98 };
99 
105 [[nodiscard]] LHASH_EXPORT std::unique_ptr<Hasher> createHasherForType (Type type);
106 
112 [[nodiscard]] LHASH_EXPORT std::string hash (Type type, const char* input, std::size_t length);
113 
119 [[nodiscard]] LHASH_EXPORT std::string hash (Type type, std::string_view input);
120 
123 } // namespace limes::hash
A base class representing an object that calculates a hash function.
Definition: lhashes_hash.h:69
LHASH_EXPORT std::unique_ptr< Hasher > createHasherForType(Type type)
Creates an appropriate Hasher for the given Type.
virtual void update(std::string_view input)
Updates the internal state of the hasher with some new data.
LHASH_EXPORT std::string hash(Type type, std::string_view input)
Calculates a hash value for the input data using a hasher appropriate for the desired type.
LHASH_EXPORT std::string hash(Type type, const char *input, std::size_t length)
Calculates a hash value for the input data using a hasher appropriate for the desired type.
virtual std::string getHash()=0
Retrieves the calculated hash value as a string.
virtual std::size_t getLengthOfHash() const =0
Returns the length of a hash string for this algorithm.
virtual ~Hasher()=default
Destructor.
virtual void update(const unsigned char *input, std::size_t length)=0
Updates the internal state of the hasher with some new data.
md5
An MD5 hash.
Definition: lhashes_hash.h:41
sha224
An SHA224 hash.
Definition: lhashes_hash.h:43
sha384
An SHA384 hash.
Definition: lhashes_hash.h:45
sha256
An SHA256 hash.
Definition: lhashes_hash.h:44
sha1
An SHA1 hash.
Definition: lhashes_hash.h:42
Cryptographic hash functions.