sha1.hpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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/uuid/sha1.hpp>
  15. namespace boost {
  16. namespace compute {
  17. namespace detail {
  18. // Accumulates SHA1 hash of the passed strings.
  19. class sha1 {
  20. public:
  21. sha1(const std::string &s = "") {
  22. if (!s.empty()) this->process(s);
  23. }
  24. sha1& process(const std::string &s) {
  25. h.process_bytes(s.c_str(), s.size());
  26. return *this;
  27. }
  28. operator std::string() {
  29. unsigned int digest[5];
  30. h.get_digest(digest);
  31. std::ostringstream buf;
  32. for(int i = 0; i < 5; ++i)
  33. buf << std::hex << std::setfill('0') << std::setw(8) << digest[i];
  34. return buf.str();
  35. }
  36. private:
  37. boost::uuids::detail::sha1 h;
  38. };
  39. } // end detail namespace
  40. } // end compute namespace
  41. } // end boost namespace
  42. #endif // BOOST_COMPUTE_DETAIL_SHA1_HPP