00001 /* 00002 Copyright (C) 2004, 2006 Brad Hards <bradh@frogmouth.net> 00003 00004 Permission is hereby granted, free of charge, to any person obtaining a copy 00005 of this software and associated documentation files (the "Software"), to deal 00006 in the Software without restriction, including without limitation the rights 00007 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00008 copies of the Software, and to permit persons to whom the Software is 00009 furnished to do so, subject to the following conditions: 00010 00011 The above copyright notice and this permission notice shall be included in 00012 all copies or substantial portions of the Software. 00013 00014 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00015 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 00018 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 00019 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00020 */ 00021 00022 // QtCrypto has the declarations for all of QCA 00023 #include <QtCrypto> 00024 00025 #include <QCoreApplication> 00026 #include <QDebug> 00027 00028 // needed for printf 00029 #include<stdio.h> 00030 00031 int main(int argc, char **argv) 00032 { 00033 QCoreApplication app(argc, argv); 00034 00035 qDebug() << "This example shows hashed MAC"; 00036 00037 // the Initializer object sets things up, and 00038 // also does cleanup when it goes out of scope 00039 QCA::Initializer init; 00040 00041 // we use the first argument as the data to authenticate 00042 // if an argument is provided. Use "hello" if no argument 00043 QByteArray arg = (argc >= 2) ? argv[1] : "hello"; 00044 00045 // we use the second argument as the key to authenticate 00046 // with, if two arguments are provided. Use "secret" as 00047 // the key if less than two arguments. 00048 QCA::SecureArray key((argc >= 3) ? argv[2] : "secret"); 00049 00050 // must always check that an algorithm is supported before using it 00051 if( !QCA::isSupported("hmac(sha1)") ) { 00052 printf("HMAC(SHA1) not supported!\n"); 00053 } else { 00054 // create the required object using HMAC with SHA-1, and an 00055 // empty key. 00056 QCA::MessageAuthenticationCode hmacObject( "hmac(sha1)", QCA::SecureArray() ); 00057 00058 // create the key 00059 QCA::SymmetricKey keyObject(key); 00060 00061 // set the HMAC object to use the key 00062 hmacObject.setup(key); 00063 // that could also have been done in the 00064 // QCA::MessageAuthenticationCode constructor 00065 00066 // we split it into two parts to show incremental update 00067 QCA::SecureArray part1(arg.left(3)); // three chars - "hel" 00068 QCA::SecureArray part2(arg.mid(3)); // the rest - "lo" 00069 hmacObject.update(part1); 00070 hmacObject.update(part2); 00071 00072 // no more updates after calling final. 00073 QCA::SecureArray resultArray = hmacObject.final(); 00074 00075 // convert the result into printable hexadecimal. 00076 QString result = QCA::arrayToHex(resultArray.toByteArray()); 00077 printf("HMAC(SHA1) of \"%s\" with \"%s\" = [%s]\n", arg.data(), key.data(), result.toLatin1().data()); 00078 } 00079 00080 return 0; 00081 } 00082