package com.bootdo.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * ant 加密 工具类
 *
 * @author bc
 * @Date 2020-08-12 9:34
 */
public class SHAUtil {

    public static String sha1(String decript) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            digest.update(decript.getBytes());
            byte[] messageDigest = digest.digest();
            // Create Hex String
            StringBuffer hexString = new StringBuffer();
            // 字节数组转换为 十六进制 数
            for (int i = 0; i < messageDigest.length; i++) {
                String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
                if (shaHex.length() < 2) {
                    hexString.append(0);
                }
                hexString.append(shaHex);
            }
            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            System.out.println("字节数组转换异常" + e);
        }
        return "";
    }

    /**
     * 南京鸿信
     *
     * @return
     */
    public static Map<String, Object> getHeaders() {
        Map<String, Object> map = Collections.synchronizedMap(new HashMap<>(5));
        String decript = "";
        String appId = PropertiesUtil.queryPropertiesByKey("APPID");
        String appKey = PropertiesUtil.queryPropertiesByKey("APPKEY");
        String nonce = UUID.randomUUID().toString().replace("-", "");
        String curTime = String.valueOf(System.currentTimeMillis());
        decript = nonce + appKey + curTime;
        String checkNum = sha1(decript);
        System.out.println("appId:" + appId + ", appKey:" + appKey + ", nonce:" + nonce + ", curTime:" + curTime + ", checkNum:" + checkNum);
        map.put("appId", appId);
        map.put("appKey", appKey);
        map.put("nonce", nonce);
        map.put("curTime", curTime);
        map.put("checkNum", checkNum);
        return map;
    }



}
