@@ -71,55 +71,35 @@ static ByteList byteArrayToByteList(const Byte* inArray, size_t inSize) {
71
71
return result;
72
72
}
73
73
74
-
75
-
76
- static ByteList createSHA256 (const ByteList& inKey) {
74
+ static ByteList createSHA (const ByteList& inKey, const EVP_MD* inMd) {
77
75
ByteList result;
78
76
79
- Byte* buffer = byteListToNewByteArray (inKey) ;
80
- Byte bufferResult[SHA256_DIGEST_LENGTH] ;
77
+ Byte bufferResult[EVP_MAX_MD_SIZE] ;
78
+ unsigned int hash_len = 0 ;
81
79
80
+ Byte* buffer = byteListToNewByteArray (inKey);
82
81
83
- SHA256_CTX ctx;
84
- SHA256_Init (&ctx);
85
- SHA256_Update (&ctx, buffer, inKey.size ());
86
- SHA256_Final (bufferResult, &ctx);
82
+ EVP_MD_CTX *ctx = EVP_MD_CTX_new ();
83
+ EVP_DigestInit_ex (ctx, inMd, NULL );
84
+ EVP_DigestUpdate (ctx, buffer, inKey.size ()); // data: pointer to your data, data_len: length
85
+ EVP_DigestFinal_ex (ctx, bufferResult, &hash_len);
86
+ EVP_MD_CTX_free (ctx);
87
87
88
88
delete[] buffer;
89
- return byteArrayToByteList (bufferResult, SHA256_DIGEST_LENGTH );
89
+ return byteArrayToByteList (bufferResult, hash_len );
90
90
}
91
91
92
+ static ByteList createSHA256 (const ByteList& inKey) {
93
+ return createSHA (inKey, EVP_sha256 ());
94
+ }
92
95
93
- static ByteList createSHA384 (const ByteList& inKey) {
94
- ByteList result;
95
-
96
- Byte* buffer = byteListToNewByteArray (inKey);
97
- Byte bufferResult[SHA384_DIGEST_LENGTH];
98
-
99
- SHA512_CTX ctx;
100
- SHA384_Init (&ctx);
101
- SHA384_Update (&ctx, buffer, inKey.size ());
102
- SHA384_Final (bufferResult, &ctx);
103
96
104
- delete[] buffer;
105
- return byteArrayToByteList (bufferResult, SHA384_DIGEST_LENGTH );
97
+ static ByteList createSHA384 ( const ByteList& inKey) {
98
+ return createSHA (inKey, EVP_sha384 () );
106
99
}
107
100
108
101
static ByteList createSHA512 (const ByteList& inKey) {
109
- ByteList result;
110
-
111
- Byte* buffer = byteListToNewByteArray (inKey);
112
- Byte bufferResult[SHA512_DIGEST_LENGTH];
113
-
114
- SHA512_CTX ctx;
115
- SHA512_Init (&ctx);
116
- SHA512_Update (&ctx, buffer, inKey.size ());
117
- SHA512_Final (bufferResult, &ctx);
118
-
119
- delete[] buffer;
120
- return byteArrayToByteList (bufferResult, SHA512_DIGEST_LENGTH);
121
-
122
- return result;
102
+ return createSHA (inKey, EVP_sha512 ());
123
103
}
124
104
125
105
static ByteList encryptKeyCBC (const ByteList& inKey, const ByteList& inData, Byte* inIV) {
0 commit comments