sdf 接口
This commit is contained in:
parent
db484a070d
commit
87fcf9c0f1
@ -6,6 +6,7 @@ import com.sunyard.chsm.sdf.lib.SdfLibrary;
|
|||||||
import com.sunyard.chsm.sdf.model.DeviceInfo;
|
import com.sunyard.chsm.sdf.model.DeviceInfo;
|
||||||
import com.sunyard.chsm.sdf.model.EccKey;
|
import com.sunyard.chsm.sdf.model.EccKey;
|
||||||
import com.sunyard.chsm.sdf.model.EccPubKey;
|
import com.sunyard.chsm.sdf.model.EccPubKey;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -16,21 +17,21 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||||||
* @author liulu
|
* @author liulu
|
||||||
* @since 2024/11/4
|
* @since 2024/11/4
|
||||||
*/
|
*/
|
||||||
|
@RequiredArgsConstructor
|
||||||
public abstract class JnaSdfAdaptor implements SdfApiAdapter {
|
public abstract class JnaSdfAdaptor implements SdfApiAdapter {
|
||||||
|
|
||||||
protected static final Map<String, Pointer> DEVICE_HANDLE_CONTEXT = new ConcurrentHashMap<>();
|
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> SESSION_HANDLE_CONTEXT = new ConcurrentHashMap<>();
|
||||||
protected static final Map<String, Pointer> KEY_HANDLE_CONTEXT = new ConcurrentHashMap<>();
|
protected static final Map<String, Pointer> KEY_HANDLE_CONTEXT = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
protected final SdfLibrary sdfLibrary;
|
||||||
protected abstract SdfLibrary getSdfLibrary();
|
|
||||||
|
|
||||||
protected abstract int getAlgId(String alg);
|
protected abstract int getAlgId(String alg);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String openDevice() {
|
public String openDevice() {
|
||||||
PointerByReference phDeviceHandle = new PointerByReference();
|
PointerByReference phDeviceHandle = new PointerByReference();
|
||||||
getSdfLibrary().SDF_OpenDevice(phDeviceHandle);
|
sdfLibrary.SDF_OpenDevice(phDeviceHandle);
|
||||||
String key = UUID.randomUUID().toString();
|
String key = UUID.randomUUID().toString();
|
||||||
DEVICE_HANDLE_CONTEXT.put(key, phDeviceHandle.getValue());
|
DEVICE_HANDLE_CONTEXT.put(key, phDeviceHandle.getValue());
|
||||||
return key;
|
return key;
|
||||||
@ -40,7 +41,7 @@ public abstract class JnaSdfAdaptor implements SdfApiAdapter {
|
|||||||
public boolean closeDevice(String deviceHandle) {
|
public boolean closeDevice(String deviceHandle) {
|
||||||
Pointer pointer = DEVICE_HANDLE_CONTEXT.remove(deviceHandle);
|
Pointer pointer = DEVICE_HANDLE_CONTEXT.remove(deviceHandle);
|
||||||
if (pointer != null) {
|
if (pointer != null) {
|
||||||
int res = getSdfLibrary().SDF_CloseDevice(pointer);
|
int res = sdfLibrary.SDF_CloseDevice(pointer);
|
||||||
return res == 0;
|
return res == 0;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -51,7 +52,7 @@ public abstract class JnaSdfAdaptor implements SdfApiAdapter {
|
|||||||
Pointer device = getDeviceHandle(deviceHandle);
|
Pointer device = getDeviceHandle(deviceHandle);
|
||||||
|
|
||||||
PointerByReference phSessionHandle = new PointerByReference();
|
PointerByReference phSessionHandle = new PointerByReference();
|
||||||
getSdfLibrary().SDF_OpenSession(device, phSessionHandle);
|
sdfLibrary.SDF_OpenSession(device, phSessionHandle);
|
||||||
|
|
||||||
String key = UUID.randomUUID().toString();
|
String key = UUID.randomUUID().toString();
|
||||||
SESSION_HANDLE_CONTEXT.put(key, phSessionHandle.getValue());
|
SESSION_HANDLE_CONTEXT.put(key, phSessionHandle.getValue());
|
||||||
@ -62,7 +63,7 @@ public abstract class JnaSdfAdaptor implements SdfApiAdapter {
|
|||||||
public void closeSession(String sessionHandle) {
|
public void closeSession(String sessionHandle) {
|
||||||
Pointer pointer = SESSION_HANDLE_CONTEXT.remove(sessionHandle);
|
Pointer pointer = SESSION_HANDLE_CONTEXT.remove(sessionHandle);
|
||||||
if (pointer != null) {
|
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) {
|
public byte[] generateRandom(String sessionHandle, int uiLength) {
|
||||||
byte[] pucRandom = new byte[uiLength];
|
byte[] pucRandom = new byte[uiLength];
|
||||||
Pointer hSessionHandle = getSessionHandle(sessionHandle);
|
Pointer hSessionHandle = getSessionHandle(sessionHandle);
|
||||||
getSdfLibrary().SDF_GenerateRandom(hSessionHandle, uiLength, pucRandom);
|
sdfLibrary.SDF_GenerateRandom(hSessionHandle, uiLength, pucRandom);
|
||||||
return pucRandom;
|
return pucRandom;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,7 +2,6 @@ package com.sunyard.chsm.sdf.adapter;
|
|||||||
|
|
||||||
import com.sun.jna.Native;
|
import com.sun.jna.Native;
|
||||||
import com.sun.jna.ptr.PointerByReference;
|
import com.sun.jna.ptr.PointerByReference;
|
||||||
import com.sunyard.chsm.sdf.lib.SdfLibrary;
|
|
||||||
import com.sunyard.chsm.sdf.lib.SunyardSdfLibrary;
|
import com.sunyard.chsm.sdf.lib.SunyardSdfLibrary;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -20,7 +19,6 @@ public class SunyardJnaSdfAdaptor extends JnaSdfAdaptor {
|
|||||||
|
|
||||||
private final String ip;
|
private final String ip;
|
||||||
private final Integer port;
|
private final Integer port;
|
||||||
private final String libName;
|
|
||||||
private final Integer connTimeout;
|
private final Integer connTimeout;
|
||||||
private final Integer dealTimeout;
|
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) {
|
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.ip = ip;
|
||||||
this.port = port;
|
this.port = port;
|
||||||
this.libName = libName;
|
|
||||||
this.connTimeout = connTimeout;
|
this.connTimeout = connTimeout;
|
||||||
this.dealTimeout = dealTimeout;
|
this.dealTimeout = dealTimeout;
|
||||||
SDF_LIB_MAP.computeIfAbsent(libName, k -> Native.load(libName, SunyardSdfLibrary.class));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String openDevice() {
|
public String openDevice() {
|
||||||
SunyardSdfLibrary sdfLibrary = (SunyardSdfLibrary) getSdfLibrary();
|
SunyardSdfLibrary sunyardSdfLibrary = (SunyardSdfLibrary) sdfLibrary;
|
||||||
PointerByReference phDeviceHandle = new PointerByReference();
|
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();
|
String key = UUID.randomUUID().toString();
|
||||||
DEVICE_HANDLE_CONTEXT.put(key, phDeviceHandle.getValue());
|
DEVICE_HANDLE_CONTEXT.put(key, phDeviceHandle.getValue());
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected SdfLibrary getSdfLibrary() {
|
|
||||||
return SDF_LIB_MAP.get(libName);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected int getAlgId(String alg) {
|
protected int getAlgId(String alg) {
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user