Java HMAC hmacSign(String skey, String data)

Here you can find the source of hmacSign(String skey, String data)

Description

hmac Sign

License

Open Source License

Declaration

public static String hmacSign(String skey, String data) throws NoSuchAlgorithmException, InvalidKeyException 

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.spec.SecretKeySpec;

public class Main {
    public static String hmacSign(String skey, String data) throws NoSuchAlgorithmException, InvalidKeyException {
        SecretKeySpec key = new SecretKeySpec(skey.getBytes(), "HmacSHA1");
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(key);//from w  w w .j a va2  s .co m
        byte[] raw = mac.doFinal(data.getBytes());
        return bytesToHex(raw);
    }

    public static String bytesToHex(byte[] b) {
        String result = "";
        for (int i = 0; i < b.length; i++) {
            result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
        }
        return result;
    }
}

Related

  1. hmacSHA256(byte[] secret, byte[] data)
  2. hmacSha256(String keyHex, String stringData)
  3. hmacsha256Representation(String data, String pusherApplicationSecret)
  4. hmacSha512(byte[] key, byte[] message)
  5. hmacSign(String aValue, String aKey)