password.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // password.cpp
  2. //
  3. // Copyright (c) 2010
  4. // Steven Watanabe
  5. //
  6. // Distributed under the Boost Software License, Version 1.0. (See
  7. // accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //[password
  10. /*`
  11. For the source of this example see
  12. [@boost://libs/random/example/password.cpp password.cpp].
  13. This example demonstrates generating a random 8 character
  14. password.
  15. */
  16. #include <boost/random/random_device.hpp>
  17. #include <boost/random/uniform_int_distribution.hpp>
  18. #include <iostream>
  19. int main() {
  20. /*<< We first define the characters that we're going
  21. to allow. This is pretty much just the characters
  22. on a standard keyboard.
  23. >>*/
  24. std::string chars(
  25. "abcdefghijklmnopqrstuvwxyz"
  26. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  27. "1234567890"
  28. "!@#$%^&*()"
  29. "`~-_=+[{]}\\|;:'\",<.>/? ");
  30. /*<< We use __random_device as a source of entropy, since we want
  31. passwords that are not predictable.
  32. >>*/
  33. boost::random::random_device rng;
  34. /*<< Finally we select 8 random characters from the
  35. string and print them to cout.
  36. >>*/
  37. boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);
  38. for(int i = 0; i < 8; ++i) {
  39. std::cout << chars[index_dist(rng)];
  40. }
  41. std::cout << std::endl;
  42. }
  43. //]