Android HmacSHA256 Create getHmacSha256Base64(String key, String data)

Here you can find the source of getHmacSha256Base64(String key, String data)

Description

get Hmac Sha Base

Declaration

public static String getHmacSha256Base64(String key, String data)
            throws NoSuchAlgorithmException, InvalidKeyException 

Method Source Code

//package com.java2s;

import android.util.Base64;

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

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class Main {
    public static String getHmacSha256Base64(String key, String data)
            throws NoSuchAlgorithmException, InvalidKeyException {
        final String encryptionAlgorithm = "HmacSHA256";
        SecretKey secretKey = new SecretKeySpec(data.getBytes(),
                encryptionAlgorithm);//from ww  w .  ja v a2  s. c o m
        Mac messageAuthenticationCode = Mac
                .getInstance(encryptionAlgorithm);
        messageAuthenticationCode.init(secretKey);
        messageAuthenticationCode.update(key.getBytes());
        byte[] digest = messageAuthenticationCode.doFinal();
        return Base64.encodeToString(digest, Base64.NO_WRAP);
    }
}

Related

  1. encryptHMAC(String data, String secret)
  2. hmacSha1(String str, String keyString)