×
Namespaces

Variants
Actions
(Difference between revisions)

How to calculate hash for a text using QCryptographicHash in Qt

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot change of template (Template:CodeSnippet) - now using Template:ArticleMetaData)
NegInfinity (Talk | contribs)
(NegInfinity - - Main.cpp)
Line 48: Line 48:
 
#include <QString>
 
#include <QString>
 
#include <QByteArray>
 
#include <QByteArray>
 +
#include <QLineEdit>
 
#include <QLabel>
 
#include <QLabel>
 +
#include <QGridLayout>
 +
#include <QPushButton>
 +
#include <QHBoxLayout>
 +
#include "hash.h"
  
int main(int argc, char *argv[])
+
int main(int argc, char **argv){
{
+
QApplication app(argc, argv);
    QApplication a(argc, argv);
+
 
    QWidget *win=new QWidget();
+
QWidget mainWindow;
    QHBoxLayout *lay=new QHBoxLayout();
+
QGridLayout* layout=new QGridLayout();
    QLabel *lbl=new QLabel();
+
 
    QLabel *lbl1=new QLabel("Encrypted Text:");
+
//widget initialization
    lbl1->setBuddy(lbl);
+
QLineEdit *hashTextEdit=new QLineEdit(),
    QByteArray string="Nokia";
+
*originalTextEdit = new QLineEdit();
    QCryptographicHash *hash=new QCryptographicHash(QCryptographicHash::Md4);
+
originalTextEdit->setReadOnly(true);
    hash->addData(string);
+
originalTextEdit->setText(QObject::tr("Nokia"));
    QByteArray string1=hash->result();
+
hashTextEdit->setReadOnly(true);
    lbl->setText(string1); // TODO: use e.g. toHex or toBase64
+
QLabel *hashLabel=new QLabel(QObject::tr("MD4 &Hash:")),
    lay->addWidget(lbl1);
+
*originalTextLabel = new QLabel(QObject::tr("Original &Text:"));
    lay->addWidget(lbl);
+
hashLabel->setBuddy(hashTextEdit);
    win->setLayout(lay);
+
originalTextLabel->setBuddy(originalTextEdit);
    win->setStyleSheet("* { background-color:rgb(199,147,88); padding: 7px ;  color:rgb(255,255,255)}");
+
 
    win->showMaximized();
+
//widget placement
     return a.exec();
+
layout->addWidget(originalTextLabel, 0, 0);
 +
layout->addWidget(originalTextEdit, 0, 1);
 +
layout->addWidget(hashLabel, 1, 0);
 +
layout->addWidget(hashTextEdit, 1, 1);
 +
 
 +
//bottom layout initialization ("close" button)
 +
QHBoxLayout* bottomLayout = new QHBoxLayout();
 +
bottomLayout->addStretch();
 +
QPushButton* closeButton = new QPushButton(QObject::tr("C&lose"));
 +
bottomLayout->addWidget(closeButton);
 +
bottomLayout->addStretch();
 +
bottomLayout->setMargin(0);
 +
 
 +
layout->addLayout(bottomLayout, 2, 0, 1, 2);
 +
 
 +
QObject::connect(closeButton, SIGNAL(clicked()), &mainWindow, SLOT(close()));
 +
 
 +
mainWindow.setLayout(layout);
 +
 
 +
//actual hash calculation TODO: make proper widget with signal/slots for hash calculation
 +
QCryptographicHash hash(QCryptographicHash::Md4);
 +
hash.addData(originalTextEdit->text().toUtf8());
 +
hashTextEdit->setText(QString(hash.result().toHex()));
 +
 
 +
mainWindow.showMaximized();
 +
      
 +
return app.exec();
 
}
 
}
 
</code>
 
</code>

Revision as of 13:51, 15 September 2011

{{{width}}}
28 Sep
2009
Article Metadata
Tested with
Devices(s): S60 Emulator
Compatibility
Platform(s): S60 3rd Edition FP1, S60 3rd Edition FP2, S60 5th Edition
Symbian
Platform Security
Capabilities: )
Article
Keywords: QCryptographicHash
Created: mind_freak (10 Apr 2009)
Last edited: NegInfinity (15 Sep 2011)

Contents

Introduction

This example shows how to encrypt text using a QCryptographicHash.

Preconditions

  • Download and install the Qt SDK


Some Related Function

  • Adds the data to the cryptographic hash:
QByteArray string = "Nokia";
QCryptographicHash hasher(QCryptographicHash::Sha1);
hasher.addData(string);
  • Returns the final hash value.
QByteArray string1=hasher.result();

Source Code

Main.cpp

#include <QtGui/QApplication>
#include <QWidget>
#include <QHBoxLayout>
#include <QCryptographicHash>
#include <QString>
#include <QByteArray>
#include <QLineEdit>
#include <QLabel>
#include <QGridLayout>
#include <QPushButton>
#include <QHBoxLayout>
#include "hash.h"
 
int main(int argc, char **argv){
QApplication app(argc, argv);
 
QWidget mainWindow;
QGridLayout* layout=new QGridLayout();
 
//widget initialization
QLineEdit *hashTextEdit=new QLineEdit(),
*originalTextEdit = new QLineEdit();
originalTextEdit->setReadOnly(true);
originalTextEdit->setText(QObject::tr("Nokia"));
hashTextEdit->setReadOnly(true);
QLabel *hashLabel=new QLabel(QObject::tr("MD4 &Hash:")),
*originalTextLabel = new QLabel(QObject::tr("Original &Text:"));
hashLabel->setBuddy(hashTextEdit);
originalTextLabel->setBuddy(originalTextEdit);
 
//widget placement
layout->addWidget(originalTextLabel, 0, 0);
layout->addWidget(originalTextEdit, 0, 1);
layout->addWidget(hashLabel, 1, 0);
layout->addWidget(hashTextEdit, 1, 1);
 
//bottom layout initialization ("close" button)
QHBoxLayout* bottomLayout = new QHBoxLayout();
bottomLayout->addStretch();
QPushButton* closeButton = new QPushButton(QObject::tr("C&lose"));
bottomLayout->addWidget(closeButton);
bottomLayout->addStretch();
bottomLayout->setMargin(0);
 
layout->addLayout(bottomLayout, 2, 0, 1, 2);
 
QObject::connect(closeButton, SIGNAL(clicked()), &mainWindow, SLOT(close()));
 
mainWindow.setLayout(layout);
 
//actual hash calculation TODO: make proper widget with signal/slots for hash calculation
QCryptographicHash hash(QCryptographicHash::Md4);
hash.addData(originalTextEdit->text().toUtf8());
hashTextEdit->setText(QString(hash.result().toHex()));
 
mainWindow.showMaximized();
 
return app.exec();
}

ScreenShot

Crypto.JPG

367 page views in the last 30 days.
×