sdf api
This commit is contained in:
@@ -17,8 +17,7 @@ import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.pool2.impl.GenericObjectPool;
|
||||
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
@@ -44,7 +43,7 @@ import java.util.stream.Collectors;
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class DeviceManager implements ApplicationRunner {
|
||||
public class DeviceManager implements InitializingBean {
|
||||
|
||||
private static final Map<Long, AtomicInteger> ROUND_MAP = new HashMap<>();
|
||||
|
||||
@@ -238,7 +237,7 @@ public class DeviceManager implements ApplicationRunner {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
Executors.newSingleThreadScheduledExecutor()
|
||||
.scheduleWithFixedDelay(this::syncDevice, 0L, 5L, TimeUnit.MINUTES);
|
||||
}
|
||||
|
||||
+27
-52
@@ -1,8 +1,5 @@
|
||||
package com.sunyard.chsm.pool;
|
||||
|
||||
import com.sunyard.chsm.enums.AlgMode;
|
||||
import com.sunyard.chsm.enums.KeyAlg;
|
||||
import com.sunyard.chsm.enums.Padding;
|
||||
import com.sunyard.chsm.sdf.SdfApiService;
|
||||
import com.sunyard.chsm.sdf.context.AlgId;
|
||||
import com.sunyard.chsm.sdf.model.EccKey;
|
||||
@@ -40,58 +37,24 @@ public class LoadBalancedSdfApiService implements SdfApiService {
|
||||
return apply(s -> s.getSdfApiAdapter().generateRandom(s.getSessionHandle(), len));
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] genSymKey(KeyAlg alg, Integer keyLen) {
|
||||
return generateRandom(keyLen);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] genSymKey(KeyAlg alg) {
|
||||
return generateRandom(16);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] symEncrypt(KeyAlg alg, AlgMode mode, Padding padding, byte[] key, byte[] data) {
|
||||
byte[] paddingData = null;
|
||||
switch (padding) {
|
||||
case NOPadding:
|
||||
paddingData = data;
|
||||
break;
|
||||
case PCKS5Padding:
|
||||
paddingData = PaddingUtil.PKCS5Padding(data);
|
||||
break;
|
||||
case PCKS7Padding:
|
||||
paddingData = PaddingUtil.PKCS7Padding(data);
|
||||
break;
|
||||
}
|
||||
byte[] finalPaddingData = paddingData;
|
||||
return apply(s -> s.getSdfApiAdapter()
|
||||
.externalSymEncrypt(s.getSessionHandle(), key, null, null, finalPaddingData));
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] symEncrypt(KeyAlg alg, byte[] key, byte[] data) {
|
||||
return new byte[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] symEncrypt(AlgId alg, byte[] key, byte[] iv, byte[] data) {
|
||||
return apply(s -> s.getSdfApiAdapter().externalSymEncrypt(s.getSessionHandle(), key, alg, iv, PaddingUtil.PKCS7Padding(data)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] symDecrypt(KeyAlg alg, AlgMode mode, Padding padding, byte[] key, byte[] data) {
|
||||
return new byte[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] symDecrypt(KeyAlg alg, byte[] key, byte[] data) {
|
||||
return new byte[0];
|
||||
return apply(s -> {
|
||||
String keyHandle = s.getSdfApiAdapter().importKey(s.getSessionHandle(), key);
|
||||
byte[] encrypt = s.getSdfApiAdapter().symEncrypt(s.getSessionHandle(), keyHandle, alg, iv, PaddingUtil.PKCS7Padding(data));
|
||||
s.getSdfApiAdapter().destroyKey(s.getSessionHandle(), keyHandle);
|
||||
return encrypt;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] symDecrypt(AlgId alg, byte[] key, byte[] iv, byte[] data) {
|
||||
byte[] decrypt = apply(s -> s.getSdfApiAdapter().externalSymDecrypt(s.getSessionHandle(), key, alg, iv, data));
|
||||
byte[] decrypt = apply(s -> {
|
||||
String keyHandle = s.getSdfApiAdapter().importKey(s.getSessionHandle(), key);
|
||||
byte[] d = s.getSdfApiAdapter().symDecrypt(s.getSessionHandle(), keyHandle, alg, iv, data);
|
||||
s.getSdfApiAdapter().destroyKey(s.getSessionHandle(), keyHandle);
|
||||
return d;
|
||||
});
|
||||
return PaddingUtil.PKCS7Unpadding(decrypt);
|
||||
}
|
||||
|
||||
@@ -102,7 +65,12 @@ public class LoadBalancedSdfApiService implements SdfApiService {
|
||||
|
||||
@Override
|
||||
public byte[] calculateMAC(byte[] symKey, byte[] pucIv, byte[] pucData) {
|
||||
return apply(s -> s.getSdfApiAdapter().calculateMAC(s.getSessionHandle(), symKey, AlgId.SGD_SM4_MAC, pucIv, pucData));
|
||||
return apply(s -> {
|
||||
String keyHandle = s.getSdfApiAdapter().importKey(s.getSessionHandle(), symKey);
|
||||
byte[] mac = s.getSdfApiAdapter().calculateMAC(s.getSessionHandle(), keyHandle, AlgId.SGD_SM4_MAC, pucIv, pucData);
|
||||
s.getSdfApiAdapter().destroyKey(s.getSessionHandle(), keyHandle);
|
||||
return mac;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -112,12 +80,19 @@ public class LoadBalancedSdfApiService implements SdfApiService {
|
||||
|
||||
@Override
|
||||
public byte[] hash(byte[] pucData) {
|
||||
return apply(s -> s.getSdfApiAdapter().hashOperation(s.getSessionHandle(), AlgId.SGD_SM3, null, null, pucData));
|
||||
return apply(s -> {
|
||||
String newSession = s.getSdfApiAdapter().openSession(s.getDeviceHandle());
|
||||
s.getSdfApiAdapter().hashInit(newSession, AlgId.SGD_SM3, null, null);
|
||||
s.getSdfApiAdapter().hashUpdate(newSession, pucData);
|
||||
byte[] hash = s.getSdfApiAdapter().hashFinish(newSession);
|
||||
s.getSdfApiAdapter().closeSession(newSession);
|
||||
return hash;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] encryptByTMK(byte[] data) {
|
||||
byte[] pad = PaddingUtil.PKCS7Unpadding(data);
|
||||
byte[] pad = PaddingUtil.PKCS7Padding(data);
|
||||
return apply(s -> s.getSdfApiAdapter().symEncrypt(s.getSessionHandle(), s.getKeyHandle(), AlgId.SGD_SM4_CBC, new byte[8], pad));
|
||||
}
|
||||
|
||||
|
||||
@@ -44,6 +44,7 @@ public class TMKContextFactory extends BasePooledObjectFactory<TMKContext> {
|
||||
this.deviceHandle = sdfApiAdapter.openDevice();
|
||||
hs = sdfApiAdapter.openSession(deviceHandle);
|
||||
}
|
||||
keyHandle.setDeviceHandle(deviceHandle);
|
||||
keyHandle.setSessionHandle(hs);
|
||||
keyHandle.setEncTmk(context.getEncTmk());
|
||||
|
||||
|
||||
@@ -1,34 +1,34 @@
|
||||
package sdf;
|
||||
import com.sunyard.chsm.WebServerApp;
|
||||
import com.sunyard.chsm.enums.KeyAlg;
|
||||
import com.sunyard.chsm.sdf.BCSdfApiService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.Assert;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
|
||||
@Slf4j
|
||||
@SpringBootTest(classes = WebServerApp.class)
|
||||
public class BCSdfApiServiceTest {
|
||||
|
||||
@Autowired
|
||||
private BCSdfApiService bcSdfApiService;
|
||||
|
||||
// 对称加密解密测试
|
||||
@Test
|
||||
void testSym() {
|
||||
byte[] data = new byte[128];
|
||||
new Random().nextBytes( data );
|
||||
|
||||
byte[] key = bcSdfApiService.genSymKey( KeyAlg.SM4 );
|
||||
byte[] enData = bcSdfApiService.symEncrypt( KeyAlg.SM4, key, data );
|
||||
byte[] deData = bcSdfApiService.symDecrypt( KeyAlg.SM4, key, enData );
|
||||
|
||||
Assert.assertArrayEquals( data, deData );
|
||||
|
||||
}
|
||||
}
|
||||
//package sdf;
|
||||
//import com.sunyard.chsm.WebServerApp;
|
||||
//import com.sunyard.chsm.enums.KeyAlg;
|
||||
//import com.sunyard.chsm.sdf.BCSdfApiService;
|
||||
//import lombok.extern.slf4j.Slf4j;
|
||||
//import org.junit.Assert;
|
||||
//import org.junit.jupiter.api.Test;
|
||||
//import org.springframework.beans.factory.annotation.Autowired;
|
||||
//import org.springframework.boot.test.context.SpringBootTest;
|
||||
//
|
||||
//import java.util.Random;
|
||||
//
|
||||
//
|
||||
//@Slf4j
|
||||
//@SpringBootTest(classes = WebServerApp.class)
|
||||
//public class BCSdfApiServiceTest {
|
||||
//
|
||||
// @Autowired
|
||||
// private BCSdfApiService bcSdfApiService;
|
||||
//
|
||||
// // 对称加密解密测试
|
||||
// @Test
|
||||
// void testSym() {
|
||||
// byte[] data = new byte[128];
|
||||
// new Random().nextBytes( data );
|
||||
//
|
||||
// byte[] key = bcSdfApiService.genSymKey( KeyAlg.SM4 );
|
||||
// byte[] enData = bcSdfApiService.symEncrypt( KeyAlg.SM4, key, data );
|
||||
// byte[] deData = bcSdfApiService.symDecrypt( KeyAlg.SM4, key, enData );
|
||||
//
|
||||
// Assert.assertArrayEquals( data, deData );
|
||||
//
|
||||
// }
|
||||
//}
|
||||
|
||||
Reference in New Issue
Block a user