Mercurial > hg > orthanc
comparison Core/Toolbox.cpp @ 22:1bc6327d1de3
MD5 sum
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 27 Aug 2012 11:45:53 +0200 |
parents | 27e2bbc11200 |
children | 62bd05fe4b7c |
comparison
equal
deleted
inserted
replaced
21:29bfa3095207 | 22:1bc6327d1de3 |
---|---|
34 #if defined(__linux) | 34 #if defined(__linux) |
35 #include <signal.h> | 35 #include <signal.h> |
36 #include <unistd.h> | 36 #include <unistd.h> |
37 #endif | 37 #endif |
38 | 38 |
39 #include "../Resources/md5/md5.h" | |
40 | |
39 | 41 |
40 namespace Palantir | 42 namespace Palantir |
41 { | 43 { |
42 static bool finish; | 44 static bool finish; |
43 | 45 |
300 catch (boost::filesystem::filesystem_error) | 302 catch (boost::filesystem::filesystem_error) |
301 { | 303 { |
302 throw PalantirException(ErrorCode_InexistentFile); | 304 throw PalantirException(ErrorCode_InexistentFile); |
303 } | 305 } |
304 } | 306 } |
307 | |
308 | |
309 | |
310 static char GetHexadecimalCharacter(uint8_t value) | |
311 { | |
312 assert(value < 16); | |
313 | |
314 if (value < 10) | |
315 return value + '0'; | |
316 else | |
317 return (value - 10) + 'a'; | |
318 } | |
319 | |
320 void Toolbox::ComputeMD5(std::string& result, | |
321 const std::string& data) | |
322 { | |
323 md5_state_s state; | |
324 md5_init(&state); | |
325 | |
326 if (data.size() > 0) | |
327 { | |
328 md5_append(&state, reinterpret_cast<const md5_byte_t*>(&data[0]), | |
329 static_cast<int>(data.size())); | |
330 } | |
331 | |
332 md5_byte_t actualHash[16]; | |
333 md5_finish(&state, actualHash); | |
334 | |
335 result.resize(32); | |
336 for (unsigned int i = 0; i < 16; i++) | |
337 { | |
338 result[2 * i] = GetHexadecimalCharacter(actualHash[i] / 16); | |
339 result[2 * i + 1] = GetHexadecimalCharacter(actualHash[i] % 16); | |
340 } | |
341 | |
342 } | |
305 } | 343 } |