add task
This commit is contained in:
parent
b229e6abdf
commit
4febd0520d
@ -1,78 +0,0 @@
|
||||
package com.sunyard.chsm.task;
|
||||
|
||||
import com.sunyard.chsm.enums.DeviceTmkStatus;
|
||||
import com.sunyard.chsm.mapper.ParamConfMapper;
|
||||
import com.sunyard.chsm.mapper.SpDeviceMapper;
|
||||
import com.sunyard.chsm.model.entity.Device;
|
||||
import com.sunyard.chsm.sdf.adapter.SdfApiAdapter;
|
||||
import com.sunyard.chsm.sdf.adapter.SdfApiAdapterFactory;
|
||||
import com.sunyard.chsm.sdf.context.AlgId;
|
||||
import com.sunyard.chsm.sdf.model.DeviceInfo;
|
||||
import com.sunyard.chsm.sdf.model.EccCipher;
|
||||
import com.sunyard.chsm.sdf.model.EccPubKey;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @since 2024/12/10
|
||||
*/
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class CheckDeviceRunner implements Runnable {
|
||||
|
||||
private final SpDeviceMapper spDeviceMapper;
|
||||
private final ParamConfMapper paramConfMapper;
|
||||
private final Device device;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
boolean hasError = false;
|
||||
DeviceTmkStatus status = DeviceTmkStatus.device_error;
|
||||
SdfApiAdapter sdfApi = SdfApiAdapterFactory.newInstance(device.getManufacturerModel(), device.getServiceIp(), device.getServicePort());
|
||||
String hd = sdfApi.openDevice();
|
||||
String hs = null;
|
||||
|
||||
try {
|
||||
hs = sdfApi.openSession(hd);
|
||||
DeviceInfo info = sdfApi.getDeviceInfo(hs);
|
||||
log.info("get DeviceInfo: {}", info);
|
||||
status = DeviceTmkStatus.key_error;
|
||||
} catch (Exception e) {
|
||||
log.warn("check device connect error: {}:{}", device.getServiceIp(), device.getServicePort(), e);
|
||||
hasError = true;
|
||||
}
|
||||
if(hasError) {
|
||||
// update status
|
||||
return;
|
||||
}
|
||||
try {
|
||||
EccPubKey pubKey = sdfApi.exportEncPublicKeyECC(hs, device.getEncKeyIdx());
|
||||
status = DeviceTmkStatus.key_error;
|
||||
} catch (Exception e) {
|
||||
log.warn("check device connect error: {}:{}", device.getServiceIp(), device.getServicePort(), e);
|
||||
hasError = true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
byte[] rk = sdfApi.generateRandom(hs, 16);
|
||||
EccPubKey pubKey = sdfApi.exportEncPublicKeyECC(hs, device.getEncKeyIdx());
|
||||
EccCipher cipher = sdfApi.externalEncryptECC(hs, pubKey, rk);
|
||||
sdfApi.getPrivateKeyAccessRight(hs, device.getEncKeyIdx(), device.getAccessCredentials().getBytes());
|
||||
String hk = sdfApi.importKeyWithISKECC(hs, device.getEncKeyIdx(), cipher);
|
||||
byte[] encrk = sdfApi.symEncrypt(hs, hk, AlgId.SGD_SM4_ECB, new byte[0], rk);
|
||||
byte[] prk = sdfApi.symDecrypt(hs, hk, AlgId.SGD_SM4_ECB, new byte[0], encrk);
|
||||
Assert.isTrue(Arrays.equals(rk, prk), "密码机加解密异常");
|
||||
|
||||
sdfApi.destroyKey(hs, hk);
|
||||
sdfApi.closeSession(hs);
|
||||
sdfApi.closeDevice(hd);
|
||||
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user