Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
DerivedKey.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4
5namespace Stroika::Foundation::Cryptography::Providers::OpenSSL {
6
7#if qStroika_HasComponent_OpenSSL
8 /*
9 ********************************************************************************
10 ********************** Cryptography::Providers::OpenSSL::DerivedKey ***********************
11 ********************************************************************************
12 */
13 inline DerivedKey::DerivedKey (const BLOB& key, const BLOB& iv)
14 : fKey{key}
15 , fIV{iv}
16 {
17 }
18 inline DerivedKey::DerivedKey (const pair<BLOB, BLOB>& keyAndIV)
19 : fKey{keyAndIV.first}
20 , fIV{keyAndIV.second}
21 {
22 }
23 inline BLOB DerivedKey::NormalizePassword (const BLOB& passwd)
24 {
25 return passwd;
26 }
27 inline BLOB DerivedKey::NormalizePassword (const String& passwd)
28 {
29 string ascii = passwd.AsASCII ();
30 return BLOB{reinterpret_cast<const byte*> (ascii.c_str ()), reinterpret_cast<const byte*> (ascii.c_str () + ascii.length ())};
31 }
32
33 /*
34 ********************************************************************************
35 ********************* Cryptography::Providers::OpenSSL::EVP_BytesToKey ********************
36 ********************************************************************************
37 */
38 template <typename PASSWORD_TYPE>
39 inline EVP_BytesToKey::EVP_BytesToKey (CipherAlgorithm cipherAlgorithm, DigestAlgorithm digestAlgorithm, const PASSWORD_TYPE& passwd,
40 unsigned int nRounds, const optional<BLOB>& salt)
41 : EVP_BytesToKey{cipherAlgorithm, digestAlgorithm, NormalizePassword (passwd), nRounds, salt}
42 {
43 }
44
45 /*
46 ********************************************************************************
47 ******************** Cryptography::Providers::OpenSSL::PKCS5_PBKDF2_HMAC ******************
48 ********************************************************************************
49 */
50 template <typename PASSWORD_TYPE>
51 inline PKCS5_PBKDF2_HMAC::PKCS5_PBKDF2_HMAC (size_t keyLen, size_t ivLen, DigestAlgorithm digestAlgorithm, const PASSWORD_TYPE& passwd,
52 unsigned int nRounds, const optional<BLOB>& salt)
53 : PKCS5_PBKDF2_HMAC{keyLen, ivLen, digestAlgorithm, NormalizePassword (passwd), nRounds, salt}
54 {
55 }
56 template <typename PASSWORD_TYPE, typename CIPHER_ALGORITHM_TYPE>
57 inline PKCS5_PBKDF2_HMAC::PKCS5_PBKDF2_HMAC (CIPHER_ALGORITHM_TYPE cipherAlgorithm, DigestAlgorithm digestAlgorithm,
58 const PASSWORD_TYPE& passwd, unsigned int nRounds, const optional<BLOB>& salt)
59 : PKCS5_PBKDF2_HMAC{KeyLength (cipherAlgorithm), IVLength (cipherAlgorithm), digestAlgorithm, passwd, nRounds, salt}
60 {
61 }
62
63 /*
64 ********************************************************************************
65 *************** Cryptography::Providers::OpenSSL::PKCS5_PBKDF2_HMAC_SHA1 ******************
66 ********************************************************************************
67 */
68 template <typename PASSWORD_TYPE>
69 inline PKCS5_PBKDF2_HMAC_SHA1::PKCS5_PBKDF2_HMAC_SHA1 (size_t keyLen, size_t ivLen, const PASSWORD_TYPE& passwd, unsigned int nRounds,
70 const optional<BLOB>& salt)
71 : PKCS5_PBKDF2_HMAC{keyLen, ivLen, DigestAlgorithms::kSHA1, passwd, nRounds, salt}
72 {
73 }
74 template <typename PASSWORD_TYPE, typename CIPHER_ALGORITHM_TYPE>
75 inline PKCS5_PBKDF2_HMAC_SHA1::PKCS5_PBKDF2_HMAC_SHA1 (CIPHER_ALGORITHM_TYPE cipherAlgorithm, const PASSWORD_TYPE& passwd,
76 unsigned int nRounds, const optional<BLOB>& salt)
77 : PKCS5_PBKDF2_HMAC{cipherAlgorithm, DigestAlgorithms::kSHA1, passwd, nRounds, salt}
78 {
79 }
80#endif
81
82}