4
0
mirror of https://github.com/QuasarApp/qca.git synced 2025-05-10 17:59:33 +00:00

Big examples update. New examples for showing HMAC, and changes to

show the two ways to do a basic hash. Also, an example of some of the
options for working with providers. More comments to explain everything.

This also adds copyright notices for everything, and notes on the
pre-QCA2 examples.

svn path=/trunk/kdesupport/qca/; revision=374181
This commit is contained in:
Brad Hards 2004-12-30 09:32:01 +00:00
parent 4b1b4a73ab
commit 789799583a
12 changed files with 346 additions and 38 deletions

@ -1,3 +1,25 @@
/*
Copyright (C) 2003 Justin Karneges
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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
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.
*/
// TODO: this code needs to be updated for QCA2
#include<qdom.h>
#include<qfile.h>
#include"base64.h"

@ -1,19 +1,38 @@
/*
Copyright (C) 2003 Justin Karneges
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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
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.
*/
// TODO: this code needs to be updated for QCA2
#include"qca.h"
#include<stdio.h>
static QCString arrayToCString(const QByteArray &);
static QByteArray cstringToArray(const QCString &);
static void doDynTest(QCA::Cipher *c, const QString &name, const QCString &cs);
int main(int argc, char **argv)
{
QCA::init();
QCA::Initializer init;
QCString cs = (argc >= 2) ? argv[1] : "hello";
// AES128 test
if(!QCA::isSupported(QCA::CAP_AES128))
if(!QCA::isSupported("aes128"))
printf("AES128 not supported!\n");
else {
#if 0
// encrypt
QByteArray key = QCA::AES128::generateKey();
QByteArray iv = QCA::AES128::generateIV();
@ -30,42 +49,29 @@ int main(int argc, char **argv)
d.update(f);
QCString dec = arrayToCString(d.final());
printf("<aes128(\"%s\") = [%s]\n", result.latin1(), dec.data());
#endif
}
// BlowFish, TripleDES, and AES256 tested dynamically
if(!QCA::isSupported(QCA::CAP_BlowFish))
if(!QCA::isSupported("blowfish"))
printf("BlowFish not supported!\n");
else
doDynTest(new QCA::BlowFish, "bfish", cs);
// else
// doDynTest(new QCA::BlowFish, "bfish", cs);
if(!QCA::isSupported(QCA::CAP_TripleDES))
if(!QCA::isSupported("tripledes"))
printf("TripleDES not supported!\n");
else
doDynTest(new QCA::TripleDES, "3des", cs);
// else
// doDynTest(new QCA::TripleDES, "3des", cs);
if(!QCA::isSupported(QCA::CAP_AES256))
if(!QCA::isSupported("aes256"))
printf("AES256 not supported!\n");
else
doDynTest(new QCA::AES256, "aes256", cs);
// else
// doDynTest(new QCA::AES256, "aes256", cs);
return 0;
}
QCString arrayToCString(const QByteArray &a)
{
QCString cs;
cs.resize(a.size()+1);
memcpy(cs.data(), a.data(), a.size());
return cs;
}
QByteArray cstringToArray(const QCString &cs)
{
QByteArray a(cs.length());
memcpy(a.data(), cs.data(), a.size());
return a;
}
#if 0
void doDynTest(QCA::Cipher *c, const QString &name, const QCString &cs)
{
// encrypt
@ -86,4 +92,4 @@ void doDynTest(QCA::Cipher *c, const QString &name, const QCString &cs)
printf("<%s(\"%s\") = [%s]\n", name.latin1(), result.latin1(), dec.data());
delete c;
}
#endif

@ -2,10 +2,13 @@ TEMPLATE = subdirs
SUBDIRS += \
hashtest \
ciphertest \
rsatest \
certtest \
ssltest \
sslservtest \
sasltest
mactest \
providertest
# ciphertest \
# rsatest \
# certtest \
# ssltest \
# sslservtest \
# sasltest

