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
Contributor
Gaspertrix
Posts: 29
Registered: ‎09-28-2013
My Carrier: Movistar
Accepted Solution

Hash MAC SHA256

I want to generate a keyed hash value using the HMAC method and SHA256 hashing algorithm using the Security Builder API but I can not get the expected result and could not find any example in documentaion or forum on how to do this.

Please use plain text.
Developer
BBSJdev
Posts: 4,945
Registered: ‎07-05-2012
My Carrier: Orange

Re: Hash MAC SHA256

Have you looked at this example...

 

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


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

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.
Contributor
Gaspertrix
Posts: 29
Registered: ‎09-28-2013
My Carrier: Movistar

Re: Hash MAC SHA256

Yes, but it is not using MAC hashing, only SHA256 and parameters and functions are different.

Please use plain text.
Developer
BBSJdev
Posts: 4,945
Registered: ‎07-05-2012
My Carrier: Orange

Re: Hash MAC SHA256

I think you are going to be hard pressed to find an exact example, why not post your code and tell us why it's not working then someone here may be able to help.


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

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.
Contributor
Gaspertrix
Posts: 29
Registered: ‎09-28-2013
My Carrier: Movistar

Re: Hash MAC SHA256

It is not working because the result is not the expected but always the same.

Sure the way I am doing it is wrong.

 

I am doing this:

[CODE]

sb_Context hmacContext;

sb_GlobalCtx globalCtx;

QString key = "xO2WZ578j0fO3G5";

QString data = "This is a message";

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*) key.toUtf8().constData(), NULL, &hmacContext, globalCtx);

hu_HMACSHA256Hash(hmacContext, data.length(), (unsigned char*) data.toUtf8().constData(), globalCtx);

 

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

QString password_hash_as_hex = QString::fromAscii(password_hash_data.toHex());

 

hu_HMACSHA256End(&hmacContext, SB_HMAC_SHA256_256_TAG_LEN, message_digest_user_id, globalCtx);

hu_GlobalCtxDestroy(&globalCtx);

[/CODE]

 

The keyed hash for the above example must be:

3a6896ba0e967236a8788a5283c39780deedbb12c4e54f7023f73231ebdc4d5d

Please use plain text.
BlackBerry Development Advisor
PBernhardt
Posts: 698
Registered: ‎12-16-2008
My Carrier: Bell

Re: Hash MAC SHA256

It's the call to hu_HMACSHA256End that actually generates the hash for you. All you need to do is put that line above where you define QByteArray password_hash_data.

 

 

 

Paul Bernhardt
Application Development Consultant
BlackBerry
@PBernhardt

Did this answer your question? Please accept this post as the solution.
Found a bug? Report it to the Developer Issue Tracker
Please use plain text.
Developer
Svat_
Posts: 85
Registered: ‎02-13-2009
My Carrier: MTS

Re: Hash MAC SHA256

Did you found solution?

I tried this code and it retuns alway different results :smileysad:

 

sb_GlobalCtx globalCtx;
sb_Context hmacContext;

QString key = "xO2WZ578j0fO3G5";
QString data = "This is a message";

 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*) key.toUtf8().constData(), NULL, &hmacContext, globalCtx);
hu_HMACSHA256Hash(hmacContext, data.length(), (unsigned char*) data.toUtf8().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);
QString password_hash_as_hex = QString::fromAscii(password_hash_data.toHex());

hu_GlobalCtxDestroy(&globalCtx);

 

I tried to use hu_RegisterSbg56HMACSHA256 instead of hu_RegisterSbg56. But no success too..

Is there any example how to do HMAC-SHA-256 right way?

Please use plain text.
BlackBerry Development Advisor (Retired)
robbieDubya
Posts: 418
Registered: ‎07-18-2012
My Carrier: Bell Canada

Re: Hash MAC SHA256

[ Edited ]

Hi,

 

You're exagerating your key size...

 

QString key = "xO2WZ578j0fO3G5";

key.length() == 15

 

Then you tell hu_HMACSHA256Begin that the length of your key is 32!

SB_HMAC_SHA256_256_TAG_LEN == 32

 

ie: 16 bytes of random heap/stack data are the last half of your key...

 

 

--
Rob is no longer associated with BlackBerry.
Please use plain text.
Developer
Svat_
Posts: 85
Registered: ‎02-13-2009
My Carrier: MTS

Re: Hash MAC SHA256

You are right! It works fine now!

Thanks a lot! 

Please use plain text.
BlackBerry Development Advisor (Retired)
robbieDubya
Posts: 418
Registered: ‎07-18-2012
My Carrier: Bell Canada

Re: Hash MAC SHA256

Great!
--
Rob is no longer associated with BlackBerry.
Please use plain text.