2004-12-30 09:32:01 +00:00
|
|
|
/*
|
2006-01-28 06:54:42 +00:00
|
|
|
Copyright (C) 2004, 2006 Brad Hards <bradh@frogmouth.net>
|
2004-12-30 09:32:01 +00:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
2006-11-16 10:58:43 +00:00
|
|
|
|
2004-12-30 09:32:01 +00:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2006-11-16 10:58:43 +00:00
|
|
|
|
2004-12-30 09:32:01 +00:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
|
|
|
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2005-03-01 02:07:22 +00:00
|
|
|
// QtCrypto has the declarations for all of QCA
|
2005-01-01 05:34:22 +00:00
|
|
|
#include <QtCrypto>
|
2007-04-17 12:01:26 +00:00
|
|
|
|
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QDebug>
|
|
|
|
|
2004-12-30 09:32:01 +00:00
|
|
|
// needed for printf
|
2020-02-12 17:14:33 +01:00
|
|
|
#include <cstdio>
|
2004-12-30 09:32:01 +00:00
|
|
|
|
2014-01-02 03:50:18 +06:00
|
|
|
#ifdef QT_STATICPLUGIN
|
|
|
|
#include "import_plugins.h"
|
|
|
|
#endif
|
|
|
|
|
2004-12-30 09:32:01 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2005-03-01 02:07:22 +00:00
|
|
|
// the Initializer object sets things up, and
|
2004-12-30 09:32:01 +00:00
|
|
|
// also does cleanup when it goes out of scope
|
|
|
|
QCA::Initializer init;
|
2020-09-06 20:19:00 +02:00
|
|
|
|
2009-04-24 17:59:20 +00:00
|
|
|
QCoreApplication app(argc, argv);
|
2020-09-06 20:19:00 +02:00
|
|
|
|
2009-04-24 17:59:20 +00:00
|
|
|
qDebug() << "This example shows hashed MAC";
|
2020-09-06 20:19:00 +02:00
|
|
|
|
2004-12-30 09:32:01 +00:00
|
|
|
// we use the first argument as the data to authenticate
|
|
|
|
// if an argument is provided. Use "hello" if no argument
|
2005-03-01 02:07:22 +00:00
|
|
|
QByteArray arg = (argc >= 2) ? argv[1] : "hello";
|
2020-09-06 20:19:00 +02:00
|
|
|
|
2004-12-30 09:32:01 +00:00
|
|
|
// we use the second argument as the key to authenticate
|
|
|
|
// with, if two arguments are provided. Use "secret" as
|
|
|
|
// the key if less than two arguments.
|
2007-04-13 19:19:18 +00:00
|
|
|
QCA::SecureArray key((argc >= 3) ? argv[2] : "secret");
|
2020-09-06 20:19:00 +02:00
|
|
|
|
2004-12-30 09:32:01 +00:00
|
|
|
// must always check that an algorithm is supported before using it
|
|
|
|
if (!QCA::isSupported("hmac(sha1)")) {
|
|
|
|
printf("HMAC(SHA1) not supported!\n");
|
|
|
|
} else {
|
2006-11-16 10:58:43 +00:00
|
|
|
// create the required object using HMAC with SHA-1, and an
|
|
|
|
// empty key.
|
2020-02-10 00:29:23 +01:00
|
|
|
QCA::MessageAuthenticationCode hmacObject(QStringLiteral("hmac(sha1)"), QCA::SecureArray());
|
2020-09-06 20:19:00 +02:00
|
|
|
|
2004-12-30 09:32:01 +00:00
|
|
|
// create the key
|
|
|
|
QCA::SymmetricKey keyObject(key);
|
2020-09-06 20:19:00 +02:00
|
|
|
|
2006-01-28 06:54:42 +00:00
|
|
|
// set the HMAC object to use the key
|
|
|
|
hmacObject.setup(key);
|
2006-08-23 12:38:39 +00:00
|
|
|
// that could also have been done in the
|
|
|
|
// QCA::MessageAuthenticationCode constructor
|
2020-09-06 20:19:00 +02:00
|
|
|
|
2004-12-30 09:32:01 +00:00
|
|
|
// we split it into two parts to show incremental update
|
2007-04-13 19:19:18 +00:00
|
|
|
QCA::SecureArray part1(arg.left(3)); // three chars - "hel"
|
|
|
|
QCA::SecureArray part2(arg.mid(3)); // the rest - "lo"
|
2004-12-30 09:32:01 +00:00
|
|
|
hmacObject.update(part1);
|
|
|
|
hmacObject.update(part2);
|
2020-09-06 20:19:00 +02:00
|
|
|
|
2004-12-30 09:32:01 +00:00
|
|
|
// no more updates after calling final.
|
2007-04-13 19:19:18 +00:00
|
|
|
QCA::SecureArray resultArray = hmacObject.final();
|
2020-09-06 20:19:00 +02:00
|
|
|
|
2004-12-30 09:32:01 +00:00
|
|
|
// convert the result into printable hexadecimal.
|
2007-06-13 02:30:43 +00:00
|
|
|
QString result = QCA::arrayToHex(resultArray.toByteArray());
|
2005-03-01 02:07:22 +00:00
|
|
|
printf("HMAC(SHA1) of \"%s\" with \"%s\" = [%s]\n", arg.data(), key.data(), result.toLatin1().data());
|
2004-12-30 09:32:01 +00:00
|
|
|
}
|
2020-09-06 20:19:00 +02:00
|
|
|
|
2004-12-30 09:32:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|