84 lines
3.2 KiB
Java
84 lines
3.2 KiB
Java
package cmbpoc;
|
|
|
|
import com.sunyard.RetWrap;
|
|
import com.sunyard.SydApi;
|
|
import com.sunyard.proto.Util;
|
|
import org.junit.After;
|
|
import org.junit.Assert;
|
|
import org.junit.Before;
|
|
import org.junit.Test;
|
|
import racal.sunyard.main.SydApi4j;
|
|
|
|
public class SignatureVerifyTest {
|
|
|
|
private SydApi4j api;
|
|
private byte[] orgData;
|
|
private String SM2dn;
|
|
|
|
@Before
|
|
public void setUp() {
|
|
System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
|
this.api = (SydApi4j) new SydApi4j().connect("172.1.41.139", 8889, null, 3000);
|
|
this.orgData = "Hello, World! 测试数据".getBytes();
|
|
this.SM2dn = "C=cn,O=CFCA OCA1 SM2,OU=Local RA OCA1,OU=Units,CN=041@N91330108762038374D@sunyard@00000064";
|
|
}
|
|
|
|
@After
|
|
public void tearDown() {
|
|
if (api != null) {
|
|
this.api.disconnect();
|
|
this.api = null;
|
|
}
|
|
}
|
|
|
|
@Test
|
|
public void testP1SignAndVerify() {
|
|
byte[] sign = this.api.SYD_NakedSign(orgData, SM2dn);
|
|
Assert.assertNotNull("P1签名结果不应为空", sign);
|
|
System.out.println("P1签名结果: " + Util.bytes2HexString(sign));
|
|
|
|
boolean verifyResult = this.api.SYD_NakedVerify(orgData, sign, SM2dn);
|
|
Assert.assertTrue("P1验签应通过", verifyResult);
|
|
System.out.println("P1验签结果: " + verifyResult);
|
|
}
|
|
|
|
@Test
|
|
public void testP7DetachSignAndVerify() {
|
|
String sign = this.api.detachedSign(1, orgData, SM2dn);
|
|
Assert.assertNotNull("P7 Detach签名结果不应为空", sign);
|
|
System.out.println("P7 Detach签名结果: " + sign);
|
|
|
|
boolean verifyResult = this.api.detachedVerify(1, orgData, sign);
|
|
Assert.assertTrue("P7 Detach验签应通过", verifyResult);
|
|
System.out.println("P7 Detach验签结果: " + verifyResult);
|
|
}
|
|
|
|
@Test
|
|
public void testP7AttachSignAndVerify() throws Exception {
|
|
String sign = this.api.attachedSign(orgData, SM2dn);
|
|
Assert.assertNotNull("P7 Attach签名结果不应为空", sign);
|
|
System.out.println("P7 Attach签名结果: " + sign);
|
|
|
|
boolean verifyResult = this.api.attachedVerify(sign);
|
|
Assert.assertTrue("P7 Attach验签应通过", verifyResult);
|
|
System.out.println("P7 Attach验签结果: " + verifyResult);
|
|
}
|
|
|
|
@Test
|
|
public void testDigitalEnvelopeEncryptAndDecrypt() {
|
|
RetWrap genResult = this.api.generateDEByDn(SM2dn, orgData);
|
|
Assert.assertNotNull("数字信封生成结果不应为空", genResult);
|
|
|
|
byte[] cipherKey = (byte[]) genResult.get(SydApi.RET_CIPHER_KEY);
|
|
Assert.assertNotNull("数字信封密文密钥不应为空", cipherKey);
|
|
System.out.println("数字信封密文: " + new String(cipherKey));
|
|
|
|
RetWrap decryptResult = this.api.decryptDEByDN(SM2dn, cipherKey);
|
|
Assert.assertNotNull("数字信封解密结果不应为空", decryptResult);
|
|
|
|
byte[] decryptedData = (byte[]) decryptResult.get("oData");
|
|
Assert.assertNotNull("解密数据不应为空", decryptedData);
|
|
Assert.assertArrayEquals("解密数据应与原始数据一致", orgData, decryptedData);
|
|
System.out.println("数字信封解密结果: " + new String(decryptedData));
|
|
}
|
|
} |