comparison Core/Toolbox.cpp @ 1833:47d032c48818

"OrthancPluginComputeMd5()" and "OrthancPluginComputeSha1()" to compute MD5/SHA-1 hash
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 27 Nov 2015 12:22:44 +0100
parents 1065401501fb
children b1291df2f780
comparison
equal deleted inserted replaced
1832:b7da58699f92 1833:47d032c48818
492 } 492 }
493 493
494 494
495 void Toolbox::ComputeMD5(std::string& result, 495 void Toolbox::ComputeMD5(std::string& result,
496 const void* data, 496 const void* data,
497 size_t length) 497 size_t size)
498 { 498 {
499 md5_state_s state; 499 md5_state_s state;
500 md5_init(&state); 500 md5_init(&state);
501 501
502 if (length > 0) 502 if (size > 0)
503 { 503 {
504 md5_append(&state, 504 md5_append(&state,
505 reinterpret_cast<const md5_byte_t*>(data), 505 reinterpret_cast<const md5_byte_t*>(data),
506 static_cast<int>(length)); 506 static_cast<int>(size));
507 } 507 }
508 508
509 md5_byte_t actualHash[16]; 509 md5_byte_t actualHash[16];
510 md5_finish(&state, actualHash); 510 md5_finish(&state, actualHash);
511 511
757 } 757 }
758 758
759 return result; 759 return result;
760 } 760 }
761 761
762
762 void Toolbox::ComputeSHA1(std::string& result, 763 void Toolbox::ComputeSHA1(std::string& result,
763 const std::string& data) 764 const void* data,
765 size_t size)
764 { 766 {
765 boost::uuids::detail::sha1 sha1; 767 boost::uuids::detail::sha1 sha1;
766 768
767 if (data.size() > 0) 769 if (size > 0)
768 { 770 {
769 sha1.process_bytes(&data[0], data.size()); 771 sha1.process_bytes(data, size);
770 } 772 }
771 773
772 unsigned int digest[5]; 774 unsigned int digest[5];
773 775
774 // Sanity check for the memory layout: A SHA-1 digest is 160 bits wide 776 // Sanity check for the memory layout: A SHA-1 digest is 160 bits wide
782 digest[1], 784 digest[1],
783 digest[2], 785 digest[2],
784 digest[3], 786 digest[3],
785 digest[4]); 787 digest[4]);
786 } 788 }
789
790 void Toolbox::ComputeSHA1(std::string& result,
791 const std::string& data)
792 {
793 if (data.size() > 0)
794 {
795 ComputeSHA1(result, data.c_str(), data.size());
796 }
797 else
798 {
799 ComputeSHA1(result, NULL, 0);
800 }
801 }
802
787 803
788 bool Toolbox::IsSHA1(const char* str, 804 bool Toolbox::IsSHA1(const char* str,
789 size_t size) 805 size_t size)
790 { 806 {
791 if (size == 0) 807 if (size == 0)