142 lines
5.9 KiB
Java
142 lines
5.9 KiB
Java
package sample;
|
||
|
||
import com.sunyard.RetWrap;
|
||
import com.sunyard.proto.Util;
|
||
import com.sunyard.util.Configure;
|
||
import java.io.IOException;
|
||
import racal.sunyard.main.SydApi4j;
|
||
import racal.sunyard.main.SydApiBuilder;
|
||
|
||
public class TestDemo {
|
||
public static String sign(SydApi4j api, String certDn, String orgData) {
|
||
byte[] nakedSign = api.SYD_NakedSignAll(2, orgData.getBytes(), certDn);
|
||
System.out.println("签名数据:"+ Util.bytes2HexString(nakedSign));
|
||
return Util.bytes2HexString(nakedSign);
|
||
}
|
||
|
||
public static Boolean signVer(SydApi4j api, String certDn, String orgData, String sign) {
|
||
boolean nakedVerify = api.SYD_NakedVerifyAll(2, orgData.getBytes(), Util.hexString2Bytes(sign), certDn);
|
||
System.out.println("验签结果:"+ nakedVerify);
|
||
return nakedVerify;
|
||
}
|
||
|
||
public static String enData(SydApi4j api, String certDn, String orgData) {
|
||
RetWrap genSM2 = api.generateDEByDnAll(3, certDn, orgData.getBytes());
|
||
byte[] cipherKey = (byte[])genSM2.get("CipherKey");
|
||
System.out.println("加密结果:"+ new String(cipherKey));
|
||
return new String(cipherKey);
|
||
}
|
||
|
||
public static String deData(SydApi4j api, String certDn, String sign) {
|
||
RetWrap denSM2 = api.decryptDEByDNAll(3, certDn, sign.getBytes());
|
||
byte[] oData = (byte[])denSM2.get("oData");
|
||
System.out.println("解密数据:"+ new String(oData));
|
||
return new String(oData);
|
||
}
|
||
|
||
public static String detachSign(SydApi4j api, String certDn, String orgData) {
|
||
String sign = api.detachedSignAll(2, orgData.getBytes(), certDn);
|
||
System.out.println("detach签名结果:"+ sign);
|
||
return sign;
|
||
}
|
||
|
||
public static Boolean detachSignVer(SydApi4j api, String orgData, String sign) {
|
||
boolean signVerify = api.detachedVerifyAll(2, orgData.getBytes(), sign);
|
||
System.out.println("detach验签:"+ signVerify);
|
||
return signVerify;
|
||
}
|
||
|
||
public static String snSign(SydApi4j api, String sn, String orgData) {
|
||
String sign = api.SM2SignCustomized(sn,orgData.getBytes());
|
||
System.out.println("sn签名结果:"+ sign);
|
||
return sign;
|
||
}
|
||
|
||
public static Boolean snSignVer(SydApi4j api, String sn,String orgData, String sign) {
|
||
boolean signVerify = api.SM2VerifyCustomized(sn, orgData.getBytes(), sign);
|
||
System.out.println("sn验签:"+ signVerify);
|
||
return signVerify;
|
||
}
|
||
|
||
public static String sm3hash(SydApi4j api,String pk,String orgData) {
|
||
byte[] hash = api.SM3Hash(pk, orgData.getBytes());
|
||
System.out.println("hash计算:"+ Util.bytes2HexString(hash));
|
||
return Util.bytes2HexString(hash);
|
||
}
|
||
|
||
|
||
public static void testAll(SydApi4j api, String certDn, String orgData) {
|
||
byte[] nakedSign = api.SYD_NakedSignAll(2, orgData.getBytes(), certDn);
|
||
System.out.println("签名数据:"+ Util.bytes2HexString(nakedSign));
|
||
boolean nakedVerify = api.SYD_NakedVerifyAll(2, orgData.getBytes(), nakedSign, certDn);
|
||
System.out.println("验签结果:"+ nakedVerify);
|
||
RetWrap genSM2 = api.generateDEByDnAll(3, certDn, orgData.getBytes());
|
||
byte[] cipherKey = (byte[])genSM2.get("CipherKey");
|
||
System.out.println("加密结果:"+ new String(cipherKey));
|
||
RetWrap denSM2 = api.decryptDEByDNAll(3, certDn, cipherKey);
|
||
byte[] oData = (byte[])denSM2.get("oData");
|
||
System.out.println("解密数据:"+ new String(oData));
|
||
String sign = api.detachedSignAll(2, orgData.getBytes(), certDn);
|
||
System.out.println("detach签名结果:"+ sign);
|
||
boolean signVerify = api.detachedVerifyAll(2, orgData.getBytes(), sign);
|
||
System.out.println("detach验签:"+ signVerify);
|
||
|
||
}
|
||
|
||
public static void main(String[] args) throws InterruptedException {
|
||
try {
|
||
String ip = Configure.getKey("ip");
|
||
System.out.println("ip:"+ ip);
|
||
int port = Integer.parseInt(Configure.getKey("port"));
|
||
System.out.println("port:"+ port);
|
||
int timeout = Integer.parseInt(Configure.getKey("timeout"));
|
||
System.out.println(""+ timeout);
|
||
String certDn = Configure.getKey("certDn");
|
||
System.out.println("certDn:"+ certDn);
|
||
String signorenData = Configure.getKey("signorenData");
|
||
System.out.println("signorenData:"+ signorenData);
|
||
String orgData = Configure.getKey("orgData");
|
||
System.out.println("orgData:"+ orgData);
|
||
int type = Integer.parseInt(Configure.getKey("type"));
|
||
System.out.println("type:"+ type);
|
||
SydApiBuilder builder = (new SydApiBuilder()).setIp(ip).setPort(port).setTimeout(timeout);
|
||
SydApi4j api = (SydApi4j)builder.build();
|
||
switch (type) {
|
||
case 1:
|
||
sign(api, certDn, orgData);
|
||
break;
|
||
case 2:
|
||
signVer(api, certDn, orgData, signorenData);
|
||
break;
|
||
case 3:
|
||
enData(api, certDn, orgData);
|
||
break;
|
||
case 4:
|
||
deData(api, certDn, signorenData);
|
||
break;
|
||
case 5:
|
||
detachSign(api, certDn, orgData);
|
||
break;
|
||
case 6:
|
||
detachSignVer(api, orgData, signorenData);
|
||
break;
|
||
case 7:
|
||
snSign(api, certDn, orgData);
|
||
break;
|
||
case 8:
|
||
snSignVer(api, certDn,orgData, signorenData);
|
||
break;
|
||
|
||
case 9:
|
||
sm3hash(api, certDn,orgData);
|
||
break;
|
||
case 0:
|
||
testAll(api, certDn, orgData);
|
||
break;
|
||
}
|
||
} catch (IOException e) {
|
||
throw new RuntimeException(e);
|
||
}
|
||
}
|
||
}
|