SignatureGenerateUtil.java 5.35 KB
package com.genersoft.iot.vmp.vmanager.util;

import com.genersoft.iot.vmp.conf.exception.ServiceException;
import com.genersoft.iot.vmp.vmanager.jt1078.platform.domain.ApiParamReq;
import org.apache.commons.lang3.StringUtils;

import java.security.MessageDigest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 签名生成工具类
 *
 * @Author WangXin
 * @Data 2025/2/11
 * @Version 1.0.0
 */
public class SignatureGenerateUtil {

    /**
     * 临港调度接口签名生成工具
     * @param map 请求入参
     * Map<String, String> map = new HashMap<String, String>();
     * map.put("timestamp", "1490769820000");//时间戳
     * map.put("nonce", "a1503");//随机字符串
     * map.put("password", "a097286f0aeab6baf093bfea9afa2c29f6751212");//密码
     * @return 签名
     */
    public static String getSHA1(Map<String, String> map) throws Exception {
        try {
            String[] array = new String[map.size()];
            map.values().toArray(array);
            StringBuilder sb = new StringBuilder();
            // 字符串排序
            Arrays.sort(array);
            for (String s : array) {
                sb.append(s);
            }
            String str = sb.toString();
            // SHA1签名生成
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            md.update(str.getBytes());
            byte[] digest = md.digest();
            StringBuilder extra = new StringBuilder();
            String shaHex = "";
            for (byte b : digest) {
                shaHex = Integer.toHexString(b & 0xFF);
                if (shaHex.length() < 2) {
                    extra.append(0);
                }
                extra.append(shaHex);
            }
            return extra.toString();
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    public static  String getSHA1(String timestamp, String nonce, String password) throws Exception {
        HashMap<String, String> map = new HashMap<>();
        map.put("timestamp", timestamp);//时间戳
        map.put("nonce", nonce);//随机字符串
        map.put("password", password);
        return getSHA1(map);
    }

    /**
     * 获取SHA1加密字符串
     * @param timestamp 时间戳
     * @param nonce 随机字符串
     * @param password 密码
     * @param username 用户名
     * @return SHA1加密后的字符串
     * @throws Exception 加密过程中可能抛出的异常
     */
    public static  String getSHA1(String timestamp, String nonce, String password, String username) throws Exception {
        HashMap<String, String> map = new HashMap<>();
        map.put("timestamp", timestamp);//时间戳
        map.put("nonce", nonce);//随机字符串
        map.put("password", password);
        map.put("username", username);
        return getSHA1(map);
    }

    public static ApiParamReq getApiParamReq(){
        String timestamp = String.valueOf(System.currentTimeMillis());
        String nonce = "a1503";
//        String password = getBean(LgDvrPropertiesConfig.class).getLgApiPassword();
        String password = "f8267b7bc5e51994bab57c8e8884f203609d1dc3";
//        String password = "bafb2b44a07a02e5e9912f42cd197423884116a8";
//        String password = "9dddf2a4f7d94594ec2ea98407a410e1";
        try {
            return ApiParamReq.builder()
                    .timestamp(timestamp)
                    .nonce(nonce)
                    .password(password)
                    .sign(getSHA1(timestamp, nonce, password))
                    .build();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static ApiParamReq getApiParamReq(String password){
        String timestamp = String.valueOf(System.currentTimeMillis());
        String nonce = "a1503";
        String username = "";
//        String password = getBean(LgDvrPropertiesConfig.class).getLgApiPassword();
//        String password = "bafb2b44a07a02e5e9912f42cd197423884116a8";
//        String password = "9dddf2a4f7d94594ec2ea98407a410e1";
        try {
            return ApiParamReq.builder()
                    .timestamp(timestamp)
                    .nonce(nonce)
                    .password(password)
                    .sign(StringUtils.isNotBlank(username)?getSHA1(timestamp, nonce, password, username):getSHA1(timestamp, nonce, password))
                    .build();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) throws Exception {
        String password = "f8267b7bc5e51994bab57c8e8884f203609d1dc3";
        ApiParamReq apiParamReq = getApiParamReq(password);//密码
        System.out.println(apiParamReq.getSign());
        System.out.println(apiParamReq.getTimestamp());
        //
        System.out.println(StringUtils.join("http://61.169.120.202:40007/getInfo?password=",apiParamReq.getPassword(),"&nonce=",apiParamReq.getNonce(),"&sign=",apiParamReq.getSign(),"&timestamp=",apiParamReq.getTimestamp(),"&username=","wangxin","&deviceId=","00000000"));
    }

    public static String createUrl(String url, String password){
        ApiParamReq apiParamReq = getApiParamReq(password);
        return StringUtils.join(url,"?password=",apiParamReq.getPassword(),"&nonce=",apiParamReq.getNonce(),"&sign=",apiParamReq.getSign(),"&timestamp=",apiParamReq.getTimestamp());
    }
}