|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r62629 - in trunk/libs/random: doc example
From: steven_at_[hidden]
Date: 2010-06-08 22:33:42
Author: steven_watanabe
Date: 2010-06-08 22:33:40 EDT (Tue, 08 Jun 2010)
New Revision: 62629
URL: http://svn.boost.org/trac/boost/changeset/62629
Log:
Add an example of generating a random password. Fixes #4183
Added:
trunk/libs/random/example/password.cpp (contents, props changed)
Text files modified:
trunk/libs/random/doc/tutorial.qbk | 7 +++++++
trunk/libs/random/example/Jamfile.v2 | 1 +
2 files changed, 8 insertions(+), 0 deletions(-)
Modified: trunk/libs/random/doc/tutorial.qbk
==============================================================================
--- trunk/libs/random/doc/tutorial.qbk (original)
+++ trunk/libs/random/doc/tutorial.qbk 2010-06-08 22:33:40 EDT (Tue, 08 Jun 2010)
@@ -19,3 +19,10 @@
[weighted_die]
[endsect]
+
+[section Generating a random password]
+
+[import ../example/password.cpp]
+[password]
+
+[endsect]
Modified: trunk/libs/random/example/Jamfile.v2
==============================================================================
--- trunk/libs/random/example/Jamfile.v2 (original)
+++ trunk/libs/random/example/Jamfile.v2 2010-06-08 22:33:40 EDT (Tue, 08 Jun 2010)
@@ -9,3 +9,4 @@
run die.cpp ;
run weighted_die.cpp ;
+run password.cpp /boost//random ;
Added: trunk/libs/random/example/password.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/random/example/password.cpp 2010-06-08 22:33:40 EDT (Tue, 08 Jun 2010)
@@ -0,0 +1,50 @@
+// password.cpp
+//
+// Copyright (c) 2010
+// Steven Watanabe
+//
+// Distributed under the Boost Software License, Version 1.0. (See
+// accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+
+//[password
+/*`
+ For the source of this example see
+ [@boost://libs/random/example/password.cpp password.cpp].
+
+ This example demonstrates generating a random 8 character
+ password.
+ */
+
+
+#include <boost/nondet_random.hpp>
+#include <boost/random/variate_generator.hpp>
+#include <boost/random/uniform_int.hpp>
+
+int main() {
+ /*<< We first define the characters that we're going
+ to allow. This is pretty much just the characters
+ on a standard keyboard.
+ >>*/
+ std::string chars(
+ "abcdefghijklmnopqrstuvwxyz"
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "1234567890"
+ "!@#$%^&*()"
+ "`~-_=+[{]{\\|;:'\",<.>/? ");
+ /*<< We use __random_device as a source of entropy, since we want
+ passwords that are not predictable.
+ >>*/
+ boost::random_device rng;
+ /*<< Finally we select 8 random characters from the
+ string and print them to cout.
+ >>*/
+ boost::variate_generator<boost::random_device&, boost::uniform_int<> >
+ gen(rng, boost::uniform_int<>(0, chars.size()));
+ for(int i = 0; i < 8; ++i) {
+ std::cout << chars[gen()];
+ }
+ std::cout << std::endl;
+}
+
+//]
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk