86 lines
1.8 KiB
Java
86 lines
1.8 KiB
Java
package cmbpoc;
|
|
|
|
import com.sunyard.RetWrap;
|
|
import com.sunyard.proto.Util;
|
|
import org.junit.*;
|
|
import racal.sunyard.main.SydApi4j;
|
|
|
|
public class FunctionTest {
|
|
|
|
private SydApi4j api;
|
|
private String keypairIndex;
|
|
private byte[] privateKey;
|
|
private byte[] publicKey;
|
|
private byte[] orgData;
|
|
private String sign;
|
|
|
|
@Before
|
|
public void start(){
|
|
// 调试模式
|
|
System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
|
|
|
// 建立链接(单台)
|
|
this.api = (SydApi4j) new SydApi4j().connect("192.168.21.166", 8889, null, 1000);
|
|
|
|
}
|
|
|
|
|
|
@After
|
|
public void stop(){
|
|
if ( null != api ) {
|
|
this.api.disconnect();
|
|
this.api = null;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Ignore
|
|
@Test
|
|
public void genKeyPair(){
|
|
RetWrap r = this.api.SYD_SM2GenKeyPair();
|
|
String pk = r.get("publicKey").toString();
|
|
String sk = r.get("privateKey").toString();
|
|
|
|
System.out.println("pk=" + pk);
|
|
System.out.println("sk=" + sk);
|
|
|
|
this.privateKey = Util.hexString2Bytes(sk);
|
|
this.publicKey = Util.hexString2Bytes(pk);
|
|
}
|
|
|
|
|
|
|
|
@Ignore
|
|
@Test
|
|
public void getPrivateKeyAndPublickKey(){
|
|
this.publicKey = Util.hexString2Bytes(
|
|
this.api.SM2GetPublicKeyC(keypairIndex)
|
|
);
|
|
|
|
this.privateKey = Util.hexString2Bytes(
|
|
this.api.SM2GetPrivateKeyC(keypairIndex)
|
|
);
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
public void SYD_SM2_Sign(){
|
|
this.api.SYD_SM2_Sign_HA(1, privateKey, publicKey, orgData);
|
|
this.api.SYD_SM2_Verify_HA(1, publicKey, orgData, sign);
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
public void SYD_SM2_Encrypt(){
|
|
byte[] enData = this.api.SYD_SM2Encrypt(publicKey, orgData);
|
|
byte[] deData = this.api.SYD_SM2Decrypt(privateKey, enData);
|
|
Assert.assertArrayEquals(enData, deData);
|
|
}
|
|
|
|
|
|
|
|
}
|