00001 /* 00002 Copyright (C) 2004 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/QtCrypto has the declarations for all of QCA 00023 #include <QtCrypto> 00024 00025 #include <QCoreApplication> 00026 00027 #include <stdio.h> 00028 00029 int main(int argc, char **argv) 00030 { 00031 // the Initializer object sets things up, and 00032 // also does cleanup when it goes out of scope 00033 QCA::Initializer init; 00034 00035 QCoreApplication app(argc, argv); 00036 00037 // we use the first argument if provided, or 00038 // use "hello" if no arguments 00039 QCA::SecureArray arg = (argc >= 2) ? argv[1] : "hello"; 00040 00041 // must always check that an algorithm is supported before using it 00042 if( !QCA::isSupported("sha1") ) 00043 printf("SHA1 not supported!\n"); 00044 else { 00045 // this shows the "all in one" approach 00046 QString result = QCA::Hash("sha1").hashToString(arg); 00047 printf("sha1(\"%s\") = [%s]\n", arg.data(), result.toAscii().data()); 00048 } 00049 00050 // must always check that an algorithm is supported before using it 00051 if( !QCA::isSupported("md5") ) 00052 printf("MD5 not supported!\n"); 00053 else { 00054 // this shows the incremental approach. Naturally 00055 // for this simple job, we could use the "all in one" 00056 // approach - this is an example, after all :-) 00057 QCA::SecureArray part1(arg.toByteArray().left(3)); // three chars - "hel" 00058 QCA::SecureArray part2(arg.toByteArray().mid(3)); // the rest - "lo" 00059 00060 // create the required object. 00061 QCA::Hash hashObject("md5"); 00062 // we split it into two parts to show incremental update 00063 hashObject.update(part1); 00064 hashObject.update(part2); 00065 // no more updates after calling final. 00066 QCA::SecureArray resultArray = hashObject.final(); 00067 // convert the result into printable hexadecimal. 00068 QString result = QCA::arrayToHex(resultArray.toByteArray()); 00069 printf("md5(\"%s\") = [%s]\n", arg.data(), result.toAscii().data()); 00070 } 00071 00072 return 0; 00073 } 00074