Batista R. Harahap

Android - Create SHA1 Hash

I've been toying with Urbanesia's OAuth provider API calls in Android these few hours regretting why it is so complicated in Java compared to PHP hehehe. Since OAuth's requirement to generate a signature is using HMAC SHA1 to sign the whole POST & GET requests, I had to find a way to do it in Android's Java.

So I scour Google to look for answers but sadly the results is surprisingly scarce. I wanted to make a standardize OAuth client for Urbanesia's API but the standard libraries provided by the Android SDK is not what I've expected. For this to work, will need to download Apache Commons Codec Library here. So without further ado, here are the codes.

    private String sha1(String s, String keyString) throws 
        UnsupportedEncodingException, NoSuchAlgorithmException, 
            InvalidKeyException {

        SecretKeySpec key = new SecretKeySpec((keyString).getBytes("UTF-8"), "HmacSHA1");
        Mac mac = Mac.getInstance("HmacSHA1");

        byte[] bytes = mac.doFinal(s.getBytes("UTF-8"));

        return new String( Base64.encodeBase64(bytes) );
24 December 2010 by Batista Harahap on Android | api | hash hmac sha1 | hmac sha1 | oauth | sha1 | urbanesia
comments powered by Disqus