1 /* Copyright (c) MediaArea.net SARL. All Rights Reserved.
2 *
3 * Use of this source code is governed by a BSD-style license that can
4 * be found in the License.html file in the root of the source tree.
5 */
6
7 //---------------------------------------------------------------------------
8 // Pre-compilation
9 #include "MediaInfo/PreComp.h"
10 #ifdef __BORLANDC__
11 #pragma hdrstop
12 #endif
13 //---------------------------------------------------------------------------
14
15 //---------------------------------------------------------------------------
16 #include "MediaInfo/Setup.h"
17 //---------------------------------------------------------------------------
18
19 //---------------------------------------------------------------------------
20 #include "MediaInfo/HashWrapper.h" //For getting MEDIAINFO_HASH, not in setup
21 //---------------------------------------------------------------------------
22
23 #if MEDIAINFO_HASH
24
25 //---------------------------------------------------------------------------
26 #include "ZenLib/Ztring.h"
27 using namespace ZenLib;
28 #if MEDIAINFO_MD5
29 extern "C"
30 {
31 #include <md5.h>
32 }
33 #endif //MEDIAINFO_MD5
34 #if MEDIAINFO_SHA1
35 extern "C"
36 {
37 #include <sha1.h>
38 }
39 #endif //MEDIAINFO_SHA1
40 #if MEDIAINFO_SHA2
41 extern "C"
42 {
43 #include <sha2.h>
44 }
45 #endif //MEDIAINFO_SHA2
46 //---------------------------------------------------------------------------
47
48 namespace MediaInfoLib
49 {
50
51 //***************************************************************************
52 // info
53 //***************************************************************************
54
55 static const char HashWrapper_Hex[16] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
56
57
58 //***************************************************************************
59 // Constructor/Destructor
60 //***************************************************************************
61
62 //---------------------------------------------------------------------------
Init(const HashFunctions & Functions)63 void HashWrapper::Init (const HashFunctions &Functions)
64 {
65 //Init to zero
66 if (sizeof(m))
67 {
68 memset(m, 0, sizeof(m));
69 }
70
71 #if MEDIAINFO_MD5
72 if (Functions[MD5])
73 {
74 m[MD5]=new struct MD5Context;
75 MD5Init((struct MD5Context*)m[MD5]);
76 }
77 #endif //MEDIAINFO_MD5
78
79 #if MEDIAINFO_SHA1
80 if (Functions[SHA1])
81 {
82 m[SHA1]=new sha1_ctx;
83 sha1_begin((sha1_ctx*)m[SHA1]);
84 }
85 #endif //MEDIAINFO_SHA1
86
87 #if MEDIAINFO_SHA2
88 if (Functions[SHA224])
89 {
90 m[SHA224]=new sha224_ctx;
91 sha224_begin((sha224_ctx*)m[SHA224]);
92 }
93 if (Functions[SHA256])
94 {
95 m[SHA256]=new sha256_ctx;
96 sha256_begin((sha256_ctx*)m[SHA256]);
97 }
98 if (Functions[SHA384])
99 {
100 m[SHA384]=new sha384_ctx;
101 sha384_begin((sha384_ctx*)m[SHA384]);
102 }
103 if (Functions[SHA512])
104 {
105 m[SHA512]=new sha512_ctx;
106 sha512_begin((sha512_ctx*)m[SHA512]);
107 }
108 #endif //MEDIAINFO_SHA2
109 }
110
~HashWrapper()111 HashWrapper::~HashWrapper ()
112 {
113 #if MEDIAINFO_MD5
114 delete (struct MD5Context*)m[MD5];
115 #endif //MEDIAINFO_MD5
116
117 #if MEDIAINFO_SHA1
118 delete (sha1_ctx*)m[SHA1];
119 #endif //MEDIAINFO_SHA1
120
121 #if MEDIAINFO_SHA2
122 delete (sha224_ctx*)m[SHA224];
123 delete (sha256_ctx*)m[SHA256];
124 delete (sha384_ctx*)m[SHA384];
125 delete (sha512_ctx*)m[SHA512];
126 #endif //MEDIAINFO_SHA2
127 }
128
Update(const int8u * Buffer,const size_t Buffer_Size)129 void HashWrapper::Update (const int8u* Buffer, const size_t Buffer_Size)
130 {
131 #if MEDIAINFO_MD5
132 if (m[MD5])
133 MD5Update((struct MD5Context*)m[MD5], Buffer, (unsigned int)Buffer_Size);
134 #endif //MEDIAINFO_MD5
135
136 #if MEDIAINFO_SHA1
137 if (m[SHA1])
138 sha1_hash(Buffer, (unsigned long)Buffer_Size, (sha1_ctx*)m[SHA1]);
139 #endif //MEDIAINFO_SHA1
140
141 #if MEDIAINFO_SHA2
142 if (m[SHA224])
143 sha224_hash(Buffer, (unsigned long)Buffer_Size, (sha224_ctx*)m[SHA224]);
144 if (m[SHA256])
145 sha256_hash(Buffer, (unsigned long)Buffer_Size, (sha256_ctx*)m[SHA256]);
146 if (m[SHA384])
147 sha384_hash(Buffer, (unsigned long)Buffer_Size, (sha384_ctx*)m[SHA384]);
148 if (m[SHA512])
149 sha512_hash(Buffer, (unsigned long)Buffer_Size, (sha512_ctx*)m[SHA512]);
150 #endif //MEDIAINFO_SHA2
151 }
152
Generate(const HashFunction Function)153 string HashWrapper::Generate (const HashFunction Function)
154 {
155 #if MEDIAINFO_MD5
156 if (Function==MD5 && m[MD5])
157 {
158 unsigned char Digest[16];
159 MD5Final(Digest, (struct MD5Context*)m[MD5]);
160 return Hex2String(Digest, 16);
161 }
162 #endif //MEDIAINFO_MD5
163
164 #if MEDIAINFO_SHA1
165 if (Function==SHA1 && m[SHA1])
166 {
167 unsigned char Digest[20];
168 sha1_end(Digest, (sha1_ctx*)m[SHA1]);
169 return Hex2String(Digest, 20);
170 }
171 #endif //MEDIAINFO_SHA1
172
173 #if MEDIAINFO_SHA2
174 if (Function==SHA224 && m[SHA224])
175 {
176 unsigned char Digest[28];
177 sha224_end(Digest, (sha224_ctx*)m[SHA224]);
178 return Hex2String(Digest, 28);
179 }
180 if (Function==SHA256 && m[SHA256])
181 {
182 unsigned char Digest[32];
183 sha256_end(Digest, (sha256_ctx*)m[SHA256]);
184 return Hex2String(Digest, 32);
185 }
186 if (Function==SHA384 && m[SHA384])
187 {
188 unsigned char Digest[48];
189 sha384_end(Digest, (sha384_ctx*)m[SHA384]);
190 return Hex2String(Digest, 48);
191 }
192 if (Function==SHA512 && m[SHA512])
193 {
194 unsigned char Digest[64];
195 sha512_end(Digest, (sha512_ctx*)m[SHA512]);
196 return Hex2String(Digest, 64);
197 }
198 #endif //MEDIAINFO_SHA2
199
200 return string();
201 }
202
Name(const HashFunction Function)203 string HashWrapper::Name (const HashFunction Function)
204 {
205 #if MEDIAINFO_MD5
206 if (Function==MD5)
207 return "MD5";
208 #endif //MEDIAINFO_MD5
209
210 #if MEDIAINFO_SHA1
211 if (Function==SHA1)
212 return "SHA-1";
213 #endif //MEDIAINFO_SHA1
214
215 #if MEDIAINFO_SHA2
216 if (Function==SHA224)
217 return "SHA-224";
218 if (Function==SHA256)
219 return "SHA-256";
220 if (Function==SHA384)
221 return "SHA-384";
222 if (Function==SHA512)
223 return "SHA-512";
224 #endif //MEDIAINFO_SHA2
225
226 return string();
227 }
228
Hex2String(const int8u * Digest,const size_t Digest_Size)229 string HashWrapper::Hex2String(const int8u* Digest, const size_t Digest_Size)
230 {
231 string DigestS;
232 DigestS.resize(Digest_Size*2);
233 for (size_t i=0, j=0; i<Digest_Size; ++i)
234 {
235 DigestS[j++]=HashWrapper_Hex[Digest[i] >> 4];
236 DigestS[j++]=HashWrapper_Hex[Digest[i] & 0xF];
237 }
238 return DigestS;
239 }
240
241 } //NameSpace
242
243 #endif //MEDIAINFO_HASH
244