package com.tax.token.utils;


import isc.authclt.IscJcrypt;

import org.apache.log4j.Logger;

public class AuthC {

	private static Logger logger = Logger.getLogger(AuthC.class);
	public  static  String getClientHello(String index,String reqUrl) {
		long start = System.currentTimeMillis();
		IscJcrypt JapiC = null;
		String result = "";


		int err = 0;
		String srvContainerC = "";
		try {
			JapiC = new IscJcrypt();

			srvContainerC = reqUrl + index;// 打开指定的签名服务器1
			// //打开指定的签名服务器2�?
			logger.info("尝试getClientHello�?"+srvContainerC);
			// 打开设备，模拟客户端，准备密码运�?
			int i=0;
			err = JapiC.openDevice(srvContainerC);
			logger.info("尝试openDevice完成�?"+err);
			while(err != 0 && i<3){
				i++;
				System.out.println("打开设备重试次数�?"+i);
				err = JapiC.openDevice(srvContainerC);
			}
			if (err != 0) {
				// 如果返回值不等于0，则�?要对错误信息进行处理
				logger.error("srvContainerC=" + srvContainerC);
				logger.error("错误信息C=" + JapiC.errMsg);
				JapiC.closeDevice();
				JapiC = null;
				return "";
			}
			// 生成ClientHello信息
			i=0;
			err = JapiC.clientHello(0);// 客户端发起第�?�?
			while(err != 0 && i<3){
				i++;
				System.out.println("身份认证客户端获取ClientHello重试次数�?"+i);
				err = JapiC.clientHello(0);
			}
			if (err != 0) {
				logger.error("身份认证客户端获取ClientHello出错====="
						+ JapiC.getErrMsg());
				JapiC.closeDevice();
				JapiC = null;
				return "";
			}
			result = JapiC.strResult; // 取出结果

			JapiC.closeDevice();
			JapiC = null;
		} catch (Exception e) {
			logger.error("获取serverHello出错�?======="+e);
			err = JapiC.closeDevice();
			JapiC = null;
			e.printStackTrace();
		}
		System.out.println(System.currentTimeMillis()-start+" getClientHello cost!");
		System.out.println("result:"+result);
		return result;
	}

	public static String getClientAuth(String serverHello,String index,String reqUrl){
		long start = System.currentTimeMillis();
		IscJcrypt JapiC = null;

		int err = 0;
		String srvContainerC = "";
		String strClientAuth ="";
		try {

				JapiC = new IscJcrypt();
				srvContainerC = reqUrl + index;// 打开指定的签名服务器1
				// //打开指定的签名服务器2�?

				// 打开设备，模拟客户端，准备密码运�?
//				int i=0;
				err = JapiC.openDevice(srvContainerC);
//				while(err != 0 && i<3){
//					i++;
//					System.out.println("打开设备重试次数�?"+i);
//					err = JapiC.openDevice(srvContainerC);
//				}
				if (err != 0) {
					// 如果返回值不等于0，则�?要对错误信息进行处理
					logger.error("srvContainerC=" + srvContainerC);
					logger.error("错误信息C=" + JapiC.errMsg);
					JapiC.closeDevice();
					JapiC = null;
					return "";
				}

				// 客户端发起第二次
//				i=0;
				err = JapiC.clientAuth(serverHello);
//				while(err != 0 && i<2){
//					i++;
//					System.out.println("客户端获取clientAuth重试次数�?"+i);
//					err = JapiC.clientAuth(serverHello);
//				}
				if (err != 0) {
					logger.error("客户端获取clientAuth失败:" + JapiC.getErrMsg()+" serverHello:"+serverHello);
					JapiC.closeDevice();
					JapiC = null;
					return "";
				}
				strClientAuth = JapiC.strResult;// 取出结果
				JapiC.closeDevice();
				JapiC = null;
		} catch (Exception e) {
			logger.error("获取ClientAuth出错�?======="+e);
			err = JapiC.closeDevice();
			JapiC = null;
			e.printStackTrace();
		}
		System.out.println(System.currentTimeMillis()-start+" getClientAuth cost!");
		return strClientAuth;
	}
	
}
