How to calculate hash for a text using QCryptographicHash in Qt
This code snippet shows how to calculate hash for a text string using QCryptographicHash.
Article Metadata
Code Example
Source file: Media:MD4HashSample.zip
Tested with
SDK: Qt SDK 1.1.3
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
Device(s): All which support Qt
Article
Keywords: QCryptographicHash
Created: mind_freak
(10 Apr 2009)
Updated: vineet.jain
(24 Feb 2012)
Reviewed: NegInfinity
(15 Sep 2011)
Last edited: hamishwillee
(11 Oct 2012)
Contents |
Source Code
Main.cpp
#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
#include <QCryptographicHash>
#include "QDeclarativeContext"
#include "hashcalculate.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
HashCalculate*ihashcalc = new HashCalculate();
QmlApplicationViewer viewer;
viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
QDeclarativeContext *ctxt = viewer.rootContext();
ctxt->setContextProperty("myContextProperty",ihashcalc);
viewer.setMainQmlFile(QLatin1String("qml/MD4HashSample/main.qml"));
viewer.showExpanded();
return app.exec();
}
hashcalculate.cpp
#include "hashcalculate.h"
#include <QCryptographicHash>
#include "QDebug"
HashCalculate::HashCalculate(QObject *parent) :
QObject(parent)
{
iHashValue = "";
}
void HashCalculate::calculateHash(const QString& aOriginalText )
{
QCryptographicHash hash(QCryptographicHash::Md4);
hash.addData(aOriginalText.toUtf8());
SetHash(QString(hash.result().toHex()));
//qDebug() << QString(hash.result().toHex());
}
void HashCalculate::SetHash(const QString& aHashValue)
{
iHashValue = aHashValue;
}
QString HashCalculate::getHash()
{
return iHashValue;
}
hashcalculate.h
#ifndef HASHCALCULATE_H
#define HASHCALCULATE_H
#include <QObject>
class HashCalculate : public QObject
{
Q_OBJECT
public:
explicit HashCalculate(QObject *parent = 0);
Q_INVOKABLE void calculateHash(const QString& aOriginalText );
signals:
public slots:
QString getHash();
void SetHash(const QString& aHashValue);
public:
QString iHashValue;
};
#endif // HASHCALCULATE_H
Main.qml
import QtQuick 1.0
import com.nokia.symbian 1.0
Window {
id: window
StatusBar {
id: statusBar
anchors.top: window.top
}
PageStack {
id: pageStack
anchors { left: parent.left; right: parent.right; top: statusBar.bottom; bottom: toolBar.top }
}
ToolBar {
id: toolBar
anchors.bottom: window.bottom
tools: ToolBarLayout {
id: toolBarLayout
ToolButton {
flat: true
iconSource: "toolbar-back"
onClicked: pageStack.depth <= 1 ? Qt.quit() : pageStack.pop()
}
}
}
Component.onCompleted: {
pageStack.push(Qt.resolvedUrl("MainPage.qml"))
}
}
MainPage.qml
import QtQuick 1.0
import com.nokia.symbian 1.0
Page {
id: mainPage
Text {
text: qsTr("Enter Text:")
id:textorg
color: platformStyle.colorNormalLight
font.pixelSize: 20
x:text_originaltxt.x
y:text_originaltxt.y-45
}
Text {
text: qsTr("MD4 Hash :")
id: txthshd
color: platformStyle.colorNormalLight
font.pixelSize: 20
x:text_hashedtxt.x
y:text_hashedtxt.y-40
}
TextField {
id: text_originaltxt
x: parent.x+5
y:parent.y+55
width: parent.width-15
text : "Nokia"
}
TextField {
id: text_hashedtxt
x: text_originaltxt.x
y:text_originaltxt.y+110
width: parent.width-15
readOnly: true
text : ""
}
Button {
id: btnhash
x:parent.x+50
y:text_hashedtxt.y+text_hashedtxt.height+txthshd.height+5
text: "Get Hash"
onClicked: {
myContextProperty.calculateHash(text_originaltxt.text)
text_hashedtxt.text = myContextProperty.getHash()
}
}
Button {
id: btnclr
x:parent.x+btnhash.width+130
y:text_hashedtxt.y+text_hashedtxt.height+txthshd.height+5
text: "Clear"
onClicked: {
text_originaltxt.text = ""
text_hashedtxt.text = ""
}
}
}
ScreenShots
About the Update
The implementation has now been converted using the Qt Quick components.As compared with the previous functionality in which a hard-coded text was getting converted into a hash, now the user has flexibility to enter any text & convert it into a hash code as visible in the screenshots.
Source Code
Full source code can be downloaded from here : Media:MD4HashSample.zip





16 Sep
2009
Encryption is the process of transforming information to an unreadable data to anyone except those possesing the Key. The encrytion is done using an algoritm and the study of information which is hidden/ encrypted is called Cryptography. The Key is used to decrypt the encrypted data. Encryption is broadly used to transfer data privately.
The article presents the way to encrypt data using QCryptoGraphicHash class in Qt for Symbian. The article contains an application to encrypt a predefined string in to encrypted data. The code snippet and sceen-shot are provided in this article.
Encryption is very important aspect in transfering data securely and this way the article has its importance. It can be useful to beginners to study encryption.
28 Sep
2009
Encryption is the most effective way to achieve data security. To read an encrypted file, you must have access to a secret key or password that enables you to decrypt it. Sending a plain text, Unencrypted data, over internet Or saving plain text in a file (which is located in public directory) is risky, anyone can tap the channel and use data directly. Cipher text, encrypted data, is more secure compared to plain text.
The QCryptographicHash class provides a way to generate cryptographic hashes. Currently MD4, MD5, and SHA-1 hash are supported.
The article is useful to generates cryptographic hashes using QCryptographicHash class. A beginners find it useful to generate cryptographic hashes.
Hamishwillee - @NegInfinity - nice updates!
This looks like almost a complete re-write. Thanks very much for your community spirit. Writing an article in the first place is appreciated, but improving and updating one when it is out of date adds just as much value!
Regards
Hamish
(Community Services Manager, Nokia)hamishwillee 02:25, 16 September 2011 (EEST)