package com.tax.token.utils;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * TODO
 *
 * @author admin
 * @date 2020/1/7 16:07
 */
public class MakeSignatureUtil {

    static final char hexDigits[] = {
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            'A', 'B', 'C', 'D', 'E', 'F'
    };


    /**
     * @param @return
     * @return String
     * @throws @author admin
     * @Description: 利用当前时间生成时间戳
     * @date 2018年4月20日
     */
    public static String makeTimeStamp() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");// 设置日期格式
        String timeStamp = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
        return timeStamp;
    }

    public static String makeTimeStampLong() {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");// 设置日期格式
        String timeStamp = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
        return timeStamp;
    }


    public static void main(String[] args) {
        String s = makeTimeStampLong();
        System.out.println(s);
    }
    /**
     * @param @return
     * @return String
     * @throws @author admin
     * @Description: 使用UUID生成随机数
     * @date 2018年4月20日
     */
    public static String makeSignatureNonce() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    /**
     * 获取签名串
     * @param data
     * @param key
     * @return
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     */
    public static String getSignature(byte[] data, byte[] key)
            throws NoSuchAlgorithmException, InvalidKeyException {
        SecretKeySpec signingKey = new SecretKeySpec(key, "HmacSHA1");
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(signingKey);
        byte[] rawHmac = mac.doFinal(data);
        return getEncode(rawHmac);
    }

    /**
     * 获取MD5码
     * @param key
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static String getEncode(byte[] key) throws NoSuchAlgorithmException{
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.update(key);
        byte digest[] = messageDigest.digest();
        int j = digest.length;
        char str[] = new char[j * 2];
        int k = 0;
        for(int i = 0; i < j; i++)
        {
            byte byte0 = digest[i];
            str[k++] = hexDigits[byte0 >>> 4 & 0xf];
            str[k++] = hexDigits[byte0 & 0xf];
        }

        return new String(str);
    }
	
    /**
     * 生成验签
     * @param AccessKeyID
     * @param SignatureNonce
     * @param TimeStamp
     * @param Version
     * @param passWord
     * @return
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     * @throws UnsupportedEncodingException
     */
    public static String makeSignature(String AccessKeyID, String SignatureNonce, String TimeStamp,
                                String Version, String passWord) {
        String signature = "";
        try {
            String singnatureData = "AccessKeyID=" + AccessKeyID;
            singnatureData += "&SignatureNonce=" + SignatureNonce;
            singnatureData += "&TimeStamp=" + TimeStamp;
            singnatureData += "&Version=" + Version;
            signature = getSignature(singnatureData.getBytes("UTF-8"),
                    passWord.getBytes("UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return signature;
    }


}
