删除控制台输出
This commit is contained in:
@@ -1,13 +1,16 @@
|
||||
package com.sunyard.sydapi.example;
|
||||
|
||||
import com.sunyard.RetWrap;
|
||||
import com.sunyard.SydApi;
|
||||
import com.sunyard.proto.Util;
|
||||
import com.sunyard.util.SYMUtil;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import racal.sunyard.main.SydApi4j;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Base64;
|
||||
|
||||
import static com.sunyard.sydapi.util.PrintUtil.printByteArray;
|
||||
import static com.sunyard.sydapi.util.PrintUtil.printRetWrap;
|
||||
@@ -20,7 +23,7 @@ public class ABCNorthBranch {
|
||||
SydApi4j api;
|
||||
|
||||
// SM2密钥索引
|
||||
String SM2PrivateKeyTag = "111111111111111800000000001";
|
||||
String SM2PrivateKeyTag = "111111111111115100000000001";
|
||||
// SM2公钥
|
||||
String SM2PublicKey = null;
|
||||
// SM2私钥
|
||||
@@ -85,19 +88,39 @@ public class ABCNorthBranch {
|
||||
System.out.println(s);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// 75
|
||||
@Test
|
||||
public void testSM2Verify2() {
|
||||
String thisOrgData = "FFFFFFFFFFFFFFFF";
|
||||
public void testSM2SignAndVerify() {
|
||||
String orgData = "11111111111111";
|
||||
|
||||
String s = api.SM2SignC(0, SM2PrivateKey, Util.hexString2Bytes(SM2PublicKey), thisOrgData.getBytes());
|
||||
// 这里,不管是签名还是验签,都需要在外部做一次hash。
|
||||
byte[] hash = api.SM3Hash(SM2PublicKey, orgData.getBytes());
|
||||
|
||||
System.out.println(s);
|
||||
boolean b = api.SM2Verify( Util.hexString2Bytes(SM2PublicKey), thisOrgData.getBytes(), s);
|
||||
System.out.println(b);
|
||||
/**
|
||||
* 用私钥签名(外部私钥)
|
||||
* @param nOrgDataType 签名数据类型,0 : hash 1 : 原数据
|
||||
* @param privateKey LMK34-35 下加密的私钥
|
||||
* @param orgData “签名数据类型”为 0 时:HASH 值。“签名数据类型”为 1 时:要签名的信息
|
||||
* @return 签名的 base64 编码
|
||||
*/
|
||||
String s = api.SM2Sign(SydApi.DATA_HASH, Util.hexString2Bytes(SM2PrivateKey), hash);
|
||||
|
||||
// 这里的签名结果已经符合条件了,不需要做转换
|
||||
System.out.println("签名结果: " + s);
|
||||
System.out.println("decBase64:" + Util.bytes2HexString(Util.decodeBase64(s)));
|
||||
/**
|
||||
* 用指定的公钥对指定的原始数据进行数字签名验证(PKCS#1)
|
||||
* @param nOrgDataType 签名数据类型,SydApi.DATA_ORIGIN(1) 原始数据 SydApi.DATA_HASH(0) : hash 后的类型
|
||||
* @param publicKey 公钥串,DER编码,HEX格式
|
||||
* @param orgData 待签名的原始数据,“签名数据类型”为 0 时:HASH 值。“签名数据类型”为 1 时:要签名的信息
|
||||
* @param base64Sign 签名数据 (DER 编码 + Base64 编码)
|
||||
* @return 验签是否通过
|
||||
*/
|
||||
boolean b = api.SM2Verify(SydApi.DATA_HASH, Util.hexString2Bytes(SM2PublicKey), hash, s);
|
||||
System.out.println("验签结果: " + b);
|
||||
}
|
||||
|
||||
|
||||
// 测试通过
|
||||
// 75
|
||||
@Test
|
||||
|
||||
@@ -775,7 +775,7 @@ public class TestAllMethods {
|
||||
// LF
|
||||
@Test
|
||||
public void testReadKey() {
|
||||
String index = "333333333333333300A00000001";
|
||||
String index = "000000000000000100022222222";
|
||||
RetWrap retWrap = api.readKey(index);
|
||||
printRetWrap(retWrap);
|
||||
|
||||
@@ -1011,6 +1011,7 @@ public class TestAllMethods {
|
||||
byte[] oData = api.SM3Hash(pk, Util.hexString2Bytes(orgData));
|
||||
String s = api.SYD_SM2_Sign_HA(SydApi.DATA_HASH, Util.hexString2Bytes(sk), Util.hexString2Bytes(pk), oData);
|
||||
|
||||
System.out.println("sign:" + s);
|
||||
System.out.println("decBase64:" + Util.bytes2HexString(Util.decodeBase64(s)));
|
||||
System.out.println("decBase64-1:" + Util.bytes2HexString(SYMUtil.base64SignAsDerSign(s)));
|
||||
boolean b = api.SYD_SM2_Verify_HA(SydApi.DATA_HASH, Util.hexString2Bytes(pk), Util.hexString2Bytes(orgData), s);
|
||||
@@ -1663,10 +1664,10 @@ public class TestAllMethods {
|
||||
@Test
|
||||
public void testSM2Verify3() {
|
||||
|
||||
String thisPublicKey = "03420004C0D7FCD41C4A27B6B2DAC20B1332ED89202F2F85EF79929CAD1028F11AE0E8EFECA9C26A2145773F234631B2D2F1C7DAE5880C681122B28DA842509CEC8DF82D";
|
||||
String thisPublicKey = "034200048043AF118B45629857DB693105849B2617FED249D59DFD29BE9A891ADEE7441A2D8C62020953DAEF038184E0244310CF2123C507F26F5380FFECC8D53E79B24E";
|
||||
String thisOrgData = "FFFFFFFFFFFFFFFF";
|
||||
|
||||
String s = api.SM2SignC(1, "000000000000000100000000001", Util.hexString2Bytes(thisPublicKey), thisOrgData.getBytes());
|
||||
String s = api.SM2SignC(1, "111111111111115100100000001", Util.hexString2Bytes(thisPublicKey), thisOrgData.getBytes());
|
||||
|
||||
System.out.println(s);
|
||||
System.out.println("DecBase64:" + Util.bytes2HexString(Base64.getDecoder().decode(s)));
|
||||
@@ -1691,6 +1692,26 @@ public class TestAllMethods {
|
||||
System.out.println(b);
|
||||
}
|
||||
|
||||
/**
|
||||
* 外部做hash,避免当原始数据过长时解析参数时ByteBuffer长度不够时报错的问题
|
||||
*/
|
||||
// 测试通过
|
||||
// 75
|
||||
@Test
|
||||
public void testSM2SignAndVerify() {
|
||||
String privateKey = "000100009FBA90C22E18982D60022C683219F72DAAA1B878A094181381486FC5137B1B160000000000000000000000000000000000000000000000000000000000000000";
|
||||
String thisPublicKey = "03420004E5888368B9D5BC319FE48AB92123857E696EFB1A7E08D75DD5C3C5A5E9FA18099EFFCAFD61A0838527D36C7FA6AE8E2649FB89883FC44A494830CE642A03DC24";
|
||||
String orgData = "11111111111111";
|
||||
|
||||
// 这里,不管是签名还是验签,都需要在外部做一次hash。
|
||||
byte[] hash = api.SM3Hash(thisPublicKey, orgData.getBytes());
|
||||
String s = api.SM2Sign(SydApi.DATA_HASH, Util.hexString2Bytes(privateKey), hash);
|
||||
|
||||
System.out.println("decBase64:" + Util.bytes2HexString(Util.decodeBase64(s)));
|
||||
boolean b = api.SM2Verify(SydApi.DATA_HASH, Util.hexString2Bytes(thisPublicKey), hash, s);
|
||||
System.out.println("验签结果: " + b);
|
||||
}
|
||||
|
||||
// 测试通过
|
||||
// 77
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user