sha1.hpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@gmail.com>
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. // See http://boostorg.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #ifndef BOOST_COMPUTE_DETAIL_SHA1_HPP
  11. #define BOOST_COMPUTE_DETAIL_SHA1_HPP
  12. #include <sstream>
  13. #include <iomanip>
  14. #include <boost/version.hpp>
  15. #if BOOST_VERSION >= 106600
  16. # include <boost/uuid/detail/sha1.hpp>
  17. #else
  18. # include <boost/uuid/sha1.hpp>
  19. #endif
  20. namespace boost {
  21. namespace compute {
  22. namespace detail {
  23. // Accumulates SHA1 hash of the passed strings.
  24. class sha1 {
  25. public:
  26. sha1(const std::string &s = "") {
  27. if (!s.empty()) this->process(s);
  28. }
  29. sha1& process(const std::string &s) {
  30. h.process_bytes(s.c_str(), s.size());
  31. return *this;
  32. }
  33. operator std::string() {
  34. unsigned int digest[5];
  35. h.get_digest(digest);
  36. std::ostringstream buf;
  37. for(int i = 0; i < 5; ++i)
  38. buf << std::hex << std::setfill('0') << std::setw(8) << digest[i];
  39. return buf.str();
  40. }
  41. private:
  42. boost::uuids::detail::sha1 h;
  43. };
  44. } // end detail namespace
  45. } // end compute namespace
  46. } // end boost namespace
  47. #endif // BOOST_COMPUTE_DETAIL_SHA1_HPP