File indexing completed on 2024-10-06 07:22:34
0001 /* 0002 Copyright (C) 2004-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 QCoreApplication(argc, argv); 0036 0037 // the Initializer object sets things up, and 0038 // also does cleanup when it goes out of scope 0039 QCA::Initializer init; 0040 0041 // we use the first argument as the data to encode 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::Base64 encoder(QCA::Encode); is equivalent 0048 QCA::Base64 encoder; 0049 0050 // This does the actual conversion (encoding). 0051 // You might prefer to use encoder.encode(); and have 0052 // it return a QCA::SecureArray, depending on your needs 0053 QString encoded = encoder.arrayToString(arg); 0054 0055 std::cout << arg.data() << " in base64 encoding is "; 0056 std::cout << encoded.toLatin1().data() << std::endl; 0057 0058 // This time, we'll create an object to decode base64. We 0059 // could also have reused the existing object, calling 0060 // clear(); and setup(QCA::Decode); on it. 0061 QCA::Base64 decoder(QCA::Decode); 0062 0063 // This time, we convert a QString into a QString 0064 QString decoded = decoder.decodeString(encoded); 0065 0066 std::cout << encoded.toLatin1().data() << " decoded from base64 is "; 0067 std::cout << decoded.toLatin1().data() << std::endl; 0068 0069 return 0; 0070 }