Menu

Android Example

saeed atalla

Android Example:

This example will explain how to use CryptoEngine to write a secure app:

First you need to install these tools (if this is your first Android app using c++):

1- Android SDK (adt-bundle).
2- Android NDK (r10c).
3- Qt5-SDK for Android (I use Qt 5.3.2 ).

This simple example is an app that generete Hash string for any given text using SHA256 algorithm, and here is .pro file for project (named: TestAndroid2):

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = TestAndroid2
TEMPLATE = app

SOURCES += main.cpp\
        widget.cpp

HEADERS  += widget.h

FORMS    += widget.ui

CONFIG += mobility
MOBILITY = 

contains(ANDROID_TARGET_ARCH, armeabi-v7a) {
LIBS += -L$$PWD/../../MyProjects/QtCryptoEngine/CryptoEngine/Linux_out/android_armeabi_v7a/ -lCryptoEngine

INCLUDEPATH += $$PWD/../../MyProjects/QtCryptoEngine/CryptoEngine/INCLUDE/CELIB
DEPENDPATH += $$PWD/../../MyProjects/QtCryptoEngine/CryptoEngine/INCLUDE/CELIB

    ANDROID_EXTRA_LIBS = \
        $$PWD/../../MyProjects/QtCryptoEngine/CryptoEngine/Linux_out/android_armeabi_v7a/libCryptoEngine.so
}

ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android

OTHER_FILES += \
    android/AndroidManifest.xml

contains(ANDROID_TARGET_ARCH, x86) {
LIBS += -L$$PWD/../../MyProjects/QtCryptoEngine/CryptoEngine/Linux_out/android_x86/ -lCryptoEngine

INCLUDEPATH += $$PWD/../../MyProjects/QtCryptoEngine/CryptoEngine/INCLUDE/CELIB
DEPENDPATH += $$PWD/../../MyProjects/QtCryptoEngine/CryptoEngine/INCLUDE/CELIB

    ANDROID_EXTRA_LIBS = \
        $$PWD/../../MyProjects/QtCryptoEngine/CryptoEngine/Linux_out/android_x86/libCryptoEngine.so
}

The important thing here is that we add CryptoEngine Library (in specific path) as depenency in both: android x86 for Intel Atom architecture, and for armeabi v7a for ARM architecutre which is used now for most smart phones such as Galaxy S4 and Nexus 7; using function contains() - from qmake lang. - then the app will link automatically with the right verssion of library.
You can add only one vesrsion as you need, also you need to add libCryptoEngine.so as extra lib, we do this to ensure that it shipped with app when deploying.


Now we can design a form (QWidget) that contains 4 elements:
- Text field (QLineEdit) that used to enter plain text.
- Button (QPushButton) which user will click to generate hash.
- Text browser (QTextBrowser) that will use to display hash string.
- Another button for exit.

Form Design

Here is widget.cpp:

#include "sha2.h"
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_btnGHash_clicked()
{
    const QByteArray plain = ui->le_text->text().toUtf8();
    QByteArray hString;
    bool isOk;
    if(!plain.isEmpty())
    {
        hString = CELIB::SHA256::hash(plain, &isOk, 0);
        if(isOk)
            ui->textBrowser->setText(hString.toHex().toUpper());
        else
            ui->textBrowser->setText("Operation Failed!.");
    }
    else
        ui->textBrowser->clear();
}

void Widget::on_btnExit_clicked()
{
    exit(0);
}

In this file we include: sha2.h from CryptoEngine library, and use it in function (slot): on_btnGHash_clicked() to generate hash string, using static function: CELIB::SHA256::hash(plain, &isOk, 0) and print out the result on textBrowser using it's function: setText(...).

If user clicked in btnGHash button then on_btnGHash_clicked() function will execute and generate hash string for given text, and if user clicked on btnExit button then on_btnExit_clicked() function will execute and the program will exit.

Finaly, this is main.cpp file that creates widget object and show it:

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

Before you deploy your app you need to go to Projects tab (on left in QtCreator) -> run -> deploy configuration; there you can define your Android SDK version to build with it, and generate Certificate to sign you app and you can add Manafist file and other setting.

And this is output when testing on android virual device (armeabi-v7a) :

Output

Note that the result hash string for given text: "abc" is exactly as NIST mentioned in it's Known Answer Test vector for algorithm SHA256.

Download source code from here:
Download Android Example


Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.