File indexing completed on 2024-09-15 04:15:56
0001 /* 0002 Copyright (C) 2004 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/QtCrypto has the declarations for all of QCA 0023 #include <QtCrypto> 0024 0025 #include <QCoreApplication> 0026 0027 #include <cstdio> 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 if provided, or 0042 // use "hello" if no arguments 0043 QCA::SecureArray arg = (argc >= 2) ? argv[1] : "hello"; 0044 0045 // must always check that an algorithm is supported before using it 0046 if (!QCA::isSupported("sha1")) 0047 printf("SHA1 not supported!\n"); 0048 else { 0049 // this shows the "all in one" approach 0050 QString result = QCA::Hash(QStringLiteral("sha1")).hashToString(arg); 0051 printf("sha1(\"%s\") = [%s]\n", arg.data(), qPrintable(result)); 0052 } 0053 0054 // must always check that an algorithm is supported before using it 0055 if (!QCA::isSupported("md5")) 0056 printf("MD5 not supported!\n"); 0057 else { 0058 // this shows the incremental approach. Naturally 0059 // for this simple job, we could use the "all in one" 0060 // approach - this is an example, after all :-) 0061 QCA::SecureArray part1(arg.toByteArray().left(3)); // three chars - "hel" 0062 QCA::SecureArray part2(arg.toByteArray().mid(3)); // the rest - "lo" 0063 0064 // create the required object. 0065 QCA::Hash hashObject(QStringLiteral("md5")); 0066 // we split it into two parts to show incremental update 0067 hashObject.update(part1); 0068 hashObject.update(part2); 0069 // no more updates after calling final. 0070 QCA::SecureArray resultArray = hashObject.final(); 0071 // convert the result into printable hexadecimal. 0072 QString result = QCA::arrayToHex(resultArray.toByteArray()); 0073 printf("md5(\"%s\") = [%s]\n", arg.data(), qPrintable(result)); 0074 } 0075 0076 return 0; 0077 }