×
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
NegInfinity (Talk | contribs)
hamishwillee (Talk | contribs)
m (Hamishwillee - Update reviewer and articlemetadata)
Line 1: Line 1:
[[Category:Qt]][[Category:UI]]
+
[[Category:Qt]][[Category:UI]][[Category:Code Examples]]
{{ReviewerApproved|timestamp=20090928}}
+
{{ReviewerApproved|user=[[User:Hamishwillee|<br />----]]|timestamp=20110915233119 }}
 
{{ArticleMetaData
 
{{ArticleMetaData
|id=...
 
 
|platform=S60 3rd Edition FP1, S60 3rd Edition FP2, S60 5th Edition  
 
|platform=S60 3rd Edition FP1, S60 3rd Edition FP2, S60 5th Edition  
 
|devices= S60 Emulator
 
|devices= S60 Emulator
|category=Qt
 
|subcategory=UI
 
 
|creationdate=10 April 2009
 
|creationdate=10 April 2009
 
|keywords=QCryptographicHash
 
|keywords=QCryptographicHash
 
 
|sourcecode= <!-- Link to example source code (e.g. [[Media:The Code Example ZIP.zip]]) -->
 
|sourcecode= <!-- Link to example source code (e.g. [[Media:The Code Example ZIP.zip]]) -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
Line 15: Line 11:
 
|devicecompatability=<!-- Compatible devices (e.g.: All* (must have GPS) ) -->
 
|devicecompatability=<!-- Compatible devices (e.g.: All* (must have GPS) ) -->
 
|signing=<!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
 
|signing=<!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
|capabilities=<!-- Capabilities required (e.g. Location, NetworkServices. -->)
+
|capabilities=<!-- Capabilities required (e.g. Location, NetworkServices.) -->
 
|author=[[User:Mind freak]]
 
|author=[[User:Mind freak]]
 +
 +
|reviewer=[[User:NegInfinity]]
 +
|timestamp=20110915
 
}}
 
}}
  
 
== Introduction ==
 
== Introduction ==
 
{{Abstract|visible=true|This example shows how to calculate hash for a text string using  [http://doc.qt.nokia.com/stable/qcryptographichash.html QCryptographicHash]. }}
 
{{Abstract|visible=true|This example shows how to calculate hash for a text string using  [http://doc.qt.nokia.com/stable/qcryptographichash.html QCryptographicHash]. }}
 
==Preconditions==
 
 
* Download and install the [[Qt SDK]]
 
 
  
 
== Some Related Function ==
 
== Some Related Function ==
 
*Adds the data to the cryptographic hash:
 
*Adds the data to the cryptographic hash:
 
QByteArray string = "Nokia";
 
QCryptographicHash hasher(QCryptographicHash::Sha1);
 
hasher.addData(string);
 
  
 +
<code cpp>
 +
QByteArray string = "Nokia";
 +
QCryptographicHash hasher(QCryptographicHash::Sha1);
 +
hasher.addData(string);
 +
</code>
 
*Returns the final hash value.
 
*Returns the final hash value.
 
+
<code cpp>
QByteArray string1=hasher.result();
+
QByteArray string1=hasher.result();
 
+
</code>
 
== Source Code ==
 
== Source Code ==
  
Line 103: Line 98:
 
</code>
 
</code>
  
[[Category:Code Examples]]
+
 
 
==ScreenShot==
 
==ScreenShot==
  
 
[[Image:hash.png]]
 
[[Image:hash.png]]

Revision as of 02:31, 16 September 2011

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

Contents

Introduction

This example shows how to calculate hash for a text string using QCryptographicHash.

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

Hash.png

334 page views in the last 30 days.
×