Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Developer
Posts: 60
Registered: ‎04-14-2014
My Device: z10
My Carrier: Digitel
Accepted Solution

SHA256 hash file, to upload to the cloud server. Im lose...

Again bothering around here , this time because I have a problem in cryptography (which I've never really worked ).

 

Well , I need to create a SHA256 hash of any file I want to upload to a web server in the cloud , why? Because one of the parameters of the header that requires me : x- filehash - A SHA256 hash of the file.

 

I searched google on how to do this and i found two alternatives to perform this task , the first is the native ( using the library husha2.h ) and the second is a library called polarssl.

 

When I review polarssl I noticed that is practically equal to the native form, so I finish selecting husha2.h.

 

The problem is that I do not know much about the 3 or 4 functions to create a SHA256 hash and less for an entire file . So I wanted to ask if some kind soul from over here , has a small example of using this library , but I at least explain a bit what each one of the parameters and what would be the order in which you must use the functions.

 

Thanks and sorry for the inconvenience.

My Answer is good? Nice! Please, Clicked the "Thanks" button, this is very helpful for me! Smiley Happy
Mi respuesta es buena? Que bien! Por favor, haz clic en el botón "Gracias", esto es muy útil para mí! Smiley Happy
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: SHA256 hash file, to upload to the cloud server. Im lose...

There's not much on encryption but take a look at this thread...

 

http://supportforums.blackberry.com/t5/Native-Development/AES-encryption-decryption-is-not-working-p...

 

and this sample...

 

https://github.com/blackberry/Cascades-Community-Samples/tree/master/AESCryptoDemo


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Highlighted
Retired
Posts: 170
Registered: ‎09-06-2010
My Device: None
My Carrier: None

Re: SHA256 hash file, to upload to the cloud server. Im lose...

You might also glance at the PasswordSample sample application in GitHub here: 

 

https://github.com/blackberry/Cascades-Community-Samples/tree/master/PasswordSample

 

in particular the class called SecurityManager:

 

https://github.com/blackberry/Cascades-Community-Samples/blob/master/PasswordSample/src/SecurityMana...

 

It demonstrates how to create an SHA256 hash, albeit from a small amount of data (a password) in this case rather than a file. Maybe a good starting point as well since it shows how to use the libraries.

Developer
Developer
Posts: 60
Registered: ‎04-14-2014
My Device: z10
My Carrier: Digitel

Re: SHA256 hash file, to upload to the cloud server. Im lose...

Ok i have this:

 

QByteArray fireball::firmaSHA2(QString fn, QByteArray archivoBinario) {
sb_Context hmacContext;
sb_GlobalCtx globalCtx;
unsigned char message_digest_user_id[SB_HMAC_SHA256_256_TAG_LEN];

hu_GlobalCtxCreateDefault(&globalCtx);
hu_RegisterSbg56(globalCtx);
hu_RegisterSystemSeed(globalCtx);
hu_InitSbg56(globalCtx);

hu_HMACSHA256Begin(SB_HMAC_SHA256_256_TAG_LEN, (unsigned char*) fn.toUtf8().constData(), NULL, &hmacContext, globalCtx);
hu_HMACSHA256Hash(hmacContext, archivoBinario.length(), (unsigned char*) archivoBinario.constData(), globalCtx);
hu_HMACSHA256End(&hmacContext, SB_HMAC_SHA256_256_TAG_LEN, message_digest_user_id, globalCtx);

QByteArray password_hash_data = QByteArray::fromRawData(reinterpret_cast<const char *>(message_digest_user_id), SB_HMAC_SHA256_256_TAG_LEN);

hu_GlobalCtxDestroy(&globalCtx);

return password_hash_data;
}

 

But on the first call to this function generates a code and the second call to this function generates another code other than (obviously with the same parameters, it should be noted that the third and fourth function call to generate the same code that second call.)

 

How could this happen?

My Answer is good? Nice! Please, Clicked the "Thanks" button, this is very helpful for me! Smiley Happy
Mi respuesta es buena? Que bien! Por favor, haz clic en el botón "Gracias", esto es muy útil para mí! Smiley Happy
Retired
Posts: 170
Registered: ‎09-06-2010
My Device: None
My Carrier: None

Re: SHA256 hash file, to upload to the cloud server. Im lose...

I tried the same sequence of calls as you ( I hard coded the HMAC key and input data ) and I got consistent results every time. Here's my code fragment ( I called onInitSecurity() once, onMakeHash() many times, and onEndSecurity() once) -- I use emit message() to send to QML on my screen. Have you checked the return codes from each call? I get SB_SUCCESS on each one. Variables beginning _... are class instance ones.

 

 

void ApplicationUI::onInitSecurity() {

    emit message("XXXX onInitSecurity");

    int returnCode = SB_SUCCESS; /* Return Code */

    /* Create SB Contexts */

    returnCode = hu_GlobalCtxCreateDefault(&_sbCtx);

    if (returnCode != SB_SUCCESS) {
        emit message("XXXX makeHash ERROR creating SB contexts:" + Utilities::intToHex(returnCode));
        return;
    } else {
        emit message("XXXX makeHash SUCCESS creating SB contexts");
    }

    returnCode = hu_RegisterSbg56(_sbCtx);
    if (returnCode != SB_SUCCESS) {
        emit message("XXXX makeHash ERROR calling hu_RegisterSbg56:" + Utilities::intToHex(returnCode));
        return;
    } else {
        emit message("XXXX makeHash SUCCESS calling hu_RegisterSbg56");
    }

    returnCode = hu_InitSbg56(_sbCtx);
    if (returnCode != SB_SUCCESS) {
        emit message("XXXX makeHash ERROR calling hu_InitSbg56:" + Utilities::intToHex(returnCode));
        return;
    } else {
        emit message("XXXX makeHash SUCCESS calling hu_InitSbg56");
    }
}

//...

void ApplicationUI::onMakeHash() {

    bool doHmac = true;

    QString input_data("1234567890");
    unsigned char messageDigest[SB_SHA256_DIGEST_LEN];
    unsigned char messageDigestHMAC[SB_HMAC_SHA256_256_TAG_LEN];
    unsigned char hmacKey[SB_HMAC_SHA256_256_TAG_LEN] = {0x1C, 0x85, 0xC9, 0xBB, 0x25, 0xBA, 0xFB, 0xE9,
                                                         0xFC, 0xE7, 0xB1, 0x3B, 0x9B, 0x53, 0x6F, 0x40,
                                                         0x79, 0x4F, 0x13, 0x3B, 0x60, 0x96, 0xF7, 0x44,
                                                         0x0B, 0x62, 0x39, 0xFB, 0xCA, 0x4A, 0xA3, 0xB6
                                                        };

    emit message("XXXX onMakeHash:" + input_data + " (" + QString::number(input_data.length()) + ")");

    /* Initialize variables */

    int returnCode = SB_SUCCESS; /* Return Code */

    QByteArray input_bytes = input_data.toUtf8();
    unsigned char* hash_input = reinterpret_cast<unsigned char*>(input_bytes.data());

    if (doHmac) {
        returnCode = hu_HMACSHA256Begin(SB_HMAC_SHA256_256_TAG_LEN, hmacKey, NULL, &_hmacContext, _sbCtx);
        if (returnCode != SB_SUCCESS) {
            emit message("XXXX makeHash ERROR initialising HMAC-SHA-256 context:" + Utilities::intToHex(returnCode));
            return;
        } else {
            emit message("XXXX makeHash SUCCESS initialising HMAC-SHA-256 context");
        }

        /* Hash Message */

        returnCode = hu_HMACSHA256Hash(_hmacContext, (size_t) input_bytes.length(), hash_input, _sbCtx);
        if (returnCode != SB_SUCCESS) {
            emit message("XXXX makeHash ERROR creating HMAC:" + Utilities::intToHex(returnCode));
            return;
        } else {
            emit message("XXXX makeHash SUCCESS creating HMAC");
        }

        /* Complete HMAC-SHA-256 Hashing */

        returnCode = hu_HMACSHA256End(&_hmacContext, SB_HMAC_SHA256_256_TAG_LEN, messageDigestHMAC, _sbCtx);
        if (returnCode != SB_SUCCESS) {
            emit message("XXXX makeHash ERROR completing HMAC:" + Utilities::intToHex(returnCode));
            return;
        } else {
            QString digest;
            for (int i = 0; i < SB_HMAC_SHA256_256_TAG_LEN; ++i) {
                digest.append(QString("%1").arg(QString::number((uint)messageDigestHMAC[i], 16), 2, QChar('0')));
            }
            emit message("XXXX makeHash SUCCESS completing HMAC");
            emit message("XXXX HMAC = " + digest);
        }
    } else {
//...
}

//...

void ApplicationUI::onEndSecurity() {
    emit message("XXXX onEndSecurity");
    hu_GlobalCtxDestroy(&_sbCtx);
}

 

Developer
Developer
Posts: 60
Registered: ‎04-14-2014
My Device: z10
My Carrier: Digitel

Re: SHA256 hash file, to upload to the cloud server. Im lose...

I has not happened, but now I get the result is correct (compared to online HMAC-SHA2 generators). Thanks for your support! Smiley Happy

My Answer is good? Nice! Please, Clicked the "Thanks" button, this is very helpful for me! Smiley Happy
Mi respuesta es buena? Que bien! Por favor, haz clic en el botón "Gracias", esto es muy útil para mí! Smiley Happy