2004-12-30 09:32:01 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2004 Brad Hards <bradh@frogmouth.net>
|
|
|
|
|
|
|
|
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:
|
2007-04-17 12:01:26 +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.
|
2007-04-17 12:01:26 +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-01-01 03:46:21 +00:00
|
|
|
// QtCrypto/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>
|
|
|
|
|
2005-02-28 23:26:20 +00:00
|
|
|
#include <stdio.h>
|
2003-09-17 00:53:41 +00:00
|
|
|
|
2014-01-02 03:50:18 +06:00
|
|
|
#ifdef QT_STATICPLUGIN
|
|
|
|
#include "import_plugins.h"
|
|
|
|
#endif
|
|
|
|
|
2003-09-17 00:53:41 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2007-04-17 12:01:26 +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
|
2004-11-02 09:08:29 +00:00
|
|
|
QCA::Initializer init;
|
2004-12-30 09:32:01 +00:00
|
|
|
|
2005-02-28 23:26:20 +00:00
|
|
|
QCoreApplication app(argc, argv);
|
|
|
|
|
2004-12-30 09:32:01 +00:00
|
|
|
// we use the first argument if provided, or
|
|
|
|
// use "hello" if no arguments
|
2007-04-13 19:19:18 +00:00
|
|
|
QCA::SecureArray arg = (argc >= 2) ? argv[1] : "hello";
|
2003-09-17 00:53:41 +00:00
|
|
|
|
2004-12-30 09:32:01 +00:00
|
|
|
// must always check that an algorithm is supported before using it
|
2004-11-02 09:08:29 +00:00
|
|
|
if( !QCA::isSupported("sha1") )
|
2003-09-17 00:53:41 +00:00
|
|
|
printf("SHA1 not supported!\n");
|
|
|
|
else {
|
2004-12-30 09:32:01 +00:00
|
|
|
// this shows the "all in one" approach
|
2006-08-23 12:38:39 +00:00
|
|
|
QString result = QCA::Hash("sha1").hashToString(arg);
|
2013-04-02 11:00:43 +00:00
|
|
|
printf("sha1(\"%s\") = [%s]\n", arg.data(), qPrintable(result));
|
2003-09-17 00:53:41 +00:00
|
|
|
}
|
|
|
|
|
2004-12-30 09:32:01 +00:00
|
|
|
// must always check that an algorithm is supported before using it
|
2004-11-02 09:08:29 +00:00
|
|
|
if( !QCA::isSupported("md5") )
|
2003-09-17 00:53:41 +00:00
|
|
|
printf("MD5 not supported!\n");
|
|
|
|
else {
|
2004-12-30 09:32:01 +00:00
|
|
|
// this shows the incremental approach. Naturally
|
|
|
|
// for this simple job, we could use the "all in one"
|
|
|
|
// approach - this is an example, after all :-)
|
2007-04-13 19:19:18 +00:00
|
|
|
QCA::SecureArray part1(arg.toByteArray().left(3)); // three chars - "hel"
|
|
|
|
QCA::SecureArray part2(arg.toByteArray().mid(3)); // the rest - "lo"
|
2004-12-30 09:32:01 +00:00
|
|
|
|
|
|
|
// create the required object.
|
2006-08-23 12:38:39 +00:00
|
|
|
QCA::Hash hashObject("md5");
|
2004-12-30 09:32:01 +00:00
|
|
|
// we split it into two parts to show incremental update
|
|
|
|
hashObject.update(part1);
|
|
|
|
hashObject.update(part2);
|
|
|
|
// no more updates after calling final.
|
2007-04-13 19:19:18 +00:00
|
|
|
QCA::SecureArray resultArray = hashObject.final();
|
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());
|
2013-04-02 11:00:43 +00:00
|
|
|
printf("md5(\"%s\") = [%s]\n", arg.data(), qPrintable(result));
|
2003-09-17 00:53:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|