File indexing completed on 2024-11-10 07:20:23

0001 /*
0002  Copyright (C) 2005 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 
0027 #include <iostream>
0028 
0029 #ifdef QT_STATICPLUGIN
0030 #include "import_plugins.h"
0031 #endif
0032 
0033 int main(int argc, char **argv)
0034 {
0035     // the Initializer object sets things up, and
0036     // also does cleanup when it goes out of scope
0037     QCA::Initializer init;
0038 
0039     QCoreApplication app(argc, argv);
0040 
0041     // we use the first argument as the data to encode / decode
0042     // if an argument is provided. Use "hello" if no argument
0043     QByteArray arg; // empty array
0044     arg.append((argc >= 2) ? argv[1] : "hello");
0045 
0046     // create our object, which does encoding by default
0047     // QCA::Hex encoder(QCA::Encode); is equivalent
0048     QCA::Hex encoder;
0049 
0050     // You might prefer to use encoder.encode(); and have
0051     // it return a QCA::SecureArray, depending on your needs
0052     QString encoded = encoder.arrayToString(arg);
0053 
0054     std::cout << arg.data() << " in hex encoding is ";
0055     std::cout << encoded.toLatin1().data() << std::endl;
0056 
0057     // This time, we'll create an object to decode hexadecimal.
0058     // We could also have reused the existing object, calling
0059     // clear(); and setup(QCA::Decode); on it.
0060     QCA::Hex decoder(QCA::Decode);
0061 
0062     // This time, we convert a QString into a QString
0063     QString decoded = decoder.decodeString(encoded);
0064 
0065     std::cout << encoded.toLatin1().data() << " decoded from hex is ";
0066     std::cout << decoded.toLatin1().data() << std::endl;
0067 
0068     return 0;
0069 }