File indexing completed on 2024-04-28 04:43:39

0001 /*
0002  Copyright (C) 2004, 2006 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 #include <QDebug>
0027 
0028 // needed for printf
0029 #include <cstdio>
0030 
0031 #ifdef QT_STATICPLUGIN
0032 #include "import_plugins.h"
0033 #endif
0034 
0035 int main(int argc, char **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     QCoreApplication app(argc, argv);
0042 
0043     qDebug() << "This example shows hashed MAC";
0044 
0045     // we use the first argument as the data to authenticate
0046     // if an argument is provided. Use "hello" if no argument
0047     QByteArray arg = (argc >= 2) ? argv[1] : "hello";
0048 
0049     // we use the second argument as the key to authenticate
0050     // with, if two arguments are provided. Use "secret" as
0051     // the key if less than two arguments.
0052     QCA::SecureArray key((argc >= 3) ? argv[2] : "secret");
0053 
0054     // must always check that an algorithm is supported before using it
0055     if (!QCA::isSupported("hmac(sha1)")) {
0056         printf("HMAC(SHA1) not supported!\n");
0057     } else {
0058         // create the required object using HMAC with SHA-1, and an
0059         // empty key.
0060         QCA::MessageAuthenticationCode hmacObject(QStringLiteral("hmac(sha1)"), QCA::SecureArray());
0061 
0062         // create the key
0063         QCA::SymmetricKey keyObject(key);
0064 
0065         // set the HMAC object to use the key
0066         hmacObject.setup(key);
0067         // that could also have been done in the
0068         // QCA::MessageAuthenticationCode constructor
0069 
0070         // we split it into two parts to show incremental update
0071         QCA::SecureArray part1(arg.left(3)); // three chars - "hel"
0072         QCA::SecureArray part2(arg.mid(3));  // the rest - "lo"
0073         hmacObject.update(part1);
0074         hmacObject.update(part2);
0075 
0076         // no more updates after calling final.
0077         QCA::SecureArray resultArray = hmacObject.final();
0078 
0079         // convert the result into printable hexadecimal.
0080         QString result = QCA::arrayToHex(resultArray.toByteArray());
0081         printf("HMAC(SHA1) of \"%s\" with \"%s\" = [%s]\n", arg.data(), key.data(), result.toLatin1().data());
0082     }
0083 
0084     return 0;
0085 }