hextest.cpp

The code below shows some simple operations on a QCA::Hex object, converting between QCA::SecureArray and QString.

00001 /*
00002  Copyright (C) 2005 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 
00027 #include <iostream>
00028 
00029 int main(int argc, char **argv)
00030 {
00031         QCoreApplication app(argc, argv);
00032 
00033         // the Initializer object sets things up, and
00034         // also does cleanup when it goes out of scope
00035         QCA::Initializer init;
00036 
00037         // we use the first argument as the data to encode / decode
00038         // if an argument is provided. Use "hello" if no argument
00039         QByteArray arg; // empty array
00040         arg.append((argc >= 2) ? argv[1] : "hello");
00041 
00042         // create our object, which does encoding by default
00043         // QCA::Hex encoder(QCA::Encode); is equivalent
00044         QCA::Hex encoder;
00045 
00046         // You might prefer to use encoder.encode(); and have
00047         // it return a QCA::SecureArray, depending on your needs
00048         QString encoded = encoder.arrayToString(arg);
00049 
00050         std::cout << arg.data() << " in hex encoding is ";
00051         std::cout << encoded.toLatin1().data() << std::endl;
00052 
00053         // This time, we'll create an object to decode hexadecimal.
00054         // We could also have reused the existing object, calling
00055         // clear(); and setup(QCA::Decode); on it.
00056         QCA::Hex decoder(QCA::Decode);
00057 
00058         // This time, we convert a QString into a QString
00059         QString decoded = decoder.decodeString(encoded);
00060 
00061         std::cout << encoded.toLatin1().data() << " decoded from hex is ";
00062         std::cout << decoded.toLatin1().data() << std::endl;
00063 
00064         return 0;
00065 }
00066 

Generated on Thu Sep 6 19:13:35 2007 for Qt Cryptographic Architecture by  doxygen 1.5.2