NAWA 0.9
Web Application Framework for C++
crypto.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2019-2022 Tobias Flaig.
3 *
4 * This file is part of nawa.
5 *
6 * nawa is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License,
8 * version 3, as published by the Free Software Foundation.
9 *
10 * nawa is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with nawa. If not, see <https://www.gnu.org/licenses/>.
17 */
18
24#include <nawa/Exception.h>
25#include <nawa/util/crypto.h>
26#include <nawa/util/utils.h>
27#include <openssl/md5.h>
28#include <openssl/sha.h>
29
30using namespace nawa;
31using namespace std;
32
33std::string crypto::sha1(std::string const& input, bool hex) {
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);
39 if (hex) {
40 return utils::hexDump(ret);
41 }
42 return ret;
43}
44
45std::string crypto::sha224(std::string const& input, bool hex) {
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);
51 if (hex) {
52 return utils::hexDump(ret);
53 }
54 return ret;
55}
56
57std::string crypto::sha256(std::string const& input, bool hex) {
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);
63 if (hex) {
64 return utils::hexDump(ret);
65 }
66 return ret;
67}
68
69std::string crypto::sha384(std::string const& input, bool hex) {
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);
75 if (hex) {
76 return utils::hexDump(ret);
77 }
78 return ret;
79}
80
81std::string crypto::sha512(std::string const& input, bool hex) {
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);
87 if (hex) {
88 return utils::hexDump(ret);
89 }
90 return ret;
91}
92
93std::string crypto::md5(std::string const& input, bool hex) {
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);
99 if (hex) {
100 return utils::hexDump(ret);
101 }
102 return ret;
103}
104
105std::string crypto::passwordHash(std::string const& password, hashing::HashingEngine const& hashingEngine) {
106 // use the provided HashingEngine for generation
107 return hashingEngine.generateHash(password);
108}
109
110bool crypto::passwordVerify(std::string const& password, std::string const& hash, hashing::HashTypeTable const& hashTypeTable) {
111 if (hash.empty()) {
112 throw Exception(__PRETTY_FUNCTION__, 1, "Cannot verify an empty hash");
113 }
114
115 auto verifyer = hashTypeTable.getEngine(hash);
116 if (verifyer.use_count() == 0) {
117 throw Exception(__PRETTY_FUNCTION__, 2,
118 "Could not determine a HashingEngine that is able to verify the given hash");
119 }
120
121 return verifyer->verifyHash(password, hash);
122}
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)
Definition: crypto.cpp:81
bool passwordVerify(std::string const &password, std::string const &hash, hashing::HashTypeTable const &hashTypeTable=hashing::DefaultHashTypeTable())
Definition: crypto.cpp:110
std::string passwordHash(std::string const &password, hashing::HashingEngine const &hashingEngine=hashing::BcryptHashingEngine())
Definition: crypto.cpp:105
std::string sha1(std::string const &input, bool hex=true)
Definition: crypto.cpp:33
std::string md5(std::string const &input, bool hex=true)
Definition: crypto.cpp:93
std::string sha224(std::string const &input, bool hex=true)
Definition: crypto.cpp:45
std::string sha384(std::string const &input, bool hex=true)
Definition: crypto.cpp:69
std::string sha256(std::string const &input, bool hex=true)
Definition: crypto.cpp:57
std::string hexDump(std::string const &in)
Definition: utils.cpp:247
Definition: AppInit.h:31
Contains useful functions that improve the readability and facilitate maintenance of the NAWA code.