1 /***************************************************************************
2 * Copyright (C) 2005 by Tarek Saidi *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; version 2 of the License. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 CArcFour SecString::RC4;
22 quint8* SecString::sessionkey;
24 SecString::operator QString(){
28 SecString::SecString(){
32 int SecString::length(){
36 SecString::~SecString(){
40 void SecString::lock(){
46 void SecString::unlock(){
51 const unsigned char* buffer = new unsigned char[crypt.length()];
52 RC4.decrypt( (quint8*)crypt.data(), (unsigned char*)buffer, crypt.length() );
53 plain = QString::fromUtf8((const char*)buffer, crypt.size());
54 overwrite((unsigned char*)buffer, crypt.size());
58 const QString& SecString::string(){
59 Q_ASSERT_X(!locked, "SecString::string()", "string is locked");
63 void SecString::setString(QString& str, bool DeleteSource){
64 QByteArray StrData = str.toUtf8();
65 int len = StrData.size();
66 unsigned char* buffer = new unsigned char[len];
67 RC4.encrypt((const unsigned char*)StrData.data(), buffer, len);
68 crypt = QByteArray((const char*)buffer, len);
69 overwrite(buffer, len);
70 overwrite((unsigned char*)StrData.data(), len);
79 void SecString::overwrite(unsigned char* str, int strlen){
80 if(strlen==0 || str==NULL)
83 for(int i=0; i<strlen; i++)
87 void SecString::overwrite(QString& str){
88 for (int i=0; i<str.length(); i++) {
93 void SecString::generateSessionKey(){
94 sessionkey = new quint8[32];
95 if (!lockPage(sessionkey, 32))
96 qDebug("Failed to lock session key page");
97 randomize(sessionkey, 32);
98 RC4.setKey(sessionkey, 32);
101 void SecString::deleteSessionKey() {
102 overwrite(sessionkey, 32);
103 unlockPage(sessionkey, 32);
108 SecData::SecData(int len) : locked(true){
110 data = new quint8[len];
115 for (int i=0; i<length; i++)
121 void SecData::lock(){
123 SecString::RC4.encrypt(data, data, length);
127 void SecData::unlock(){
129 SecString::RC4.decrypt(data, data, length);
133 void SecData::copyData(quint8* src){
135 memcpy(data, src, length);
139 void SecData::copyData(SecData& secData){
145 quint8* SecData::operator*(){