Re: [Boost-bugs] [Boost C++ Libraries] #7128: boost::uuids::detail::sha1 computes incorrect result

Subject: Re: [Boost-bugs] [Boost C++ Libraries] #7128: boost::uuids::detail::sha1 computes incorrect result
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2012-09-12 07:23:23


#7128: boost::uuids::detail::sha1 computes incorrect result
-------------------------------+--------------------------------------------
  Reporter: anonymous | Owner: atompkins
      Type: Bugs | Status: new
 Milestone: To Be Determined | Component: uuid
   Version: Boost 1.50.0 | Severity: Problem
Resolution: | Keywords: sha1
-------------------------------+--------------------------------------------

Comment (by jack.wgm@…):

 {{{
 #include <iostream>
 #include <string>
 #include <boost/shared_array.hpp>
 #include <boost/uuid/sha1.hpp>
 #include <boost/smart_ptr/scoped_ptr.hpp>
 #include <boost/iostreams/device/file.hpp>

 std::string sha1_to_string(const char *hash)
 {
         char str[128] = { 0 };
         char *ptr = str;
         std::string ret;

         for (int i = 0; i < 20; i++)
         {
                 sprintf(ptr, "%02X", (unsigned char)*hash);
                 ptr += 2;
                 hash++;
         }
         ret = str;

         return ret;
 }

 int main(int argc, char **argv)
 {
         if (argc != 2)
         {
                 return -1;
         }

         boost::uuids::detail::sha1 hasher;
         boost::shared_array<unsigned int> digest;
         boost::shared_array<char> temp_buf(new char[5242880]);

         boost::scoped_ptr<boost::iostreams::file> file_ptr(
                 new boost::iostreams::file(argv[1],
 BOOST_IOS::out|BOOST_IOS::in|BOOST_IOS::binary));
         if (!file_ptr->is_open())
                 return -1;

         while (true)
         {
                 std::streamsize readbytes = file_ptr->read(temp_buf.get(),
 5242880);
                 if (readbytes <= 0)
                         break;
                 hasher.process_bytes(temp_buf.get(), readbytes);
         }
         file_ptr.reset();

         digest.reset(new unsigned int [5]);
         char bin[20];

 hasher.get_digest(reinterpret_cast<boost::uuids::detail::sha1::digest_type>(*digest.get()));
         for(int i = 0; i < 5; ++i)
         {
                 const char* tmp = reinterpret_cast<char*>(digest.get());
                 bin[i * 4 ] = tmp[i * 4 + 3];
                 bin[i * 4 + 1] = tmp[i * 4 + 2];
                 bin[i * 4 + 2] = tmp[i * 4 + 1];
                 bin[i * 4 + 3] = tmp[i * 4 ];
         }

         std::string hash_hex = sha1_to_string(bin);

         // output hex digest
         std::cout << hash_hex.c_str() << std::endl;

         return 0;
 }
 }}}

 run program:


 {{{
 [root_at_media-coder test]# g++ test.cpp
 [root_at_coder test]# ./a.out dump.rmvb
 3A383A31E5B447A0614361D7E6931FE29BA9519E
 [root_at_coder test]# sha1sum dump.rmvb
 ad74ded3e3cbea7f02997d51e79e887e596ed787 dump.rmvb

 }}}

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/7128#comment:2>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:10 UTC