27#include <openssl/md5.h>
28#include <openssl/sha.h>
34 auto sha1Base = (
unsigned char const*) input.c_str();
35 unsigned char sha1Hash[SHA_DIGEST_LENGTH];
36 size_t sha1Size = input.size();
37 SHA1(sha1Base, sha1Size, sha1Hash);
38 string ret((
char*) sha1Hash, SHA_DIGEST_LENGTH);
46 auto sha2Base = (
unsigned char const*) input.c_str();
47 unsigned char sha2Hash[SHA224_DIGEST_LENGTH];
48 size_t sha2Size = input.size();
49 SHA224(sha2Base, sha2Size, sha2Hash);
50 string ret((
char*) sha2Hash, SHA224_DIGEST_LENGTH);
58 auto sha2Base = (
unsigned char const*) input.c_str();
59 unsigned char sha2Hash[SHA256_DIGEST_LENGTH];
60 size_t sha2Size = input.size();
61 SHA256(sha2Base, sha2Size, sha2Hash);
62 string ret((
char*) sha2Hash, SHA256_DIGEST_LENGTH);
70 auto sha2Base = (
unsigned char const*) input.c_str();
71 unsigned char sha2Hash[SHA384_DIGEST_LENGTH];
72 size_t sha2Size = input.size();
73 SHA384(sha2Base, sha2Size, sha2Hash);
74 string ret((
char*) sha2Hash, SHA384_DIGEST_LENGTH);
82 auto sha2Base = (
unsigned char const*) input.c_str();
83 unsigned char sha2Hash[SHA512_DIGEST_LENGTH];
84 size_t sha2Size = input.size();
85 SHA512(sha2Base, sha2Size, sha2Hash);
86 string ret((
char*) sha2Hash, SHA512_DIGEST_LENGTH);
94 auto md5Base = (
unsigned char const*) input.c_str();
95 unsigned char md5Hash[MD5_DIGEST_LENGTH];
96 size_t md5Size = input.size();
97 MD5(md5Base, md5Size, md5Hash);
98 string ret((
char*) md5Hash, MD5_DIGEST_LENGTH);
112 throw Exception(__PRETTY_FUNCTION__, 1,
"Cannot verify an empty hash");
115 auto verifyer = hashTypeTable.
getEngine(hash);
116 if (verifyer.use_count() == 0) {
118 "Could not determine a HashingEngine that is able to verify the given hash");
121 return verifyer->verifyHash(password, hash);
Exception class that can be used by apps to catch errors resulting from nawa function calls.
virtual std::shared_ptr< HashingEngine > getEngine(std::string hash) const =0
virtual std::string generateHash(std::string input) const =0
A bunch of useful cryptographic functions (esp. hashing), acting as a wrapper to C crypto libraries.
std::string sha512(std::string const &input, bool hex=true)
bool passwordVerify(std::string const &password, std::string const &hash, hashing::HashTypeTable const &hashTypeTable=hashing::DefaultHashTypeTable())
std::string passwordHash(std::string const &password, hashing::HashingEngine const &hashingEngine=hashing::BcryptHashingEngine())
std::string sha1(std::string const &input, bool hex=true)
std::string md5(std::string const &input, bool hex=true)
std::string sha224(std::string const &input, bool hex=true)
std::string sha384(std::string const &input, bool hex=true)
std::string sha256(std::string const &input, bool hex=true)
std::string hexDump(std::string const &in)
Contains useful functions that improve the readability and facilitate maintenance of the NAWA code.