-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibcpp-crypto.hpp
More file actions
779 lines (624 loc) · 25.3 KB
/
Copy pathlibcpp-crypto.hpp
File metadata and controls
779 lines (624 loc) · 25.3 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
/*
Easy-to-use, symmetric (AES-256) and asymmetric (RSA) encryption and also hash (SHA-256) library for C++ (17+)
version 1.3.1
https://github.com/leventkaragol/libcpp-crypto
If you encounter any issues, please submit a ticket at https://github.com/leventkaragol/libcpp-crypto/issues
Copyright (c) 2024 Levent KARAGÖL
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef LIBCPP_CRYPTO_HPP
#define LIBCPP_CRYPTO_HPP
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/aes.h>
#include <openssl/pem.h>
#include <openssl/rand.h>
#include <openssl/hmac.h>
#include <string>
#include <cstring>
#include <iomanip>
#include <sstream>
#include <vector>
#include <memory>
#include <stdexcept>
#include <iostream>
namespace lklibs
{
/**
* @brief Base Exception class for crypto operations
*/
struct CryptoException : public std::runtime_error
{
public:
explicit CryptoException(const std::string& message) : std::runtime_error(message)
{
}
};
/**
* @brief Exception class for invalid key errors
*/
struct InvalidKeyException : public CryptoException
{
public:
explicit InvalidKeyException(const std::string& message) : CryptoException(message)
{
}
};
/**
* @brief Exception class for invalid public key errors
*/
struct InvalidPublicKeyException : public CryptoException
{
public:
explicit InvalidPublicKeyException(const std::string& message) : CryptoException(message)
{
}
};
/**
* @brief Exception class for invalid private key errors
*/
struct InvalidPrivateKeyException : public CryptoException
{
public:
explicit InvalidPrivateKeyException(const std::string& message) : CryptoException(message)
{
}
};
/**
* @brief Exception class for corrupted text errors
*/
struct CorruptedTextException : public CryptoException
{
public:
explicit CorruptedTextException(const std::string& message) : CryptoException(message)
{
}
};
/**
* @brief Exception class for text too long for public key errors
*/
struct TextTooLongForPublicKeyException : public CryptoException
{
public:
explicit TextTooLongForPublicKeyException(const std::string& message) : CryptoException(message)
{
}
};
/**
* @brief RSA public and private key pair
*/
struct RSAKeyPair
{
std::string publicKey;
std::string privateKey;
};
/**
* @brief Crypto service class for encryption and decryption operations
*/
namespace CryptoService
{
struct EVP_CIPHER_CTX_Deleter
{
void operator()(EVP_CIPHER_CTX* ptr) const { EVP_CIPHER_CTX_free(ptr); }
};
struct EVP_MD_CTX_Deleter
{
void operator()(EVP_MD_CTX* ctx) const
{
EVP_MD_CTX_free(ctx);
}
};
/**
* @brief Checks if the given character is a base64 character
*
* @param c Character to check
*
* @return True if the character is a base64 character, false otherwise
*/
static bool isBase64(unsigned char c)
{
return (isalnum(c) || (c == '+') || (c == '/'));
}
/**
* @brief Encodes the given input string to base64
*
* @param input Input string to encode
*
* @return Encoded base64 string
*/
static std::string encode(const std::string& input)
{
static const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
std::string ret;
int i = 0;
unsigned char char_array_3[3];
unsigned char char_array_4[4];
for (auto c : input)
{
char_array_3[i++] = c;
if (i == 3)
{
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for (i = 0; i < 4; i++)
{
ret += base64_chars[char_array_4[i]];
}
i = 0;
}
}
if (i)
{
for (int j = i; j < 3; j++)
{
char_array_3[j] = '\0';
}
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
for (int j = 0; j < i + 1; j++)
{
ret += base64_chars[char_array_4[j]];
}
while (i++ < 3)
{
ret += '=';
}
}
return ret;
}
/**
* @brief Decodes the given base64 string to original string
*
* @param input Base64 string to decode
*
* @return Decoded original string
*/
static std::string decode(const std::string& input)
{
static const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
std::string ret;
int i = 0;
unsigned char char_array_4[4], char_array_3[3];
for (auto c : input)
{
if (c == '=' || !isBase64(c))
{
break;
}
char_array_4[i++] = c;
if (i == 4)
{
for (i = 0; i < 4; i++)
{
char_array_4[i] = static_cast<unsigned char>(base64_chars.find(char_array_4[i]));
}
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (i = 0; i < 3; i++)
{
ret += char_array_3[i];
}
i = 0;
}
}
if (i)
{
for (int j = i; j < 4; j++)
{
char_array_4[j] = 0;
}
for (unsigned char& j : char_array_4)
{
size_t index = base64_chars.find(j);
if (index != std::string::npos)
{
j = static_cast<unsigned char>(index);
}
else
{
j = 0;
}
}
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (int j = 0; j < i - 1; j++)
{
ret += char_array_3[j];
}
}
return ret;
}
static int encrypt(const unsigned char* plaintext, int plaintext_len, const unsigned char* key, unsigned char* iv, unsigned char* ciphertext)
{
std::unique_ptr<EVP_CIPHER_CTX, EVP_CIPHER_CTX_Deleter> ctx(EVP_CIPHER_CTX_new());
int len;
int ciphertext_len;
if (!ctx)
{
throw CryptoException("Failed to create OpenSSL context for encryption");
}
if (1 != EVP_EncryptInit_ex(ctx.get(), EVP_aes_256_cbc(), nullptr, key, iv))
{
throw CryptoException("Failed to initialize OpenSSL encryption operation");
}
if (1 != EVP_EncryptUpdate(ctx.get(), ciphertext, &len, plaintext, plaintext_len))
{
throw CryptoException("Failed to update OpenSSL encryption operation");
}
ciphertext_len = len;
if (1 != EVP_EncryptFinal_ex(ctx.get(), ciphertext + len, &len))
{
throw CryptoException("Failed to finalize OpenSSL encryption operation");
}
ciphertext_len += len;
return ciphertext_len;
}
static int decrypt(const unsigned char* ciphertext, int ciphertext_len, const unsigned char* key, unsigned char* iv, unsigned char* plaintext)
{
std::unique_ptr<EVP_CIPHER_CTX, EVP_CIPHER_CTX_Deleter> ctx(EVP_CIPHER_CTX_new());
int len;
int plaintext_len;
if (!ctx)
{
throw CryptoException("Failed to create OpenSSL context for decryption");
}
if (1 != EVP_DecryptInit_ex(ctx.get(), EVP_aes_256_cbc(), nullptr, key, iv))
{
throw CryptoException("Failed to initialize OpenSSL decryption operation");
}
if (1 != EVP_DecryptUpdate(ctx.get(), plaintext, &len, ciphertext, ciphertext_len))
{
throw CryptoException("Failed to update OpenSSL decryption operation");
}
plaintext_len = len;
if (1 != EVP_DecryptFinal_ex(ctx.get(), plaintext + len, &len))
{
if (ERR_GET_REASON(ERR_peek_last_error()) == EVP_R_BAD_DECRYPT)
{
throw InvalidKeyException("Encryption key does not match the original encryption key");
}
else
{
throw CorruptedTextException("Encrypted text is corrupted");
}
}
plaintext_len += len;
return plaintext_len;
}
static void generateRandomIV(std::vector<unsigned char>& iv)
{
if (!RAND_bytes(iv.data(), AES_BLOCK_SIZE))
{
throw CryptoException("Failed to generate random IV");
}
}
static std::string adjustKeyLength(const std::string& key)
{
if (key.size() == 32)
{
return key;
}
else if (key.size() > 32)
{
return key.substr(0, 32);
}
else
{
std::string adjusted_key = key;
adjusted_key.append(32 - key.size(), '0');
return adjusted_key;
}
}
static int passwordCallback(char* buf, int size, int, void* userdata)
{
auto* passphrase = reinterpret_cast<std::string*>(userdata);
if (passphrase->size() > static_cast<size_t>(size))
{
return 0;
}
std::memcpy(buf, passphrase->c_str(), passphrase->size());
return passphrase->size();
}
/**
* @brief Encrypts the given plaintext with the given key using AES-256 encryption
*
* @param plaintext Plaintext to encrypt
* @param key Key to use for encryption
*
* @return Encrypted ciphertext
*/
std::string encryptWithAES(const std::string& plaintext, const std::string& key)
{
std::string adjustedKey = adjustKeyLength(key);
std::vector<unsigned char> iv(AES_BLOCK_SIZE);
generateRandomIV(iv);
std::vector<unsigned char> ciphertext(plaintext.size() + AES_BLOCK_SIZE);
int ciphertext_len = encrypt(reinterpret_cast<const unsigned char*>(plaintext.c_str()), static_cast<int>(plaintext.length()), reinterpret_cast<const unsigned char*>(adjustedKey.c_str()), iv.data(), ciphertext.data());
ciphertext.resize(ciphertext_len);
std::string encrypted = std::string(iv.begin(), iv.end()) + std::string(ciphertext.begin(), ciphertext.end());
return encode(encrypted);
}
/**
* @brief Decrypts the given ciphertext with the given key using AES-256 decryption
*
* @param ciphertext Ciphertext to decrypt
* @param key Key to use for decryption
*
* @return Decrypted plaintext
*/
std::string decryptWithAES(const std::string& ciphertext, const std::string& key)
{
std::string adjustedKey = adjustKeyLength(key);
auto encryptedText = decode(ciphertext);
std::vector<unsigned char> iv(AES_BLOCK_SIZE);
std::copy(encryptedText.begin(), encryptedText.begin() + AES_BLOCK_SIZE, iv.begin());
std::vector<unsigned char> plaintext(encryptedText.size() - AES_BLOCK_SIZE);
int plaintext_len = decrypt(reinterpret_cast<const unsigned char*>(encryptedText.data() + AES_BLOCK_SIZE), static_cast<int>(encryptedText.size() - AES_BLOCK_SIZE), reinterpret_cast<const unsigned char*>(adjustedKey.c_str()), iv.data(), plaintext.data());
plaintext.resize(plaintext_len);
return std::string{plaintext.begin(), plaintext.end()};
}
/**
* @brief Generates RSA public/private key pair
*
* @param keyLength Length of the key to generate (2048, 4096, 8192, etc.)
* @param passphrase Optional passphrase for the private key
*
* @return RSA public/private key pair
*/
RSAKeyPair generateRSAKeyPair(int keyLength, const std::string& passphrase = "")
{
RSAKeyPair keyPair;
EVP_PKEY_CTX* ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, nullptr);
if (!ctx)
{
throw std::runtime_error("Failed to create context for key generation");
}
if (EVP_PKEY_keygen_init(ctx) <= 0)
{
EVP_PKEY_CTX_free(ctx);
throw std::runtime_error("Failed to initialize key generation");
}
if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, keyLength) <= 0)
{
EVP_PKEY_CTX_free(ctx);
throw std::runtime_error("Failed to set key length");
}
EVP_PKEY* pkey = nullptr;
if (EVP_PKEY_keygen(ctx, &pkey) <= 0)
{
EVP_PKEY_CTX_free(ctx);
throw std::runtime_error("Failed to generate RSA key pair");
}
EVP_PKEY_CTX_free(ctx);
BIO* bioPublic = BIO_new(BIO_s_mem());
BIO* bioPrivate = BIO_new(BIO_s_mem());
if (!bioPublic || !bioPrivate)
{
EVP_PKEY_free(pkey);
if (bioPublic)
{
BIO_free(bioPublic);
}
if (bioPrivate)
{
BIO_free(bioPrivate);
}
throw std::runtime_error("Failed to create BIO");
}
if (PEM_write_bio_PUBKEY(bioPublic, pkey) <= 0)
{
EVP_PKEY_free(pkey);
BIO_free(bioPublic);
BIO_free(bioPrivate);
throw std::runtime_error("Failed to write public key");
}
if (passphrase.empty())
{
if (PEM_write_bio_PrivateKey(bioPrivate, pkey, nullptr, nullptr, 0, nullptr, nullptr) <= 0)
{
EVP_PKEY_free(pkey);
BIO_free(bioPublic);
BIO_free(bioPrivate);
throw std::runtime_error("Failed to write private key");
}
}
else
{
if (PEM_write_bio_PrivateKey(bioPrivate, pkey, EVP_aes_256_cbc(), reinterpret_cast<const unsigned char*>(passphrase.c_str()), passphrase.length(), nullptr, nullptr) <= 0)
{
EVP_PKEY_free(pkey);
BIO_free(bioPublic);
BIO_free(bioPrivate);
throw std::runtime_error("Failed to write private key with passphrase");
}
}
char* pubKeyData = nullptr;
long pubKeyLen = BIO_get_mem_data(bioPublic, &pubKeyData);
keyPair.publicKey = std::string(pubKeyData, pubKeyLen);
char* privKeyData = nullptr;
long privKeyLen = BIO_get_mem_data(bioPrivate, &privKeyData);
keyPair.privateKey = std::string(privKeyData, privKeyLen);
BIO_free(bioPublic);
BIO_free(bioPrivate);
EVP_PKEY_free(pkey);
return keyPair;
}
/**
* @brief Encrypts the given plaintext with the given public key using RSA encryption
*
* @param plaintext Plaintext to encrypt
* @param publicKeyStr Public key to use for encryption
*
* @return Encrypted ciphertext
*/
std::string encryptWithRSA(const std::string& plaintext, const std::string& publicKeyStr)
{
auto bioDeleter = [](BIO* bio) { BIO_free(bio); };
std::unique_ptr<BIO, decltype(bioDeleter)> bio(BIO_new_mem_buf(publicKeyStr.data(), -1), bioDeleter);
auto pkeyDeleter = [](EVP_PKEY* pkey) { EVP_PKEY_free(pkey); };
std::unique_ptr<EVP_PKEY, decltype(pkeyDeleter)> publicKey(PEM_read_bio_PUBKEY(bio.get(), nullptr, nullptr, nullptr), pkeyDeleter);
if (!publicKey)
{
throw InvalidPublicKeyException("RSA public key is invalid");
}
auto ctxDeleter = [](EVP_PKEY_CTX* ctx) { EVP_PKEY_CTX_free(ctx); };
std::unique_ptr<EVP_PKEY_CTX, decltype(ctxDeleter)> ctx(EVP_PKEY_CTX_new(publicKey.get(), nullptr), ctxDeleter);
if (!ctx)
{
throw CryptoException("Failed to create context for encryption");
}
if (EVP_PKEY_encrypt_init(ctx.get()) <= 0)
{
throw CryptoException("Failed to initialize encryption operation");
}
if (EVP_PKEY_CTX_set_rsa_padding(ctx.get(), RSA_PKCS1_OAEP_PADDING) <= 0)
{
throw CryptoException("Failed to set RSA padding");
}
size_t outlen;
if (EVP_PKEY_encrypt(ctx.get(), nullptr, &outlen, reinterpret_cast<const unsigned char*>(plaintext.data()), plaintext.size()) <= 0)
{
throw CryptoException("Failed to get output length for encryption");
}
std::vector<unsigned char> ciphertext(outlen);
if (EVP_PKEY_encrypt(ctx.get(), ciphertext.data(), &outlen, reinterpret_cast<const unsigned char*>(plaintext.data()), plaintext.size()) <= 0)
{
throw TextTooLongForPublicKeyException("The text to be encrypted is too long for the public key used");
}
return encode(std::string(ciphertext.begin(), ciphertext.end()));
}
/**
* @brief Decrypts the given ciphertext with the given private key using RSA decryption
*
* @param ciphertext Ciphertext to decrypt
* @param privateKeyStr Private key to use for decryption
* @param passphrase Passphrase for the private key (optional)
*
* @return Decrypted plaintext
*/
std::string decryptWithRSA(const std::string& ciphertext, const std::string& privateKeyStr, const std::string& passphrase = "")
{
auto encryptedText = decode(ciphertext);
auto bioDeleter = [](BIO* bio) { BIO_free(bio); };
std::unique_ptr<BIO, decltype(bioDeleter)> bio(BIO_new_mem_buf(privateKeyStr.data(), -1), bioDeleter);
auto pkeyDeleter = [](EVP_PKEY* pkey) { EVP_PKEY_free(pkey); };
std::unique_ptr<EVP_PKEY, decltype(pkeyDeleter)> privateKey(PEM_read_bio_PrivateKey(bio.get(), nullptr, passwordCallback, passphrase.empty() ? nullptr : const_cast<std::string*>(&passphrase)), pkeyDeleter);
if (!privateKey)
{
throw InvalidPrivateKeyException("RSA private key is invalid or passphrase is incorrect");
}
auto ctxDeleter = [](EVP_PKEY_CTX* ctx) { EVP_PKEY_CTX_free(ctx); };
std::unique_ptr<EVP_PKEY_CTX, decltype(ctxDeleter)> ctx(EVP_PKEY_CTX_new(privateKey.get(), nullptr), ctxDeleter);
if (!ctx)
{
throw CryptoException("Failed to create context for decryption");
}
if (EVP_PKEY_decrypt_init(ctx.get()) <= 0)
{
throw CryptoException("Failed to initialize decryption operation");
}
if (EVP_PKEY_CTX_set_rsa_padding(ctx.get(), RSA_PKCS1_OAEP_PADDING) <= 0)
{
throw CryptoException("Failed to set RSA padding");
}
size_t outlen;
if (EVP_PKEY_decrypt(ctx.get(), nullptr, &outlen, reinterpret_cast<const unsigned char*>(encryptedText.data()), encryptedText.size()) <= 0)
{
throw CryptoException("Failed to get output length for decryption");
}
std::vector<unsigned char> plaintext(outlen);
if (EVP_PKEY_decrypt(ctx.get(), plaintext.data(), &outlen, reinterpret_cast<const unsigned char*>(encryptedText.data()), encryptedText.size()) <= 0)
{
throw CorruptedTextException("Encrypted text is corrupted");
}
return std::string{plaintext.begin(), plaintext.begin() + outlen};
}
/**
* @brief Hashes the given string with SHA-256
*
* @param text String to hash
*
* @return Hashed string
*/
std::string hash(const std::string& text)
{
unsigned char hash[EVP_MAX_MD_SIZE];
unsigned int lengthOfHash = 0;
std::unique_ptr<EVP_MD_CTX, EVP_MD_CTX_Deleter> context(EVP_MD_CTX_new());
if (context)
{
if (EVP_DigestInit_ex(context.get(), EVP_sha256(), nullptr))
{
if (EVP_DigestUpdate(context.get(), text.c_str(), text.size()))
{
if (EVP_DigestFinal_ex(context.get(), hash, &lengthOfHash))
{
std::stringstream ss;
for (unsigned int i = 0; i < lengthOfHash; ++i)
{
ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(hash[i]);
}
return ss.str();
}
}
}
}
return "";
}
/**
* @brief Hashes the given string with HMAC SHA-256
*
* @param plainText String to hash
* @param key key to use for hashing plainText
*
* @return Hashed string
*/
inline std::string hashWithHMACSHA256(const std::string& plainText, const std::string& key)
{
const auto keyBytes = reinterpret_cast<const unsigned char*>(key.c_str());
const size_t keyLength = key.length();
const auto data = reinterpret_cast<const unsigned char*>(plainText.c_str());
const size_t dataLength = plainText.length();
unsigned char result[SHA256_DIGEST_LENGTH];
unsigned int resultLength;
if (HMAC(EVP_sha256(), keyBytes, static_cast<int>(keyLength), data, static_cast<int>(dataLength), result, &resultLength) == nullptr)
{
std::cerr << "Error: HMAC calculation failed." << std::endl;
return "";
}
const std::vector hashBytes(result, result + resultLength);
std::stringstream ss;
for (const unsigned char byte : hashBytes)
{
ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(byte);
}
return ss.str();
}
}
}
#endif //LIBCPP_CRYPTO_HPP