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
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <comphelper/hash.hxx>
#include <config_oox.h>
#if USE_TLS_NSS
#include <nss.h>
#include <pk11pub.h>
#include <sechash.h>
#elif USE_TLS_OPENSSL
#include <openssl/evp.h>
#include <openssl/sha.h>
#endif // USE_TLS_OPENSSL
namespace comphelper {
struct HashImpl
{
#if USE_TLS_NSS
HASHContext* mpContext;
HASH_HashType getNSSType() const
{
switch (meType)
{
case HashType::MD5:
return HASH_AlgMD5;
case HashType::SHA1:
return HASH_AlgSHA1;
case HashType::SHA256:
return HASH_AlgSHA256;
case HashType::SHA512:
return HASH_AlgSHA512;
}
return HASH_AlgNULL;
}
#elif USE_TLS_OPENSSL
EVP_MD_CTX* mpContext;
const EVP_MD* getOpenSSLType() const
{
switch (meType)
{
case HashType::MD5:
return EVP_md5();
case HashType::SHA1:
return EVP_sha1();
case HashType::SHA256:
return EVP_sha256();
case HashType::SHA512:
return EVP_sha512();
}
return nullptr;
}
#endif
HashType meType;
HashImpl(HashType eType):
meType(eType)
{
#if USE_TLS_NSS
NSS_NoDB_Init(nullptr);
mpContext = HASH_Create(getNSSType());
HASH_Begin(mpContext);
#elif USE_TLS_OPENSSL
mpContext = EVP_MD_CTX_create();
EVP_DigestInit_ex(mpContext, getOpenSSLType(), NULL);
#endif
}
~HashImpl()
{
#if USE_TLS_NSS
HASH_Destroy(mpContext);
#elif USE_TLS_OPENSSL
EVP_MD_CTX_destroy(mpContext);
#endif
}
};
Hash::Hash(HashType eType):
mpImpl(new HashImpl(eType))
{
}
Hash::~Hash()
{
}
void Hash::update(const unsigned char* pInput, size_t length)
{
#if USE_TLS_NSS
HASH_Update(mpImpl->mpContext, pInput, length);
#elif USE_TLS_OPENSSL
EVP_DigestUpdate(mpImpl->mpContext, pInput, length);
#else
(void)pInput;
(void)length;
#endif
}
std::vector<unsigned char> Hash::finalize()
{
std::vector<unsigned char> hash(getLength(), 0);
unsigned int digestWrittenLength;
#if USE_TLS_NSS
HASH_End(mpImpl->mpContext, hash.data(), &digestWrittenLength, getLength());
#elif USE_TLS_OPENSSL
EVP_DigestFinal_ex(mpImpl->mpContext, hash.data(), &digestWrittenLength);
#else
(void)digestWrittenLength;
#endif
return hash;
}
size_t Hash::getLength() const
{
switch (mpImpl->meType)
{
case HashType::MD5:
return 16;
case HashType::SHA1:
return 20;
case HashType::SHA256:
return 32;
case HashType::SHA512:
return 64;
}
return 0;
}
std::vector<unsigned char> Hash::calculateHash(const unsigned char* pInput, size_t length, HashType eType)
{
Hash aHash(eType);
aHash.update(pInput, length);
return aHash.finalize();
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|