1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| #ifndef CRYPTOGRAPHICHASH_H #define CRYPTOGRAPHICHASH_H
#include <map> #include <string> #include <openssl/evp.h> #include <openssl/sha.h> #include <openssl/md5.h>
enum class HashType : char { Md5, Sha1, Sha224, Sha256, Sha384, Sha512, Sha3_224, Sha3_256, Sha3_384, Sha3_512, };
using hashFunc = const EVP_MD* (*)(); const std::map<HashType, hashFunc> HashMethods = { {HashType::Md5, EVP_md5}, {HashType::Sha1, EVP_sha1}, {HashType::Sha224, EVP_sha224}, {HashType::Sha256, EVP_sha256}, {HashType::Sha384, EVP_sha384}, {HashType::Sha512, EVP_sha512}, {HashType::Sha3_224, EVP_sha3_224}, {HashType::Sha3_256, EVP_sha3_256}, {HashType::Sha3_384, EVP_sha3_384}, {HashType::Sha3_512, EVP_sha3_512}, };
const std::map<HashType, int> HashLength { {HashType::Md5,MD5_DIGEST_LENGTH}, {HashType::Sha1,SHA_DIGEST_LENGTH}, {HashType::Sha224,SHA224_DIGEST_LENGTH}, {HashType::Sha256,SHA256_DIGEST_LENGTH}, {HashType::Sha384,SHA384_DIGEST_LENGTH}, {HashType::Sha512,SHA512_DIGEST_LENGTH}, {HashType::Sha3_224,SHA224_DIGEST_LENGTH}, {HashType::Sha3_256,SHA256_DIGEST_LENGTH}, {HashType::Sha3_384,SHA384_DIGEST_LENGTH}, {HashType::Sha3_512,SHA512_DIGEST_LENGTH}, };
class CryptographicHash { public: enum class Type : char { Binary, Hex };
explicit CryptographicHash(HashType hashType);
~CryptographicHash();
void addData(const std::string &data) const;
void addData(const char *data, int length) const;
std::string result(Type type = Type::Hex) const;
private: EVP_MD_CTX *m_ctx; HashType m_hashType; };
#endif
|