encryption.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright (c) 2009-2016 Vladimir Batov.
  2. // Use, modification and distribution are subject to the Boost Software License,
  3. // Version 1.0. See http://www.boost.org/LICENSE_1_0.txt.
  4. #include "./test.hpp"
  5. #if defined(BOOST_CONVERT_IS_NOT_SUPPORTED)
  6. int main(int, char const* []) { return 0; }
  7. #else
  8. #include <boost/convert.hpp>
  9. #include <boost/detail/lightweight_test.hpp>
  10. static
  11. bool
  12. my_cypher(std::string const& value_in, boost::optional<std::string>& value_out)
  13. {
  14. size_t const cypher = 'A' - '1';
  15. value_out = value_in;
  16. for (std::string::iterator it = value_out->begin(); it != value_out->end(); ++it)
  17. (*it < 'A') ? (*it += cypher) : (*it -= cypher);
  18. return true;
  19. }
  20. int
  21. main(int, char const* [])
  22. {
  23. ////////////////////////////////////////////////////////////////////////////
  24. // Testing custom converter.
  25. ////////////////////////////////////////////////////////////////////////////
  26. std::string encrypted = boost::convert<std::string>("ABC", my_cypher).value();
  27. std::string decrypted = boost::convert<std::string>(encrypted, my_cypher).value();
  28. BOOST_TEST(encrypted == "123");
  29. BOOST_TEST(decrypted == "ABC");
  30. return boost::report_errors();
  31. }
  32. #endif