@ -1,22 +1,67 @@
/*
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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
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.
*/
// qca.h has the declarations for all of QCA
#include"qca.h"
// needed for printf
#include<stdio.h>
int main(int argc, char **argv)
{
// the Initializer object sets things up, and
// also does cleanup when it goes out of scope
QCA::Initializer init;
// we use the first argument if provided, or
// use "hello" if no arguments
QCString cs = (argc >= 2) ? argv[1] : "hello";
// must always check that an algorithm is supported before using it
if( !QCA::isSupported("sha1") )
printf("SHA1 not supported!\n");
else {
// this shows the "all in one" approach
QString result = QCA::SHA1().hashToString(cs);
printf("sha1(\"%s\") = [%s]\n", cs.data(), result.latin1());
}
// must always check that an algorithm is supported before using it
if( !QCA::isSupported("md5") )
printf("MD5 not supported!\n");
else {
QString result = QCA::MD5().hashToString(cs);
// this shows the incremental approach. Naturally
// for this simple job, we could use the "all in one"
// approach - this is an example, after all :-)
QSecureArray part1(cs.left(3)); // three chars - "hel"
QSecureArray part2(cs.mid(3)); // the rest - "lo"
// create the required object.
QCA::MD5 hashObject;
// we split it into two parts to show incremental update
hashObject.update(part1);
hashObject.update(part2);
// no more updates after calling final.
QSecureArray resultArray = hashObject.final();
// convert the result into printable hexadecimal.
QString result = QCA::arrayToHex(resultArray);
printf("md5(\"%s\") = [%s]\n", cs.data(), result.latin1());
}

@ -0,0 +1,69 @@
/*
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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
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.
*/
// qca.h has the declarations for all of QCA
#include"qca.h"
// needed for printf
#include<stdio.h>
int main(int argc, char **argv)
{
// the Initializer object sets things up, and
// also does cleanup when it goes out of scope
QCA::Initializer init;
// we use the first argument as the data to authenticate
// if an argument is provided. Use "hello" if no argument
QCString cs = (argc >= 2) ? argv[1] : "hello";
// 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.
QCString key = (argc >= 3) ? argv[2] : "secret";
// must always check that an algorithm is supported before using it
if( !QCA::isSupported("hmac(sha1)") ) {
printf("HMAC(SHA1) not supported!\n");
} else {
// create the required object. This is equivalent
// to QCA::HMAC hmacObject("sha1").
QCA::HMAC hmacObject;
// create the key
QCA::SymmetricKey keyObject(key);
// we split it into two parts to show incremental update
QSecureArray part1(cs.left(3)); // three chars - "hel"
QSecureArray part2(cs.mid(3)); // the rest - "lo"
hmacObject.update(part1);
hmacObject.update(part2);
// no more updates after calling final.
QSecureArray resultArray = hmacObject.final();
// convert the result into printable hexadecimal.
QString result = QCA::arrayToHex(resultArray);
printf("HMAC(SHA1) of \"%s\" with \"%s\" = [%s]\n", cs.data(), key.data(), result.latin1());
}
return 0;
}

@ -0,0 +1,6 @@
TEMPLATE = app
CONFIG += thread console
TARGET = mactest
SOURCES += mactest.cpp
include(../examples.pri)

@ -0,0 +1,63 @@
/*
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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
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.
*/
#include"qca.h"
#include <iostream>
#include <qstringlist.h>
int main(int argc, char **argv)
{
Q_UNUSED( argc );
Q_UNUSED( argv );
// the Initializer object sets things up, and
// also does cleanup when it goes out of scope
QCA::Initializer init;
// this is a hack to get all the available providers loaded.
QCA::isSupported("foo");
// this gives us all the plugin providers as a list
QCA::ProviderList qcaProviders = QCA::providers();
// which we can iterate over...
QCA::ProviderListIterator it( qcaProviders );
QCA::Provider *provider;
while ( 0 != (provider = it.current() ) ) {
++it;
// each provider has a name, which we can display
std::cout << provider->name() << ": ";
// ... and also a list of features
QStringList capabilities = provider->features();
// we turn the string list back into a single string,
// and display it as well
std::cout << capabilities.join(", ") << std::endl;
}
// Note that the default provider isn't included in
// the result of QCA::providers()
std::cout << "default: ";
// However it is still possible to get the features
// supported by the default provider
QStringList capabilities = QCA::defaultFeatures();
std::cout << capabilities.join(", ") << std::endl;
return 0;
}

@ -0,0 +1,6 @@
TEMPLATE = app
CONFIG += thread console
TARGET = providertest
SOURCES += providertest.cpp
include(../examples.pri)

@ -1,3 +1,25 @@
/*
Copyright (C) 2003 Justin Karneges
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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
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.
*/
// TODO: this code needs to be updated for QCA2
#include<qfile.h>
#include<qfileinfo.h>
#include"qca.h"

@ -1,3 +1,25 @@
/*
Copyright (C) 2003 Justin Karneges
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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
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.
*/
// TODO: this code needs to be updated for QCA2
#include<qapplication.h>
#include<qtimer.h>
#include<qsocket.h>

@ -1,3 +1,25 @@
/*
Copyright (C) 2003 Justin Karneges
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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
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.
*/
// TODO: this code needs to be updated for QCA2
#include<qapplication.h>
#include<qfile.h>
#include<qsocket.h>

@ -1,3 +1,25 @@
/*
Copyright (C) 2003 Justin Karneges
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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
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.
*/
// TODO: this code needs to be updated for QCA2
#include<qapplication.h>
#include<qdom.h>
#include<qfile.h>