get Hmac SHA Hash - Java Security

Java examples for Security:SHA

Description

get Hmac SHA Hash

Demo Code


//package com.java2s;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;

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

public class Main {


    public static byte[] getHmacSHA1(String inStr, String appSecet)
            throws NoSuchAlgorithmException, UnsupportedEncodingException,
            InvalidKeyException {
        Mac mac = null;// w  w  w .j a va 2s . com
        try {
            mac = Mac.getInstance("HmacSHA1");
            SecretKeySpec secret = new SecretKeySpec(
                    appSecet.getBytes("UTF-8"), mac.getAlgorithm());
            mac.init(secret);
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
            return null;
        }

        return mac.doFinal(inStr.getBytes());
    }
}

Related Tutorials