File indexing completed on 2024-04-28 04:43:39

0001 /*
0002  Copyright (C) 2004, 2006 Brad Hards <bradh@frogmouth.net>
0003 
0004  Permission is hereby granted, free of charge, to any person obtaining a copy
0005  of this software and associated documentation files (the "Software"), to deal
0006  in the Software without restriction, including without limitation the rights
0007  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0008  copies of the Software, and to permit persons to whom the Software is
0009  furnished to do so, subject to the following conditions:
0010 
0011  The above copyright notice and this permission notice shall be included in
0012  all copies or substantial portions of the Software.
0013 
0014  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0015  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0016  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
0017  AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
0018  AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
0019  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0020 */
0021 
0022 // QtCrypto has the declarations for all of QCA
0023 #include <QtCrypto>
0024 
0025 #include <QCoreApplication>
0026 #include <QDebug>
0027 
0028 #include <iostream>
0029 
0030 #ifdef QT_STATICPLUGIN
0031 #include "import_plugins.h"
0032 #endif
0033 
0034 int main(int argc, char **argv)
0035 {
0036     // the Initializer object sets things up, and
0037     // also does cleanup when it goes out of scope
0038     QCA::Initializer init;
0039 
0040     QCoreApplication app(argc, argv);
0041 
0042     qDebug() << "This example generates random numbers";
0043 
0044     int randInt;
0045     // This is the standard way to generate a random integer.
0046     randInt = QCA::Random::randomInt();
0047     qDebug() << "A random number: " << randInt;
0048 
0049     // If you wanted a random character (octet), you could
0050     // use something like:
0051     unsigned char randChar;
0052     randChar = QCA::Random::randomChar();
0053     // It might not be printable, so this may not produce output
0054     std::cout << "A random character: " << randChar << std::endl;
0055 
0056     QCA::SecureArray tenBytes(10);
0057     // If you need more random values, you may want to
0058     // get an array, as shown below.
0059     tenBytes = QCA::Random::randomArray(10);
0060 
0061     // To make this viewable, we convert to hexadecimal.
0062     std::cout << "A random 10 byte array (in hex): ";
0063     std::cout << qPrintable(QCA::Hex().arrayToString(tenBytes)) << std::endl;
0064 
0065     // Under some circumstances, you may want to create a
0066     // Random object, rather than a static public member function.
0067     // This isn't normally the easiest way, but it does work
0068     QCA::Random myRandomObject;
0069     randChar = myRandomObject.nextByte();
0070     tenBytes = myRandomObject.nextBytes(10);
0071     return 0;
0072 }