Search in sources :

Example 1 with TsaSigner

use of cn.topca.tsa.TsaSigner in project portal by ixinportal.

the class TimeServiceTest method test.

@Test
public void test() throws Exception {
    TsaSigner tsaSigner = new TsaSigner("http://192.168.103.214:7070/TopTSA/tspLogin/timeStamp", "admin", "password");
    byte[] btStr = tsaSigner.genTimeStamp("33e51d71ff9c5443c106acc8b8808eca040f64d1".getBytes("utf-8"), "SHA1");
    Base64Encoder enc = new Base64Encoder();
    String rep = enc.encode(btStr);
    // String rep = new String(btStr, "utf-8");
    System.out.println("EntRep:" + rep);
// System.out.println("123123"+ret_data.getString("keys").length());
}
Also used : TsaSigner(cn.topca.tsa.TsaSigner) Base64Encoder(com.thoughtworks.xstream.core.util.Base64Encoder) Test(org.junit.Test)

Example 2 with TsaSigner

use of cn.topca.tsa.TsaSigner in project portal by ixinportal.

the class EvidenceSaveServiceApi method genTimeStamp.

/**
 * 签时间戳接口
 * @param authentication
 * @param plainHash
 * @param hashAlg
 * @return
 * @throws Exception
 */
public static Map<String, Object> genTimeStamp(RealNameAuthentication authentication, String plainHash, String hashAlg, String sn, HttpClientService clientService) {
    Map<String, Object> retMap = new HashMap<String, Object>();
    boolean bl = false;
    retMap.put("retStatus", bl);
    String url = null;
    String str = null;
    String retTime = null;
    try {
        if (authentication.getVersionNumber() == 1) {
            TsaSigner tsaSigner = new TsaSigner(authentication.getAccessTokenaddress(), authentication.getIdCode(), authentication.getKeyCode());
            byte[] repTime = tsaSigner.genTimeStamp(plainHash.getBytes("utf-8"), hashAlg);
            Base64Encoder enc = new Base64Encoder();
            String rep = enc.encode(repTime);
        // return rep;
        } else {
            url = authentication.getRealNameddress() + "/openapi/timestamp";
            String hex = String.valueOf(HexUtils.str2HexStr(plainHash, "utf-8"));
            str = "{\"plainHash\":\"" + hex + "\"," + "\"hashAlg\":\"" + hashAlg + "\"," + "\"accessToken\":\"" + authentication.getAuthorizationCode() + "\"}";
            Map<String, String> header = new HashMap<>();
            header.put("Content-Type", "application/json");
            // System.out.println("genTimeStamp start param : " + str);
            retTime = OkHttpClientManagerTime.post(url, AuthService.getHeader().get("Authorization").toString(), str);
            JSONObject jsonRep = JSONObject.parseObject(retTime);
            // 判断签名时间戳是否成功
            if (StringUtils.isNotEmpty(retTime) && jsonRep.getInteger("retCode") == 0 && StringUtils.isNotEmpty(jsonRep.getString("b64TimeStamp"))) {
                // 得到时间戳签名值
                String timeStamp = jsonRep.getString("b64TimeStamp");
                EvidenceClientTimeStamp clientTimeStamp = new EvidenceClientTimeStamp();
                clientTimeStamp.setHashAlgorithm(hashAlg);
                clientTimeStamp.setEvidenceSn(sn);
                clientTimeStamp.setName("1");
                clientTimeStamp.setCreateTime(new Date());
                clientTimeStamp.setTimeSource("国家授时中心");
                clientTimeStamp.setHashvalue(plainHash);
                clientTimeStamp.setCertificate(timeStamp);
                clientTimeStamp.setSignatureTime(new Date());
                clientTimeStamp.setCertificateDn(authentication.getCertificateCn());
                // clientTimeStamp.setCreateTimeMs(date.getTime());
                bl = true;
                retMap.put("obj", clientTimeStamp);
                retMap.put("retStatus", bl);
            } else {
                LogUtil.evidencelog(SpringContextHolder.getBean(SqlSession.class), sn, "存证接口_时间戳", "时间戳失败,证据编号:" + sn + ", 请求参数:" + str + ", 请求地址:" + url + ", 返回结果:" + retTime);
                return retMap;
            }
        }
    } catch (Exception e) {
        bl = false;
        retMap.put("retStatus", bl);
        // TODO: handle exception
        LogUtil.evidencelog(SpringContextHolder.getBean(SqlSession.class), sn, "存证接口_时间戳", "时间戳失败,证据编号:" + sn + ", 请求参数:" + str + ", 请求地址:" + url + ", 返回结果:" + retTime + ", 失败原因:" + e.toString());
        return retMap;
    }
    return retMap;
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) HashMap(java.util.HashMap) EvidenceClientTimeStamp(com.itrus.portal.db.EvidenceClientTimeStamp) Base64Encoder(com.thoughtworks.xstream.core.util.Base64Encoder) Date(java.util.Date) IOException(java.io.IOException) JSONObject(com.alibaba.fastjson.JSONObject) JSONObject(com.alibaba.fastjson.JSONObject) TsaSigner(cn.topca.tsa.TsaSigner)

