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

Java Development

Reply
Contributor
Posts: 14
Registered: ‎03-29-2013
My Device: 9800
My Carrier: Vodafone

Need alternative code to compute HMAC SHA1 in blackberry

[ Edited ]

Hello All, 
I am working on oauth in blackberry , I have a J2SE SDK ..in which HMAC -SHA1 signature are compunting 
like this.:

 

private String shaOne(String s, byte[] keyString) throws InvalidKeyException,UnsupportedEncodingException, NoSuchAlgorithmException
{

SecretKeySpec key = new SecretKeySpec(keyString,"HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(key);
byte[] bytes = mac.doFinal(s.getBytes("UTF-8"));
System.out.println("HMAC key value is ......................."+base64Encode(bytes));
return base64Encode(bytes);
}

 

the above code is working fine and i am getting Http.Ok 200 in response. when , I am porting the whole SDK in blackberry using JRE 6. And computing the HMAC-SHA1 like this:

private String shaOne(String s, byte[] keyString) throws CryptoTokenException, CryptoUnsupportedOperationException, IOException
{
HMACKey k = new HMACKey(keyString);
HMAC hmac1 = new HMAC(k, new SHA1Digest());
hmac1.update(s.getBytes());
byte[] mac1 = hmac1.getMAC();

return Base64OutputStream.encodeAsString(mac1, 0, mac1.length,false, false); 

}

 

But getting 403 in repponse. After tracing on server side, I am finding signtaure Mismatch.
So, Please tell me what is the problem with Blackberry HMAC-SHA 1 computaion.

Thanks, 
Sanya

 

 

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Need alternative code to compute HMAC SHA1 in blackberry

Have you compared the bytes generated by each method?

 

I wonder if the following is your problem?

hmac1.update(s.getBytes());

will not use UTF-8, to do this you need to use:

hmac1.update(s.getBytes("UTF-8"));

Contributor
Posts: 14
Registered: ‎03-29-2013
My Device: 9800
My Carrier: Vodafone

Re: Need alternative code to compute HMAC SHA1 in blackberry

First, Thanks for replying.
How to compare bytes.Cause random nonce value changes with
each request. so value will not be same. As, I think.May be i am 
wrong. 
and after including UTF-8 in hmac1.update(s.getBytes()); 
its still responding the same.
Smiley Sad

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Need alternative code to compute HMAC SHA1 in blackberry

"Cause random nonce value changes"

 

Don't see that in the code shown above.  The result is a String which is determined by the parameters input isn't it?  For the same input you should get the same output for both the BlackBerry code and the J2SE code shouldn't you?  And do you?