sydapi/src/test/java/sample/Shangqing.java
2023-02-28 15:20:38 +08:00

89 lines
2.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package sample;
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 Shangqing {
private SydApi4j api;
// 测试数据
private String uid = "111";
private String dn = "C=CN,ST=321,L=123123,O=123,OU=321,OU=321,CN=123";
private byte[] orgData = new byte[2 * 1024 * 1024];
@Before
public void before() {
// 通过设置debug的属性开启debug如果不设置则默认不打开
System.setProperty("com.sunyard.sydapi4j.debug", "true");
api = (SydApi4j) new SydApi4j().connect("172.16.17.161", 8889, null, 1000);
}
@After
public void after() {
try {
this.api.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 裸签示例
* 1.10 SM2GetPublicKeyC
* 1.1.3 SYD_SM2_Sign_HA
* 1.2.3 SYD_SM2_Verify_HA
*/
@Test
public void sv(){
String pk = api.SM2GetPublicKeyC(uid, 0 );
String sign2 = api.SYD_SM2_Sign_HA(
uid,
SydApi.DATA_HASH,
Util.hexString2Bytes(pk),
orgData
);
System.out.print("\n\n----- SM2 算法签名 结果: ");
System.out.println( sign2 );
System.out.println( api.SYD_SM2_Verify_HA(
SydApi.DATA_HASH,
Util.hexString2Bytes(pk),
orgData ,
sign2
) );
}
/**
* 数字信封示例
* 1.3 generateDEByDn
* 1.4 decryptDEByDN
*/
@Test
public void dm(){
// 生成数字信封
RetWrap retWrap = api.generateDEByDn(dn, orgData );
byte[] cipherKey = (byte[]) retWrap.get(SydApi.RET_CIPHER_KEY);
System.out.println("数字信封: " + new String(cipherKey));
// 解密数字信封
RetWrap retWrap1 = api.decryptDEByDN(dn, cipherKey);
byte[] oData = (byte[]) retWrap1.get("oData");
Assert.assertArrayEquals( oData, orgData );
}
}