Java HMAC hmacSHA256(byte[] secret, byte[] data)

Here you can find the source of hmacSHA256(byte[] secret, byte[] data)

Description

hmac SHA

License

Apache License

Declaration

public static byte[] hmacSHA256(byte[] secret, byte[] data)
            throws NoSuchAlgorithmException, InvalidKeyException 

Method Source Code

//package com.java2s;
/*//from   ww w .  j  a v  a  2  s  .  c o m
 * Copyright (c) 2012-2017 ZoxWeb.com LLC.
 *
 * 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.
 */

import java.security.*;

import javax.crypto.*;

import javax.crypto.spec.SecretKeySpec;

public class Main {
    public static final String HMAC_SHA_256 = "HmacSHA256";

    public static byte[] hmacSHA256(byte[] secret, byte[] data)
            throws NoSuchAlgorithmException, InvalidKeyException {
        Mac sha256HMAC = Mac.getInstance(HMAC_SHA_256);
        SecretKeySpec secret_key = new SecretKeySpec(secret, HMAC_SHA_256);
        sha256HMAC.init(secret_key);
        return sha256HMAC.doFinal(data);
    }
}

Related

  1. hmacSha1(SecretKey key, byte[] data)
  2. hmacSha1(SecretKeySpec signingKey, byte[]... data)
  3. hmacSha1(String data, String key)
  4. hmacSha1(String input, byte[] keyBytes)
  5. hmacSha1(String input, byte[] keyBytes)
  6. hmacSha256(String keyHex, String stringData)
  7. hmacsha256Representation(String data, String pusherApplicationSecret)
  8. hmacSha512(byte[] key, byte[] message)
  9. hmacSign(String aValue, String aKey)