Example 3 with TsaSigner

use of cn.topca.tsa.TsaSigner in project portal by ixinportal.

the class EvidenceSaveServiceApi method genTimeStampAsyn.

/**
 * 签时间戳接口 (异步)
 * @param authentication
 * @param plainHash
 * @param hashAlg
 * @return
 * @throws Exception
 */
public static Map<String, Object> genTimeStampAsyn(RealNameAuthentication authentication, String plainHash, String hashAlg, String sn, HttpClientService clientService) {
    Map<String, Object> retMap = new HashMap<String, Object>();
    boolean bl = false;
    retMap.put("retStatus", bl);
    String url = null;
    String str = null;
    String retTime = null;
    try {
        if (authentication.getVersionNumber() == 1) {
            TsaSigner tsaSigner = new TsaSigner(authentication.getAccessTokenaddress(), authentication.getIdCode(), authentication.getKeyCode());
            byte[] repTime = tsaSigner.genTimeStamp(plainHash.getBytes("utf-8"), hashAlg);
            Base64Encoder enc = new Base64Encoder();
            String rep = enc.encode(repTime);
        // return rep;
        } else {
            url = authentication.getRealNameddress() + "/openapi/timestamp";
            String hex = String.valueOf(HexUtils.str2HexStr(plainHash, "utf-8"));
            str = "{\"plainHash\":\"" + hex + "\"," + "\"hashAlg\":\"" + hashAlg + "\"," + "\"accessToken\":\"" + authentication.getAuthorizationCode() + "\"}";
            Map<String, String> header = new HashMap<>();
            header.put("Content-Type", "application/json");
            // System.out.println("genTimeStamp start param : " + str);
            // HttpAsyncClientUtil.getInstance().execute(
            // url,
            // header,
            // str,
            // new EvidenceTimeStampTask(
            // authentication,
            // plainHash,
            // hashAlg,
            // sn));
            OkHttpClientManagerTime.postAsyn(url, AuthService.getHeader().get("Authorization").toString(), str, new EvidenceTimeStampTask(authentication, plainHash, hashAlg, sn));
        // retTime = OkHttpClientManagerTime.post(url, AuthService.getHeader().get("Authorization").toString(), str);
        // 
        // JSONObject jsonRep = JSONObject.parseObject(retTime);
        // // 判断签名时间戳是否成功
        // if(StringUtils.isNotEmpty(retTime)
        // && jsonRep.getInteger("retCode") == 0
        // && StringUtils.isNotEmpty(jsonRep.getString("b64TimeStamp"))) {
        // // 得到时间戳签名值
        // String timeStamp = jsonRep.getString("b64TimeStamp");
        // 
        // EvidenceClientTimeStamp clientTimeStamp = new EvidenceClientTimeStamp();
        // clientTimeStamp.setHashAlgorithm(hashAlg);
        // clientTimeStamp.setEvidenceSn(sn);
        // clientTimeStamp.setName("1");
        // clientTimeStamp.setCreateTime(new Date());
        // clientTimeStamp.setTimeSource("国家授时中心");
        // clientTimeStamp.setHashvalue(plainHash);
        // clientTimeStamp.setCertificate(timeStamp);
        // clientTimeStamp.setSignatureTime(new Date());
        // clientTimeStamp.setCertificateDn(authentication.getCertificateCn());
        // //			        clientTimeStamp.setCreateTimeMs(date.getTime());
        // bl = true;
        // retMap.put("obj", clientTimeStamp);
        // retMap.put("retStatus", bl);
        // } else {
        // LogUtil.evidencelog(SpringContextHolder.getBean(SqlSession.class), sn, "存证接口_时间戳", "时间戳失败,证据编号:" + sn
        // + ", 请求参数:" + str + ", 请求地址:" + url + ", 返回结果:" + retTime);
        // return retMap;
        // }
        }
    } catch (Exception e) {
        bl = false;
        retMap.put("retStatus", bl);
        // TODO: handle exception
        LogUtil.evidencelog(SpringContextHolder.getBean(SqlSession.class), sn, "存证接口_时间戳", "时间戳失败,证据编号:" + sn + ", 请求参数:" + str + ", 请求地址:" + url + ", 返回结果:" + retTime + ", 失败原因:" + e.toString());
        return retMap;
    }
    return retMap;
}
Also used : HashMap(java.util.HashMap) JSONObject(com.alibaba.fastjson.JSONObject) TsaSigner(cn.topca.tsa.TsaSigner) Base64Encoder(com.thoughtworks.xstream.core.util.Base64Encoder) IOException(java.io.IOException)

Aggregations

TsaSigner (cn.topca.tsa.TsaSigner)3 Base64Encoder (com.thoughtworks.xstream.core.util.Base64Encoder)3 JSONObject (com.alibaba.fastjson.JSONObject)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 EvidenceClientTimeStamp (com.itrus.portal.db.EvidenceClientTimeStamp)1 Date (java.util.Date)1 SqlSession (org.apache.ibatis.session.SqlSession)1 Test (org.junit.Test)1