Skip to content

Commit 7d1c1d9

Browse files
author
Levent KARAGÖL
authored
Merge pull request #12 from leventkaragol/added-hmac-sha-256-hashing
added-hmac-sha-256-hashing
2 parents b62becb + cbfc24d commit 7d1c1d9

4 files changed

Lines changed: 84 additions & 0 deletions

File tree

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Easy-to-use, symmetric (AES-256) and asymmetric (RSA) encryption and also hash (
1616
* [How to add it to my project](#how-to-add-it-to-my-project)
1717
* [How to use? (Symmetric Encryption with AES)](#how-to-use-symmetric-encryption-with-aes)
1818
* [How to use? (Hash with SHA-256)](#how-to-use-hash-with-sha-256)
19+
* [How to use? (Hash with HMAC SHA-256)](#how-to-use-hash-with-hmac-sha-256)
1920
* [How to use? (Asymmetric Encryption with RSA)](#how-to-use-asymmetric-encryption-with-rsa)
2021
* [How do I generate Public/Private Keys?](#how-do-i-generate-publicprivate-keys)
2122
* [Relationship between key size and max text length that can be encrypted](#relationship-between-key-size-and-max-text-length-that-can-be-encrypted)
@@ -102,6 +103,29 @@ int main() {
102103
}
103104
```
104105

106+
## How to use? (Hash with HMAC SHA-256)
107+
108+
All you need to do is call the **"hashWithHMACSHA256"** function to hash the given text by given secret key with HMAC SHA-256.
109+
110+
```cpp
111+
#include "libcpp-crypto.hpp"
112+
113+
using namespace lklibs;
114+
115+
int main() {
116+
117+
auto plainText = "This text will be hashed soon";
118+
119+
const auto key = "mySecretKey";
120+
121+
const auto hashedText = CryptoService::hashWithHMACSHA256(plainText, key);
122+
123+
std::cout << "Hash: " << hashedText << std::endl;
124+
125+
return 0;
126+
}
127+
```
128+
105129
## How to use? (Asymmetric Encryption with RSA)
106130

107131
To encrypt and decrypt the given text with RSA, all you need to do is call the **"encryptWithRSA"** and

examples/main.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,16 @@ void hash()
252252
std::cout << "Hash: " << hashText << std::endl;
253253
}
254254

255+
void hashWithHMACSHA256()
256+
{
257+
const auto plainText = "This text will be hashed soon";
258+
const auto key = "mySecretKey";
259+
260+
const auto hashText = CryptoService::hashWithHMACSHA256(plainText, key);
261+
262+
std::cout << "Hash: " << hashText << std::endl;
263+
}
264+
255265
int main()
256266
{
257267
// Symmetric Encryption with AES
@@ -290,5 +300,7 @@ int main()
290300

291301
hash();
292302

303+
hashWithHMACSHA256();
304+
293305
return 0;
294306
}

src/libcpp-crypto.hpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ SOFTWARE.
3636
#include <openssl/aes.h>
3737
#include <openssl/pem.h>
3838
#include <openssl/rand.h>
39+
#include <openssl/hmac.h>
3940
#include <string>
4041
#include <cstring>
4142
#include <iomanip>
@@ -735,6 +736,43 @@ namespace lklibs
735736

736737
return "";
737738
}
739+
740+
/**
741+
* @brief Hashes the given string with HMAC SHA-256
742+
*
743+
* @param plainText String to hash
744+
* @param key key to use for hashing plainText
745+
*
746+
* @return Hashed string
747+
*/
748+
inline std::string hashWithHMACSHA256(const std::string& plainText, const std::string& key)
749+
{
750+
const auto keyBytes = reinterpret_cast<const unsigned char*>(key.c_str());
751+
const size_t keyLength = key.length();
752+
753+
const auto data = reinterpret_cast<const unsigned char*>(plainText.c_str());
754+
const size_t dataLength = plainText.length();
755+
756+
unsigned char result[SHA256_DIGEST_LENGTH];
757+
unsigned int resultLength;
758+
759+
if (HMAC(EVP_sha256(), keyBytes, static_cast<int>(keyLength), data, static_cast<int>(dataLength), result, &resultLength) == nullptr)
760+
{
761+
std::cerr << "Error: HMAC calculation failed." << std::endl;
762+
763+
return "";
764+
}
765+
766+
const std::vector hashBytes(result, result + resultLength);
767+
768+
std::stringstream ss;
769+
for (const unsigned char byte : hashBytes)
770+
{
771+
ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(byte);
772+
}
773+
774+
return ss.str();
775+
}
738776
}
739777
}
740778

test/test.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,16 @@ TEST(HashTest, HashWithSHA256MustBeCompletedSuccessfully)
494494
ASSERT_EQ(hashText, "d32448bab2777b376a5592e384146c3c0182ba589e2521bd0275f2cef6a50546") << "Hash is invalid";
495495
}
496496

497+
TEST(HmacSha256Test, HashWithHMACSHA256MustBeCompletedSuccessfully)
498+
{
499+
const auto plainText = "This text will be hashed soon";
500+
const auto key = "mySecretKey";
501+
502+
const auto hashText = CryptoService::hashWithHMACSHA256(plainText, key);
503+
504+
ASSERT_EQ(hashText, "875dae56af4cb9c9c1b2e7f30e28704da4f1933bf85bb180409761f9c4721186") << "Hash is invalid";
505+
}
506+
497507
int main(int argc, char** argv)
498508
{
499509
testing::InitGoogleTest(&argc, argv);

0 commit comments

Comments
 (0)