1/*
2 Copyright (C) 2004, 2006 Brad Hards <bradh@frogmouth.net>
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20*/
21
22// QtCrypto has the declarations for all of QCA
23#include <QtCrypto>
24
25#include <QCoreApplication>
26#include <QDebug>
27
28#include <iostream>
29
30#ifdef QT_STATICPLUGIN
31#include "import_plugins.h"
32#endif
33
34int main(int argc, char **argv)
35{
36 // the Initializer object sets things up, and
37 // also does cleanup when it goes out of scope
38 QCA::Initializer init;
39
40 QCoreApplication app(argc, argv);
41
42 qDebug() << "This example generates random numbers";
43
44 int randInt;
45 // This is the standard way to generate a random integer.
46 randInt = QCA::Random::randomInt();
47 qDebug() << "A random number: " << randInt;
48
49 // If you wanted a random character (octet), you could
50 // use something like:
51 unsigned char randChar;
52 randChar = QCA::Random::randomChar();
53 // It might not be printable, so this may not produce output
54 std::cout << "A random character: " << randChar << std::endl;
55
56 QCA::SecureArray tenBytes(10);
57 // If you need more random values, you may want to
58 // get an array, as shown below.
59 tenBytes = QCA::Random::randomArray(size: 10);
60
61 // To make this viewable, we convert to hexadecimal.
62 std::cout << "A random 10 byte array (in hex): ";
63 std::cout << qPrintable(QCA::Hex().arrayToString(tenBytes)) << std::endl;
64
65 // Under some circumstances, you may want to create a
66 // Random object, rather than a static public member function.
67 // This isn't normally the easiest way, but it does work
68 QCA::Random myRandomObject;
69 randChar = myRandomObject.nextByte();
70 tenBytes = myRandomObject.nextBytes(size: 10);
71 return 0;
72}
73

source code of qca/examples/randomtest/randomtest.cpp