QCA

randomtest.cpp
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
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(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(10);
71 return 0;
72}
Hexadecimal encoding / decoding.
Convenience method for initialising and cleaning up QCA.
Definition qca_core.h:660
Source of random numbers.
Definition qca_basic.h:66
uchar nextByte()
Provide a random byte.
static SecureArray randomArray(int size)
Provide a specified number of random bytes.
static int randomInt()
Provide a random integer.
static uchar randomChar()
Provide a random character (byte)
SecureArray nextBytes(int size)
Provide a specified number of random bytes.
Secure array of bytes.
Definition qca_tools.h:317
void init(KXmlGuiWindow *window, KGameDifficulty *difficulty=nullptr)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:03:55 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.