×
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 - Minor update to ArticleMetaData)
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot update - Fix ArticleMetaData and RevieweApproval)
Line 1: Line 1:
[[Category:Qt]][[Category:UI]][[Category:Code Examples]]
+
[[Category:Qt]][[Category:UI]][[Category:Code Snippet]]
{{ReviewerApproved|user=[[User:Hamishwillee|<br />----]]|timestamp=20110915233119 }}
+
 
{{ArticleMetaData
+
{{ArticleMetaData <!-- v1.1 -->
|platform=S60 3rd Edition FP1, S60 3rd Edition FP2, S60 5th Edition, Symbian^3,  Qt 4.6, Qt 4.7
+
|devices= Nokia C5-00S60, Emulator, Simulator
+
|creationdate=10 April 2009
+
|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]]) -->
|sdk=[http://www.developer.nokia.com/info/sw.nokia.com/id/da8df288-e615-443d-be5c-00c8a72435f8/Qt_SDK.html Qt SDK 1.1.3]
+
|devices= Nokia C5-00S60, Emulator, Simulator
|devicecompatability=All which support Qt
+
|sdk= [http://www.developer.nokia.com/info/sw.nokia.com/id/da8df288-e615-443d-be5c-00c8a72435f8/Qt_SDK.html Qt SDK 1.1.3]
|signing=<!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
+
|platform= S60 3rd Edition FP1, S60 3rd Edition FP2, S60 5th Edition, Symbian^3,  Qt 4.6, Qt 4.7
|capabilities=<!-- Capabilities required (e.g. Location, NetworkServices.) -->
+
|devicecompatability= All which support Qt
|author=[[User:Mind freak]]
+
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
|review-by=[[User:NegInfinity]]
+
|signing= <!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
|review-timestamp=20110915
+
|capabilities= <!-- Capabilities required (e.g. Location, NetworkServices.) -->
 +
|keywords= QCryptographicHash
 +
|id= <!-- Article Id (Knowledge base articles only) -->
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= [[User:NegInfinity]]
 +
|review-timestamp= 20110915
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20090410
 +
|author= [[User:Mind freak]]
 
}}
 
}}
  

Revision as of 07:56, 9 February 2012


Article Metadata
Tested with
Devices(s): Nokia C5-00S60, Emulator, Simulator
Compatibility
Platform(s): S60 3rd Edition FP1, S60 3rd Edition FP2, S60 5th Edition, Symbian^3, Qt 4.6, Qt 4.7
Symbian
Device(s): All which support Qt
Article
Keywords: QCryptographicHash
Created: mind_freak (10 Apr 2009)
Reviewed: NegInfinity (15 Sep 2011)
Last edited: hamishwillee (09 Feb 2012)

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>
 
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

349 page views in the last 30 days.
×