package com.yc.ship.module.trade.framework.common.ota;


import cn.hutool.json.JSONObject;
import com.yc.ship.module.trade.framework.common.Md5;

import java.io.UnsupportedEncodingException;

public class XieChengResolver {
    /**
     * 校验签名
     * @param header
     * @param strBody
     * @param userKey
     * @return
     * @throws UnsupportedEncodingException
     */
    public static boolean checkSign(JSONObject header, String strBody, String userKey) throws UnsupportedEncodingException{
        String otaSign = header.getStr("sign");
        String usSign = sign(header, strBody, userKey);
        if (usSign.equals(otaSign)) {
            return true;
        }
        return false;
    }

    /**
     * 签名
     * @param header
     * @param strBody
     * @param userKey
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String sign(JSONObject header, String strBody, String userKey) throws UnsupportedEncodingException {
        String accountId = header.getStr("accountId");
        String serviceName = header.getStr("serviceName");
        String requestTime = header.getStr("requestTime");
        String version = header.getStr("version");
        String waitSignString = accountId + serviceName + requestTime + strBody + version + userKey;
        String usSign = Md5.getMD5(waitSignString.getBytes("UTF-8"));
        return usSign;
    }
}
