Android How to - Calculate HMAC








Question

We would like to know how to calculate HMAC.

Answer

The following code shows how to calculate HMAC.

It uses the Base64 util class to get the base 64 code value.

//from ww  w.  j a v a  2s.  c o m
import java.security.SignatureException;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;



import android.util.Base64;
import android.util.Log;

/**
 * 
 * @author Lewis Hancock
 * Contains Cryptography functions including HMAC_SHA1 encryption.
 *
 */
public class Main {

  private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";
  private static final String TAG = "Crypto";

  /**
   * 
   * @param secret
   * @param data
   * @return
   * @throws java.security.SignatureException
   */
  private String calculateHMAC(String secret, String data) throws java.security.SignatureException {
    try {
      SecretKeySpec signingKey = new SecretKeySpec(secret.getBytes(), HMAC_SHA1_ALGORITHM);
      Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
      mac.init(signingKey);
      
      // compute raw hmac on input data
      byte[] rawHmac = mac.doFinal(data.getBytes());
      String result = Base64.encodeToString(rawHmac, Base64.DEFAULT);
      return result;
    } catch (Exception e) {
      Log.e(TAG, "error: " + e.getMessage());
      throw new SignatureException("Failed to generate HMAC: " + e.getMessage());
    }
  }
  
}