randomtest.cpp

The code below shows the normal way to use the QCA::Random class.

00001 /*
00002  Copyright (C) 2004, 2006 Brad Hards <bradh@frogmouth.net>
00003 
00004  Permission is hereby granted, free of charge, to any person obtaining a copy
00005  of this software and associated documentation files (the "Software"), to deal
00006  in the Software without restriction, including without limitation the rights
00007  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  copies of the Software, and to permit persons to whom the Software is
00009  furnished to do so, subject to the following conditions:
00010 
00011  The above copyright notice and this permission notice shall be included in
00012  all copies or substantial portions of the Software.
00013 
00014  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
00017  AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00018  AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00019  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00020 */
00021 
00022 // QtCrypto has the declarations for all of QCA
00023 #include <QtCrypto>
00024 
00025 #include <QCoreApplication>
00026 #include <QDebug>
00027 
00028 #include <iostream>
00029 
00030 int main(int argc, char **argv)
00031 {
00032         QCoreApplication app(argc, argv);
00033 
00034         qDebug() << "This example generates random numbers";
00035 
00036         // the Initializer object sets things up, and
00037         // also does cleanup when it goes out of scope
00038         QCA::Initializer init;
00039 
00040         int randInt;
00041         // This is the standard way to generate a random integer.
00042         randInt = QCA::Random::randomInt();
00043         qDebug() << "A random number: " << randInt;
00044 
00045         // If you wanted a random character (octet), you could
00046         // use something like:
00047         unsigned char randChar;
00048         randChar = QCA::Random::randomChar();
00049         // It might not be printable, so this may not produce output
00050         std::cout << "A random character: " << randChar << std::endl;
00051 
00052         QCA::SecureArray tenBytes(10);
00053         // If you need more random values, you may want to
00054         // get an array, as shown below.
00055         tenBytes = QCA::Random::randomArray(10);
00056 
00057         // To make this viewable, we convert to hexadecimal.
00058         std::cout << "A random 10 byte array (in hex): ";
00059         std::cout << QCA::Hex().arrayToString(tenBytes).toAscii().data() << std::endl;
00060 
00061         // Under some circumstances, you may want to create a
00062         // Random object, rather than a static public member function.
00063         // This isn't normally the easiest way, but it does work
00064         QCA::Random myRandomObject;
00065         randChar = myRandomObject.nextByte();
00066         tenBytes = myRandomObject.nextBytes(10);
00067         return 0;
00068 }
00069 

Generated on Fri Jul 6 12:14:04 2007 for Qt Cryptographic Architecture by  doxygen 1.4.6