Java HMAC hmacSha1(SecretKeySpec signingKey, byte[]... data)

Here you can find the source of hmacSha1(SecretKeySpec signingKey, byte[]... data)

Description

hmac Sha

License

Open Source License

Declaration

public static byte[] hmacSha1(SecretKeySpec signingKey, byte[]... data) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.security.InvalidKeyException;

import java.security.NoSuchAlgorithmException;

import javax.crypto.Mac;

import javax.crypto.SecretKey;

import javax.crypto.spec.SecretKeySpec;

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

    public static byte[] hmacSha1(SecretKeySpec signingKey, byte[]... data) {
        Mac mac = buildHmacSha1(signingKey);

        return hmacSha1(mac, data);
    }/*from  w ww .  j av a  2 s .  c  o m*/

    public static byte[] hmacSha1(Mac mac, byte[]... data) {
        for (int i = 0; i < data.length - 1; i++) {
            mac.update(data[i]);
        }

        byte[] hash = mac.doFinal(data[data.length - 1]);
        return hash;
    }

    public static byte[] hmacSha1(SecretKeySpec signingKey, byte[] buffer, int offset, int length) {
        Mac mac = buildHmacSha1(signingKey);

        mac.update(buffer, offset, length);
        byte[] hash = mac.doFinal();
        return hash;
    }

    public static Mac buildHmacSha1(SecretKey key) {
        Mac mac = getHmacSha1();
        try {
            mac.init(key);
        } catch (InvalidKeyException e) {
            throw new IllegalStateException("Unexpected encryption error", e);
        }
        return mac;
    }

    private static Mac getHmacSha1() {
        return getMac(HMAC_SHA1_ALGORITHM);
    }

    private static Mac getMac(String algorithm) {
        try {
            Mac digest = Mac.getInstance(algorithm);
            return digest;
        } catch (NoSuchAlgorithmException e) {
            // should not happen
            throw new IllegalStateException("Could not find Mac algorithm: " + algorithm, e);
        }
    }
}

Related

  1. hmacSha(String crypto, String key, String value)
  2. hmacSha1(byte[] data, byte[] key)
  3. hmacSha1(byte[] key_bytes, byte[] text_bytes)
  4. hmacSha1(byte[] secret, byte[] data)
  5. hmacSha1(SecretKey key, byte[] data)
  6. hmacSha1(String data, String key)
  7. hmacSha1(String input, byte[] keyBytes)
  8. hmacSha1(String input, byte[] keyBytes)
  9. hmacSHA256(byte[] secret, byte[] data)