• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
10 //[password
11 /*`
12     For the source of this example see
13     [@boost://libs/random/example/password.cpp password.cpp].
14 
15     This example demonstrates generating a random 8 character
16     password.
17  */
18 
19 
20 #include <boost/random/random_device.hpp>
21 #include <boost/random/uniform_int_distribution.hpp>
22 #include <iostream>
23 
main()24 int main() {
25     /*<< We first define the characters that we're going
26          to allow.  This is pretty much just the characters
27          on a standard keyboard.
28     >>*/
29     std::string chars(
30         "abcdefghijklmnopqrstuvwxyz"
31         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
32         "1234567890"
33         "!@#$%^&*()"
34         "`~-_=+[{]}\\|;:'\",<.>/? ");
35     /*<< We use __random_device as a source of entropy, since we want
36          passwords that are not predictable.
37     >>*/
38     boost::random::random_device rng;
39     /*<< Finally we select 8 random characters from the
40          string and print them to cout.
41     >>*/
42     boost::random::uniform_int_distribution<> index_dist(0, chars.size() - 1);
43     for(int i = 0; i < 8; ++i) {
44         std::cout << chars[index_dist(rng)];
45     }
46     std::cout << std::endl;
47 }
48 
49 //]
50