Skip to content

Commit 358d15e

Browse files
committed
Merge branch 'galk.encryption_2.0' of github.com:galkahana/PDF-Writer into galk.encryption_2.0
2 parents a2856e4 + 655bb7a commit 358d15e

File tree

1 file changed

+16
-36
lines changed

1 file changed

+16
-36
lines changed

PDFWriter/XCryptionCommon2_0.cpp

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -71,55 +71,35 @@ static ByteList byteArrayToByteList(const Byte* inArray, size_t inSize) {
7171
return result;
7272
}
7373

74-
75-
76-
static ByteList createSHA256(const ByteList& inKey) {
74+
static ByteList createSHA(const ByteList& inKey, const EVP_MD* inMd) {
7775
ByteList result;
7876

79-
Byte* buffer = byteListToNewByteArray(inKey);
80-
Byte bufferResult[SHA256_DIGEST_LENGTH];
77+
Byte bufferResult[EVP_MAX_MD_SIZE];
78+
unsigned int hash_len = 0;
8179

80+
Byte* buffer = byteListToNewByteArray(inKey);
8281

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);
8787

8888
delete[] buffer;
89-
return byteArrayToByteList(bufferResult, SHA256_DIGEST_LENGTH);
89+
return byteArrayToByteList(bufferResult, hash_len);
9090
}
9191

92+
static ByteList createSHA256(const ByteList& inKey) {
93+
return createSHA(inKey, EVP_sha256());
94+
}
9295

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);
10396

104-
delete[] buffer;
105-
return byteArrayToByteList(bufferResult, SHA384_DIGEST_LENGTH);
97+
static ByteList createSHA384(const ByteList& inKey) {
98+
return createSHA(inKey, EVP_sha384());
10699
}
107100

108101
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());
123103
}
124104

125105
static ByteList encryptKeyCBC(const ByteList& inKey, const ByteList& inData, Byte* inIV) {

0 commit comments

Comments
 (0)