generate Hmac Sha - Android java.security

Android examples for java.security:HMAC

Description

generate Hmac Sha

Demo Code

/*/*from  w w w.j  ava  2 s.c  o m*/
 * Copyright (c) 2015 Magnet Systems, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//package com.java2s;
import android.util.Base64;

import java.security.SignatureException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class Main {
    private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";

    public static String generateHmacSha1(String data, String key)
            throws SignatureException {
        String result;
        try {

            SecretKeySpec secretKeySpecKey = new SecretKeySpec(
                    key.getBytes(), HMAC_SHA1_ALGORITHM);

            Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
            mac.init(secretKeySpecKey);

            // compute the hmac on data
            byte[] bHmac = mac.doFinal(data.getBytes());

            // stringify it
            result = Base64.encodeToString(bHmac, Base64.DEFAULT);

        } catch (Exception e) {
            throw new SignatureException("Failed to generate HMAC-SHA1 : "
                    + e.getMessage());
        }
        return result;

    }
}

Related Tutorials