Java HMAC hmacSha1(String data, String key)

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

Description

hmac Sha

License

Apache License

Declaration

public static String hmacSha1(String data, String key) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;

import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class Main {
    public static String hmacSha1(String data, String key) {
        SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "HmacSHA1");
        Mac mac = null;//w w w .j a  v  a 2  s .  com
        try {
            mac = Mac.getInstance("HmacSHA1");
            mac.init(signingKey);
        } catch (NoSuchAlgorithmException | InvalidKeyException e) {
            throw new RuntimeException(e);
        }
        byte[] rawHmac = mac.doFinal(data.getBytes(StandardCharsets.UTF_8));
        return Base64.getEncoder().encodeToString(rawHmac);
    }
}

Related

  1. hmacSha1(byte[] data, byte[] key)
  2. hmacSha1(byte[] key_bytes, byte[] text_bytes)
  3. hmacSha1(byte[] secret, byte[] data)
  4. hmacSha1(SecretKey key, byte[] data)
  5. hmacSha1(SecretKeySpec signingKey, byte[]... data)
  6. hmacSha1(String input, byte[] keyBytes)
  7. hmacSha1(String input, byte[] keyBytes)
  8. hmacSHA256(byte[] secret, byte[] data)
  9. hmacSha256(String keyHex, String stringData)