sdf 接口

This commit is contained in:
liulu 2024-11-05 09:46:57 +08:00
parent db484a070d
commit 87fcf9c0f1
2 changed files with 11 additions and 18 deletions

View File

@ -6,6 +6,7 @@ import com.sunyard.chsm.sdf.lib.SdfLibrary;
import com.sunyard.chsm.sdf.model.DeviceInfo;
import com.sunyard.chsm.sdf.model.EccKey;
import com.sunyard.chsm.sdf.model.EccPubKey;
import lombok.RequiredArgsConstructor;
import org.springframework.util.Assert;
import java.util.Map;
@ -16,21 +17,21 @@ import java.util.concurrent.ConcurrentHashMap;
* @author liulu
* @since 2024/11/4
*/
@RequiredArgsConstructor
public abstract class JnaSdfAdaptor implements SdfApiAdapter {
protected static final Map<String, Pointer> DEVICE_HANDLE_CONTEXT = new ConcurrentHashMap<>();
protected static final Map<String, Pointer> SESSION_HANDLE_CONTEXT = new ConcurrentHashMap<>();
protected static final Map<String, Pointer> KEY_HANDLE_CONTEXT = new ConcurrentHashMap<>();
protected abstract SdfLibrary getSdfLibrary();
protected final SdfLibrary sdfLibrary;
protected abstract int getAlgId(String alg);
@Override
public String openDevice() {
PointerByReference phDeviceHandle = new PointerByReference();
getSdfLibrary().SDF_OpenDevice(phDeviceHandle);
sdfLibrary.SDF_OpenDevice(phDeviceHandle);
String key = UUID.randomUUID().toString();
DEVICE_HANDLE_CONTEXT.put(key, phDeviceHandle.getValue());
return key;
@ -40,7 +41,7 @@ public abstract class JnaSdfAdaptor implements SdfApiAdapter {
public boolean closeDevice(String deviceHandle) {
Pointer pointer = DEVICE_HANDLE_CONTEXT.remove(deviceHandle);
if (pointer != null) {
int res = getSdfLibrary().SDF_CloseDevice(pointer);
int res = sdfLibrary.SDF_CloseDevice(pointer);
return res == 0;
}
return true;
@ -51,7 +52,7 @@ public abstract class JnaSdfAdaptor implements SdfApiAdapter {
Pointer device = getDeviceHandle(deviceHandle);
PointerByReference phSessionHandle = new PointerByReference();
getSdfLibrary().SDF_OpenSession(device, phSessionHandle);
sdfLibrary.SDF_OpenSession(device, phSessionHandle);
String key = UUID.randomUUID().toString();
SESSION_HANDLE_CONTEXT.put(key, phSessionHandle.getValue());
@ -62,7 +63,7 @@ public abstract class JnaSdfAdaptor implements SdfApiAdapter {
public void closeSession(String sessionHandle) {
Pointer pointer = SESSION_HANDLE_CONTEXT.remove(sessionHandle);
if (pointer != null) {
getSdfLibrary().SDF_CloseSession(pointer);
sdfLibrary.SDF_CloseSession(pointer);
}
}
@ -93,7 +94,7 @@ public abstract class JnaSdfAdaptor implements SdfApiAdapter {
public byte[] generateRandom(String sessionHandle, int uiLength) {
byte[] pucRandom = new byte[uiLength];
Pointer hSessionHandle = getSessionHandle(sessionHandle);
getSdfLibrary().SDF_GenerateRandom(hSessionHandle, uiLength, pucRandom);
sdfLibrary.SDF_GenerateRandom(hSessionHandle, uiLength, pucRandom);
return pucRandom;
}

View File

@ -2,7 +2,6 @@ package com.sunyard.chsm.sdf.adapter;
import com.sun.jna.Native;
import com.sun.jna.ptr.PointerByReference;
import com.sunyard.chsm.sdf.lib.SdfLibrary;
import com.sunyard.chsm.sdf.lib.SunyardSdfLibrary;
import java.util.Map;
@ -20,7 +19,6 @@ public class SunyardJnaSdfAdaptor extends JnaSdfAdaptor {
private final String ip;
private final Integer port;
private final String libName;
private final Integer connTimeout;
private final Integer dealTimeout;
@ -33,29 +31,23 @@ public class SunyardJnaSdfAdaptor extends JnaSdfAdaptor {
}
public SunyardJnaSdfAdaptor(String ip, int port, String libName, int connTimeout, int dealTimeout) {
super(SDF_LIB_MAP.computeIfAbsent(libName, k -> Native.load(libName, SunyardSdfLibrary.class)));
this.ip = ip;
this.port = port;
this.libName = libName;
this.connTimeout = connTimeout;
this.dealTimeout = dealTimeout;
SDF_LIB_MAP.computeIfAbsent(libName, k -> Native.load(libName, SunyardSdfLibrary.class));
}
@Override
public String openDevice() {
SunyardSdfLibrary sdfLibrary = (SunyardSdfLibrary) getSdfLibrary();
SunyardSdfLibrary sunyardSdfLibrary = (SunyardSdfLibrary) sdfLibrary;
PointerByReference phDeviceHandle = new PointerByReference();
sdfLibrary.SDF_OpenDevice(phDeviceHandle, safeStringBytes(ip), port, connTimeout, dealTimeout, 0);
sunyardSdfLibrary.SDF_OpenDevice(phDeviceHandle, safeStringBytes(ip), port, connTimeout, dealTimeout, 0);
String key = UUID.randomUUID().toString();
DEVICE_HANDLE_CONTEXT.put(key, phDeviceHandle.getValue());
return key;
}
@Override
protected SdfLibrary getSdfLibrary() {
return SDF_LIB_MAP.get(libName);
}
@Override
protected int getAlgId(String alg) {