From 5c102147e5d473ad716e98144f7b05f3266b5d30 Mon Sep 17 00:00:00 2001 From: konrad Date: Sat, 4 Aug 2007 18:27:18 +0000 Subject: [PATCH] add: code39 encoder, hmac algorithm some preps for contacting the webserver git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@20 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33 --- src/code39.cpp | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/code39.h | 16 ++++++++ src/hmac.cpp | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/hmac.h | 49 +++++++++++++++++++++++++ src/keygen.cpp | 8 ++++ src/keygen.h | 7 ++++ src/main.cpp | 4 ++ src/smoke.pro | 7 +++- 8 files changed, 298 insertions(+), 2 deletions(-) create mode 100644 src/code39.cpp create mode 100644 src/code39.h create mode 100644 src/hmac.cpp create mode 100644 src/hmac.h diff --git a/src/code39.cpp b/src/code39.cpp new file mode 100644 index 0000000..a4eb35c --- /dev/null +++ b/src/code39.cpp @@ -0,0 +1,107 @@ +// +// C++ Implementation: code39 +// +// Description: +// +// +// Author: Konrad Rosenbaum , (C) 2007 +// +// Copyright: See README/COPYING files that come with this distribution +// +// + +#include "code39.h" + +#include +#include + +//order of characters for checksum calculation and list of allowed chars +static const QString c39mod="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%"; + +//init pixel map +static QMap initmap() +{ + QMapmap; + // 0=black 1=white pixel + map.insert('*',"0111010001000101"); + map.insert('-',"0111010100010001"); + map.insert('$',"0111011101110101"); + map.insert('%',"0101110111011101"); + map.insert(' ',"0111000101000101"); + map.insert('.',"0001110101000101"); + map.insert('/',"0111011101011101"); + map.insert('+',"0111010111011101"); + map.insert('0',"0101110001000101"); + map.insert('1',"0001011101010001"); + map.insert('2',"0100011101010001"); + map.insert('3',"0001000111010101"); + map.insert('4',"0101110001010001"); + map.insert('5',"0001011100010101"); + map.insert('6',"0100011100010101"); + map.insert('7',"0101110100010001"); + map.insert('8',"0001011101000101"); + map.insert('9',"0100011101000101"); + map.insert('A',"0001010111010001"); + map.insert('B',"0100010111010001"); + map.insert('C',"0001000101110101"); + map.insert('D',"0101000111010001"); + map.insert('E',"0001010001110101"); + map.insert('F',"0100010001110101"); + map.insert('G',"0101011100010001"); + map.insert('H',"0001010111000101"); + map.insert('I',"0100010111000101"); + map.insert('J',"0101000111000101"); + map.insert('K',"0001010101110001"); + map.insert('L',"0100010101110001"); + map.insert('M',"0001000101011101"); + map.insert('0',"0101000101110001"); + map.insert('O',"0001010001011101"); + map.insert('P',"0100010001011101"); + map.insert('Q',"0101010001110001"); + map.insert('R',"0001010100011101"); + map.insert('S',"0100010100011101"); + map.insert('T',"0101000100011101"); + map.insert('U',"0001110101010001"); + map.insert('V',"0111000101010001"); + map.insert('W',"0001110001010101"); + map.insert('X',"0111010001010001"); + map.insert('Y',"0001110100010101"); + map.insert('Z',"0111000100010101"); + return map; +} +//map characters on pixel codes +static const QMapc39map=initmap(); + +//calculate checksum +static char code39mod(QString str) +{ + int sum=0; + for(int i=0;i, (C) 2007 +// +// Copyright: See README/COPYING files that come with this distribution +// +// + +#include +#include + +QPixmap code39(QString); \ No newline at end of file diff --git a/src/hmac.cpp b/src/hmac.cpp new file mode 100644 index 0000000..d6b8138 --- /dev/null +++ b/src/hmac.cpp @@ -0,0 +1,102 @@ +// +// C++ Implementation: hmac +// +// Description: +// +// +// Author: Konrad Rosenbaum , (C) 2007 +// +// Copyright: See README/COPYING files that come with this distribution +// +// + +#include "hmac.h" +#include + +SMHmac::SMHmac(QCryptographicHash::Algorithm algo,const QByteArray&key) + :subhashin(algo),subhashout(algo),state(Collecting) +{ + //calculate key schedules + QByteArray k2=key; + int hlen=blockWidth(algo); + if(k2.size()>hlen){ + //hash the key if it is too long + k2=QCryptographicHash::hash(k2,algo); + }else + if(k2.size(), (C) 2007 +// +// Copyright: See README/COPYING files that come with this distribution +// +// + +#ifndef SMOKE_HMAC_H +#define SMOKE_HMAC_H + +#include + +class SMHmac +{ + public: + /**constructs an object that calculates HMACs*/ + SMHmac(QCryptographicHash::Algorithm algo,const QByteArray&key); + /**adds length bytes of data to the hash-stream*/ + void addData(const char * data, int length ); + /**adds data to the hash-stream*/ + void addData(const QByteArray & data ); + /**reset the hash to the state immediately after construction*/ + void reset(); + /**finalize the hash and return the result*/ + QByteArray result(); + + /**returns the length of the results of given algorithm in bytes; this should have been implemented by QCryptographicHash! performs a test calculation if the algo is unknown*/ + static int resultWidth(QCryptographicHash::Algorithm); + /**returns the length of the blocks used internally in the given algorithm in bytes; this should have been implemented by QCryptographicHash! returns -1 if in doubt*/ + static int blockWidth(QCryptographicHash::Algorithm); + /**complete HMAC calculation in a can*/ + static QByteArray hmac(const QByteArray&data,const QByteArray&key,QCryptographicHash::Algorithm method); + private: + //the two key schedules + QByteArray keyA,keyB; + //remember where we are + enum State {Collecting,Finished}; + mutable State state; + //inner hash function + QCryptographicHash subhashin,subhashout; +}; + + +#endif diff --git a/src/keygen.cpp b/src/keygen.cpp index 3353dae..96ee99a 100644 --- a/src/keygen.cpp +++ b/src/keygen.cpp @@ -162,3 +162,11 @@ QString MKeyGen::getKey() if(randenttype()==QEvent::KeyPress) + qDebug("key"); + return false; +} diff --git a/src/keygen.h b/src/keygen.h index 3f9809e..17c8733 100644 --- a/src/keygen.h +++ b/src/keygen.h @@ -42,4 +42,11 @@ class MKeyGen:public QDialog void updateProps(); }; +class EFilter:public QObject +{ + Q_OBJECT + protected: + bool eventFilter(QObject*,QEvent*); +}; + #endif diff --git a/src/main.cpp b/src/main.cpp index c03dbc7..9edd26b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,11 +19,15 @@ #include "keygen.h" #include "mainwindow.h" +#include "hmac.h" int main(int argc,char**argv) { QApplication app(argc,argv); + EFilter ef; + app.installEventFilter(&ef); + app.setOrganizationName("MagicSmoke"); app.setApplicationName("MagicSmoke"); diff --git a/src/smoke.pro b/src/smoke.pro index 037800c..41e2f34 100644 --- a/src/smoke.pro +++ b/src/smoke.pro @@ -17,10 +17,13 @@ MSVERSION = "0.1 alpha" SOURCES = \ main.cpp \ keygen.cpp \ - mainwindow.cpp + mainwindow.cpp \ + hmac.cpp \ + code39.cpp HEADERS = \ keygen.h \ - mainwindow.h + mainwindow.h \ + hmac.h TRANSLATIONS = \ smoke_de.ts \ -- 1.7.2.5