init web
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package com.sunyard.chsm.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @since 2024/10/16
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum ApiFunEnum {
|
||||
|
||||
sym_enc(ApiGroupEnum.SYM_API,"sym_enc", "对称加密"),
|
||||
sym_dec(ApiGroupEnum.ASYM_API,"sym_dec", "对称解密"),
|
||||
cal_hmac(ApiGroupEnum.SYM_API,"cal_hmac", "计算Hmac"),
|
||||
check_hmac(ApiGroupEnum.ASYM_API,"check_hmac", "验证Hmac"),
|
||||
cal_mac(ApiGroupEnum.SYM_API,"cal_mac", "计算mac"),
|
||||
check_mac(ApiGroupEnum.ASYM_API,"check_mac", "验证mac"),
|
||||
gen_random(ApiGroupEnum.SYM_API,"gen_random", "生成随机数"),
|
||||
|
||||
asym_enc(ApiGroupEnum.ASYM_API,"asym_enc", "非对称加密"),
|
||||
asym_dec(ApiGroupEnum.ASYM_API,"asym_dec", "非对称解密"),
|
||||
sign_raw(ApiGroupEnum.ASYM_API,"sign_raw", "RAW签名"),
|
||||
verify_raw(ApiGroupEnum.ASYM_API,"verify_raw", "RAW验签"),
|
||||
sign_p1(ApiGroupEnum.ASYM_API,"sign_p1", "P1签名"),
|
||||
verify_p1(ApiGroupEnum.ASYM_API,"verify_p1", "P1验签"),
|
||||
sign_P7Attach(ApiGroupEnum.ASYM_API,"sign_P7Attach", "P7 Attach签名"),
|
||||
verify_P7Attach(ApiGroupEnum.ASYM_API,"verify_P7Attach", "P7 Attach验签"),
|
||||
sign_P7Detach(ApiGroupEnum.ASYM_API,"sign_P7Detach", "P7 Detach签名"),
|
||||
verify_P7Detach(ApiGroupEnum.ASYM_API,"verify_P7Detach", "P7 Detach验签"),
|
||||
;
|
||||
|
||||
private final ApiGroupEnum group;
|
||||
private final String code;
|
||||
private final String name;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.sunyard.chsm.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @since 2024/10/16
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum ApiGroupEnum {
|
||||
|
||||
SYM_API("sym_api", "对称密钥计算接口"),
|
||||
ASYM_API("asym_api", "非对称密钥计算接口"),
|
||||
;
|
||||
|
||||
private final String code;
|
||||
private final String name;
|
||||
|
||||
public static ApiGroupEnum of(String code) {
|
||||
if (code == null || code.trim().isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Arrays.stream(ApiGroupEnum.values())
|
||||
.filter(it -> Objects.equals(it.code, code))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.sunyard.chsm.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @since 2024/10/16
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum EnableStatus {
|
||||
|
||||
ENABLED("enabled", "启用"),
|
||||
DISABLED("disabled", "停用"),
|
||||
;
|
||||
|
||||
private final String code;
|
||||
private final String desc;
|
||||
|
||||
public static EnableStatus of(String code) {
|
||||
if (code == null || code.trim().isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Arrays.stream(EnableStatus.values())
|
||||
.filter(it -> Objects.equals(it.code, code))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.sunyard.chsm.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @since 2024/10/22
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum KeyAlg {
|
||||
|
||||
SM4(KeyCategory.SYM_KEY, "SM4", "国密SM4"),
|
||||
SM2(KeyCategory.ASYM_KEY, "SM2", "国密SM2"),
|
||||
;
|
||||
private final KeyCategory category;
|
||||
private final String code;
|
||||
private final String desc;
|
||||
|
||||
|
||||
public static KeyAlg of(String code) {
|
||||
if (code == null || code.trim().isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Arrays.stream(KeyAlg.values())
|
||||
.filter(it -> Objects.equals(it.code, code))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.sunyard.chsm.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @since 2024/10/22
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum KeyCategory {
|
||||
|
||||
// 对称密钥
|
||||
SYM_KEY("sym_key", "对称密钥"),
|
||||
// 非对称密钥
|
||||
ASYM_KEY("asym_key", "非对称密钥"),
|
||||
;
|
||||
|
||||
private final String code;
|
||||
private final String desc;
|
||||
|
||||
public static KeyCategory of(String code) {
|
||||
if (code == null || code.trim().isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Arrays.stream(KeyCategory.values())
|
||||
.filter(it -> Objects.equals(it.code, code))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.sunyard.chsm.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @since 2024/10/23
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum KeyStatus {
|
||||
|
||||
ENABLED("enabled", "启用中"),
|
||||
DISABLED("disabled", "已禁用"),
|
||||
ARCHIVED("archived", "已归档"),
|
||||
DELETED("deleted", "已销毁"),
|
||||
;
|
||||
private final String code;
|
||||
private final String desc;
|
||||
|
||||
public static KeyStatus of(String code) {
|
||||
if (code == null || code.trim().isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Arrays.stream(KeyStatus.values())
|
||||
.filter(it -> Objects.equals(it.code, code))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.sunyard.chsm.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @since 2024/10/22
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum KeyUsage {
|
||||
|
||||
// 加密解密
|
||||
ENCRYPT_DECRYPT("encrypt_decrypt", "加密解密", 1),
|
||||
// 签名验签
|
||||
SIGN_VERIFY("sign_verify", "签名验签", 2),
|
||||
// 计算HMac
|
||||
HMAC("hmac", "计算HMac", 4),
|
||||
// 计算Mac
|
||||
MAC("mac", "计算Mac", 8),
|
||||
|
||||
;
|
||||
private final String code;
|
||||
private final String desc;
|
||||
private final int value;
|
||||
|
||||
|
||||
public static int getStatus(List<String> codes) {
|
||||
if (CollectionUtils.isEmpty(codes)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return codes.stream()
|
||||
.map(String::toUpperCase)
|
||||
.map(KeyUsage::valueOf)
|
||||
.mapToInt(KeyUsage::getValue)
|
||||
.reduce(0, (l, r) -> l | r);
|
||||
}
|
||||
|
||||
public static List<KeyUsage> getUsage(int status) {
|
||||
if (status == 0) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
return Arrays.stream(KeyUsage.values())
|
||||
.filter(it -> hasUsage(status, it))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
||||
public static boolean hasUsage(int nowUsage, KeyUsage usage) {
|
||||
return (nowUsage & usage.value) == 1;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.sunyard.chsm.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @since 2024/10/17
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum ManufacturerEnum {
|
||||
|
||||
SUNYARD("sunyard", "信雅达"),
|
||||
;
|
||||
|
||||
private final String code;
|
||||
private final String name;
|
||||
|
||||
public static ManufacturerEnum of(String code) {
|
||||
if (code == null || code.trim().isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Arrays.stream(ManufacturerEnum.values())
|
||||
.filter(it -> Objects.equals(it.code, code))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.sunyard.chsm.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @since 2024/10/17
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum ManufacturerModelEnum {
|
||||
|
||||
enc001(ManufacturerEnum.SUNYARD, "enc001", "服务器密码机enc001"),
|
||||
;
|
||||
|
||||
private final ManufacturerEnum manufacturer;
|
||||
private final String model;
|
||||
private final String name;
|
||||
|
||||
public static ManufacturerModelEnum of(String code) {
|
||||
if (code == null || code.trim().isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Arrays.stream(ManufacturerModelEnum.values())
|
||||
.filter(it -> Objects.equals(it.model, code))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.sunyard.chsm.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.sunyard.chsm.model.entity.CryptoServiceApi;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @since 2024/10/15
|
||||
*/
|
||||
@Mapper
|
||||
public interface CryptoServiceApiMapper extends BaseMapper<CryptoServiceApi> {
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.sunyard.chsm.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.sunyard.chsm.model.entity.CryptoService;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @since 2024/10/15
|
||||
*/
|
||||
@Mapper
|
||||
public interface CryptoServiceMapper extends BaseMapper<CryptoService> {
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.sunyard.chsm.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.sunyard.chsm.model.entity.KeyInfo;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @since 2024/10/22
|
||||
*/
|
||||
@Mapper
|
||||
public interface KeyInfoMapper extends BaseMapper<KeyInfo> {
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.sunyard.chsm.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.sunyard.chsm.model.entity.KeyTemplate;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @since 2024/10/22
|
||||
*/
|
||||
@Mapper
|
||||
public interface KeyTemplateMapper extends BaseMapper<KeyTemplate> {
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.sunyard.chsm.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.sunyard.chsm.model.entity.DeviceGroup;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @since 2024/10/17
|
||||
*/
|
||||
@Mapper
|
||||
public interface SpDeviceGroupMapper extends BaseMapper<DeviceGroup> {
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.sunyard.chsm.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.sunyard.chsm.model.entity.Device;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @since 2024/10/17
|
||||
*/
|
||||
@Mapper
|
||||
public interface SpDeviceMapper extends BaseMapper<Device> {
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.sunyard.chsm.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.sunyard.chsm.model.entity.KeyRecord;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @since 2024/10/22
|
||||
*/
|
||||
@Mapper
|
||||
public interface SpKeyRecordMapper extends BaseMapper<KeyRecord> {
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.sunyard.chsm.model;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @since 2024/10/16
|
||||
*/
|
||||
@Getter
|
||||
@NoArgsConstructor
|
||||
public class PageQuery {
|
||||
|
||||
public static final int MAX_PAGE_SIZE = 200;
|
||||
|
||||
/**
|
||||
* 当前页
|
||||
*/
|
||||
private int pageNumber = 1;
|
||||
/**
|
||||
* 每页大小
|
||||
*/
|
||||
private int pageSize = 10;
|
||||
|
||||
|
||||
public PageQuery(int pageNum, int pageSize) {
|
||||
this.pageNumber = pageNum;
|
||||
this.pageSize = pageSize;
|
||||
}
|
||||
|
||||
public void setPageNumber(int pageNum) {
|
||||
this.pageNumber = Math.max(1, pageNum);
|
||||
}
|
||||
|
||||
public void setPageSize(int pageSize) {
|
||||
this.pageSize = Math.min(MAX_PAGE_SIZE, Math.max(1, pageSize));
|
||||
}
|
||||
|
||||
public int getOffset() {
|
||||
return pageSize * (pageNumber - 1);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package com.sunyard.chsm.model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @since 2024/10/17
|
||||
*/
|
||||
@Data
|
||||
public class R<T> {
|
||||
|
||||
/**
|
||||
* 成功标志
|
||||
*/
|
||||
private boolean success;
|
||||
|
||||
/**
|
||||
* 失败消息
|
||||
*/
|
||||
private String message;
|
||||
|
||||
/**
|
||||
* 返回代码 200-代表成功,
|
||||
*/
|
||||
private Integer code;
|
||||
|
||||
/**
|
||||
* 时间戳
|
||||
*/
|
||||
private long timestamp = System.currentTimeMillis();
|
||||
|
||||
/**
|
||||
* 结果对象
|
||||
*/
|
||||
private T result;
|
||||
|
||||
|
||||
public static <T> R<T> ok() {
|
||||
R<T> r = new R<>();
|
||||
r.setSuccess(true);
|
||||
r.setMessage("success");
|
||||
r.setCode(200);
|
||||
return r;
|
||||
}
|
||||
|
||||
public static <T> R<T> ok(String msg) {
|
||||
R<T> r = new R<>();
|
||||
r.setSuccess(true);
|
||||
r.setMessage(msg);
|
||||
r.setCode(200);
|
||||
return r;
|
||||
}
|
||||
|
||||
public static <T> R<T> data(T value) {
|
||||
R<T> r = new R<>();
|
||||
r.setSuccess(true);
|
||||
r.setResult(value);
|
||||
r.setCode(200);
|
||||
return r;
|
||||
}
|
||||
|
||||
public static <T> R<T> data(T value, String msg) {
|
||||
R<T> r = new R<>();
|
||||
r.setSuccess(true);
|
||||
r.setResult(value);
|
||||
r.setMessage(msg);
|
||||
r.setCode(200);
|
||||
return r;
|
||||
}
|
||||
|
||||
public static <T> R<T> error(String msg) {
|
||||
R<T> r = new R<>();
|
||||
r.setSuccess(false);
|
||||
r.setMessage(msg);
|
||||
r.setCode(500);
|
||||
return r;
|
||||
}
|
||||
|
||||
public static <T> R<T> error(int code, String msg) {
|
||||
R<T> r = new R<>();
|
||||
r.setSuccess(false);
|
||||
r.setMessage(msg);
|
||||
r.setCode(code);
|
||||
return r;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.sunyard.chsm.model.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @since 2024/10/15
|
||||
*/
|
||||
@Data
|
||||
@TableName("sp_crypto_service")
|
||||
public class CryptoService {
|
||||
|
||||
private Long id;
|
||||
private String name;
|
||||
private Long deviceGroupId;
|
||||
private String deviceGroupName;
|
||||
private String status;
|
||||
private Long creatorId;
|
||||
|
||||
private String remark;
|
||||
private LocalDateTime createTime;
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.sunyard.chsm.model.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @since 2024/10/15
|
||||
*/
|
||||
@Data
|
||||
@TableName("sp_crypto_service_api")
|
||||
public class CryptoServiceApi {
|
||||
|
||||
private Long id;
|
||||
private Long cryptoServiceId;
|
||||
private String apiGroup;
|
||||
private String apiCode;
|
||||
private String apiName;
|
||||
|
||||
private String remark;
|
||||
private LocalDateTime createTime;
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.sunyard.chsm.model.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @since 2024/10/17
|
||||
*/
|
||||
@Data
|
||||
@TableName("sp_device")
|
||||
public class Device {
|
||||
|
||||
private Long id;
|
||||
private String name;
|
||||
private String serviceIp;
|
||||
private Integer servicePort;
|
||||
private String manageIp;
|
||||
private Integer managePort;
|
||||
private String manufacturer;
|
||||
private String manufacturerModel;
|
||||
private String accessCredentials;
|
||||
|
||||
private Long groupId;
|
||||
private String groupName;
|
||||
private Integer weight;
|
||||
|
||||
private String remark;
|
||||
private LocalDateTime createTime;
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.sunyard.chsm.model.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @since 2024/10/17
|
||||
*/
|
||||
@Data
|
||||
@TableName("sp_device_group")
|
||||
public class DeviceGroup {
|
||||
|
||||
private Long id;
|
||||
private String name;
|
||||
|
||||
private String remark;
|
||||
private LocalDateTime createTime;
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.sunyard.chsm.model.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @since 2024/10/22
|
||||
*/
|
||||
@Data
|
||||
@TableName("sp_key_info")
|
||||
public class KeyInfo {
|
||||
|
||||
private Long id;
|
||||
private Long applicationId;
|
||||
private Long keyTemplateId;
|
||||
private String code;
|
||||
private String keyType;
|
||||
private String keyAlg;
|
||||
private Integer keyLength;
|
||||
private Integer keyUsage;
|
||||
private String checkAlg;
|
||||
private String checkValue;
|
||||
private String status;
|
||||
private LocalDateTime effectiveTime;
|
||||
private LocalDateTime expiredTime;
|
||||
|
||||
private String remark;
|
||||
private LocalDateTime createTime;
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.sunyard.chsm.model.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @since 2024/10/22
|
||||
*/
|
||||
@Data
|
||||
@TableName("sp_key_record")
|
||||
public class KeyRecord {
|
||||
|
||||
private Long id;
|
||||
private Long keyId;
|
||||
|
||||
private String keyIndex;
|
||||
private String keyData;
|
||||
private String pubKey;
|
||||
private String checkAlg;
|
||||
private String checkValue;
|
||||
private LocalDateTime effectiveTime;
|
||||
private LocalDateTime expiredTime;
|
||||
|
||||
private String remark;
|
||||
private LocalDateTime createTime;
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.sunyard.chsm.model.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @since 2024/10/22
|
||||
*/
|
||||
@Data
|
||||
@TableName("sp_key_template")
|
||||
public class KeyTemplate {
|
||||
|
||||
private Long id;
|
||||
private String code;
|
||||
private String name;
|
||||
private String keyType;
|
||||
private String keyAlg;
|
||||
private Integer keyLength;
|
||||
private Integer keyUsage;
|
||||
private String checkAlg;
|
||||
private Integer validTime;
|
||||
private String validUnit;
|
||||
private Integer startAfterCreateTime;
|
||||
private String startAfterCreateUnit;
|
||||
|
||||
private String remark;
|
||||
private LocalDateTime createTime;
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package com.sunyard.chsm.sdf;
|
||||
|
||||
|
||||
import com.sunyard.chsm.sdf.model.EccKey;
|
||||
import com.sunyard.chsm.sdf.model.EccPriKey;
|
||||
import com.sunyard.chsm.sdf.model.EccPubKey;
|
||||
import lombok.SneakyThrows;
|
||||
import org.bouncycastle.asn1.gm.GMNamedCurves;
|
||||
import org.bouncycastle.asn1.gm.GMObjectIdentifiers;
|
||||
import org.bouncycastle.asn1.x9.X9ECParameters;
|
||||
import org.bouncycastle.crypto.digests.SM3Digest;
|
||||
import org.bouncycastle.crypto.macs.HMac;
|
||||
import org.bouncycastle.crypto.params.KeyParameter;
|
||||
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey;
|
||||
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey;
|
||||
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||
import org.bouncycastle.jce.spec.ECParameterSpec;
|
||||
import org.bouncycastle.util.BigIntegers;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.security.KeyPair;
|
||||
import java.security.KeyPairGenerator;
|
||||
import java.security.SecureRandom;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @since 2024/10/23
|
||||
*/
|
||||
@Service
|
||||
public class BCSdfApiService implements SdfApiService {
|
||||
|
||||
|
||||
@Override
|
||||
public byte[] generateRandom(int len) {
|
||||
byte[] res = new byte[len];
|
||||
new SecureRandom().nextBytes(res);
|
||||
return res;
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public EccKey genKeyPairEcc() {
|
||||
|
||||
// 获取SM2参数
|
||||
X9ECParameters sm2Params = GMNamedCurves.getByOID(GMObjectIdentifiers.sm2p256v1);
|
||||
ECParameterSpec sm2Spec = new ECParameterSpec(sm2Params.getCurve(), sm2Params.getG(), sm2Params.getN(), sm2Params.getH());
|
||||
|
||||
// 创建密钥对生成器
|
||||
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC", BouncyCastleProvider.PROVIDER_NAME);
|
||||
keyPairGenerator.initialize(sm2Spec);
|
||||
|
||||
// 生成密钥对
|
||||
KeyPair keyPair = keyPairGenerator.generateKeyPair();
|
||||
BCECPublicKey pubKey = (BCECPublicKey) keyPair.getPublic();
|
||||
BCECPrivateKey priKey = (BCECPrivateKey) keyPair.getPrivate();
|
||||
byte[] x = pubKey.getQ().getXCoord().getEncoded();
|
||||
byte[] y = pubKey.getQ().getYCoord().getEncoded();
|
||||
byte[] d = BigIntegers.asUnsignedByteArray(64, priKey.getD());
|
||||
|
||||
return new EccKey(new EccPubKey(256, x, y), new EccPriKey(256, d));
|
||||
}
|
||||
|
||||
@Override
|
||||
public EccKey genEccKeyPairAndEncByKek() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] calculateMAC(byte[] symKey, byte[] pucIv, byte[] pucData) {
|
||||
return new byte[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] hmac(byte[] key, byte[] srcData) {
|
||||
KeyParameter keyParameter = new KeyParameter(key);
|
||||
SM3Digest digest = new SM3Digest();
|
||||
HMac mac = new HMac(digest);
|
||||
mac.init(keyParameter);
|
||||
mac.update(srcData, 0, srcData.length);
|
||||
byte[] result = new byte[mac.getMacSize()];
|
||||
mac.doFinal(result, 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] hash(byte[] pucData) {
|
||||
SM3Digest digest = new SM3Digest();
|
||||
digest.update(pucData, 0, pucData.length);
|
||||
byte[] hash = new byte[digest.getDigestSize()];
|
||||
digest.doFinal(hash, 0);
|
||||
return hash;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.sunyard.chsm.sdf;
|
||||
|
||||
|
||||
import com.sunyard.chsm.sdf.model.EccKey;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @since 2024/10/23
|
||||
*/
|
||||
public interface SdfApiService {
|
||||
|
||||
|
||||
/**
|
||||
* 产生随机数
|
||||
*
|
||||
* @param len 随机数长度
|
||||
* @return 返回随机数
|
||||
*/
|
||||
byte[] generateRandom(int len);
|
||||
|
||||
/**
|
||||
* 产生ECC密钥对并输出
|
||||
*
|
||||
* @return pubKey 返回公钥 | priKey 返回私钥
|
||||
*/
|
||||
EccKey genKeyPairEcc();
|
||||
|
||||
/**
|
||||
* 产生ECC加密密钥对并使用kek加密私钥输出
|
||||
*
|
||||
* @return pubKey 返回公钥 | priKey 返回私钥
|
||||
*/
|
||||
EccKey genEccKeyPairAndEncByKek();
|
||||
|
||||
|
||||
/**
|
||||
* 计算MAC
|
||||
*
|
||||
* @param symKey 用户指定的密钥
|
||||
* @param pucIv 缓冲区指针,用于存放输入和返回的IV数据
|
||||
* @param pucData 缓冲区指针,用于存放输入的数据明文
|
||||
* @return pucEncData 返回MAC值 | puiLength 返回MAC值长度
|
||||
*/
|
||||
byte[] calculateMAC(byte[] symKey, byte[] pucIv, byte[] pucData);
|
||||
|
||||
byte[] hmac(byte[] key, byte[] srcData);
|
||||
|
||||
/**
|
||||
* 多包杂凑运算
|
||||
*
|
||||
* @param pucData 缓冲区指针,用于存放输入的数据明文
|
||||
* @return hash值
|
||||
*/
|
||||
byte[] hash(byte[] pucData);
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.sunyard.chsm.sdf.model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @version V1.0
|
||||
* @since 2022/10/11
|
||||
*/
|
||||
@Data
|
||||
public class DeviceInfo {
|
||||
|
||||
private String issuerName;
|
||||
private String deviceName;
|
||||
private String deviceSerial;
|
||||
private int deviceVersion;
|
||||
private int standardVersion;
|
||||
private int[] asymAlgAbility;
|
||||
private int symAlgAbility;
|
||||
private int hashAlgAbility;
|
||||
private int bufferSize;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.sunyard.chsm.sdf.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @version V1.0
|
||||
* @since 2022/11/11
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class EccCipher {
|
||||
|
||||
// X 分量
|
||||
private byte[] x;
|
||||
// Y 分量
|
||||
private byte[] y;
|
||||
// 明文的 SM3 杂凑值
|
||||
private byte[] M;
|
||||
// 密文数据长度
|
||||
private int L;
|
||||
// 密文数据
|
||||
private byte[] C;
|
||||
|
||||
|
||||
public byte[] getC1C3C2Bytes() {
|
||||
int xLen = x.length;
|
||||
int yLen = y.length;
|
||||
int mLen = M.length;
|
||||
|
||||
byte[] rawData = new byte[xLen + yLen + mLen + L];
|
||||
|
||||
System.arraycopy(x, 0, rawData, 0, xLen);
|
||||
System.arraycopy(y, 0, rawData, xLen, yLen);
|
||||
System.arraycopy(M, 0, rawData, xLen + yLen, mLen);
|
||||
System.arraycopy(C, 0, rawData, xLen + yLen + mLen, L);
|
||||
|
||||
return rawData;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.sunyard.chsm.sdf.model;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @version V1.0
|
||||
* @since 2022/11/11
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class EccKey {
|
||||
|
||||
private EccPubKey pubKey;
|
||||
private EccPriKey priKey;
|
||||
|
||||
public EccKey(EccPubKey pubKey, EccPriKey priKey) {
|
||||
this.pubKey = pubKey;
|
||||
this.priKey = priKey;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.sunyard.chsm.sdf.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @version V1.0
|
||||
* @since 2022/11/11
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class EccPriKey {
|
||||
// 密 钥 位 长
|
||||
private int bits;
|
||||
// 私 钥
|
||||
private byte[] D;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.sunyard.chsm.sdf.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @version V1.0
|
||||
* @since 2022/11/11
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class EccPubKey {
|
||||
// 密 钥 位 长
|
||||
private int bits;
|
||||
// 公 钥 x 坐 标
|
||||
private byte[] x;
|
||||
// 公 钥 y 坐 标
|
||||
private byte[] y;
|
||||
|
||||
|
||||
public byte[] getPubKeyBytes() {
|
||||
byte[] rawKey = new byte[x.length + y.length];
|
||||
System.arraycopy(x, 0, rawKey, 0, x.length);
|
||||
System.arraycopy(y, 0, rawKey, x.length, y.length);
|
||||
return rawKey;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.sunyard.chsm.sdf.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @version V1.0
|
||||
* @since 2022/11/11
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class EccSignature {
|
||||
|
||||
// 签名的 r 部分
|
||||
private byte[] r;
|
||||
|
||||
// 签名的 s 部分
|
||||
private byte[] s;
|
||||
|
||||
public byte[] getRawSignBytes() {
|
||||
byte[] signData = new byte[r.length + s.length];
|
||||
System.arraycopy(r, 0, signData, 0, r.length);
|
||||
System.arraycopy(s, 0, signData, r.length, s.length);
|
||||
return signData;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.sunyard.chsm.utils;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @version V1.0
|
||||
* @since 2022/12/6
|
||||
*/
|
||||
public abstract class DateFormatPattern {
|
||||
|
||||
public static final String YYYY = "yyyy";
|
||||
public static final String YYYYMMDD = "yyyyMMdd";
|
||||
public static final String YYYY_MM_DD = "yyyy-MM-dd";
|
||||
public static final String YYYYIMMIDD = "yyyy/MM/dd";
|
||||
public static final String YYYYMMDDHHMM = "yyyyMMddHHmm";
|
||||
public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
|
||||
public static final String YYYYMMDD_HHMMSS = "yyyyMMdd HHmmss";
|
||||
public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
|
||||
public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
|
||||
public static final String YYYYMMDDHHMMSS_SS = "yyyyMMddHHmmssSSS";
|
||||
public static final String HHMMSS_TIME_FORMAT = "HHmmss";
|
||||
public static final String YYYY_MM_DD_T_HH_MM_SS_Z = "yyyy-MM-dd'T'HH:mm:ss'Z'";
|
||||
public static final String YYYY_MM_DD_HH24_MM_SS = "yyyy-MM-d hh24:mm:ss";
|
||||
public static final String HH_MM = "HH:mm";
|
||||
public static final String HHMM = "HHmm";
|
||||
public static final String HH_MM_SS = "HH:mm:ss";
|
||||
public static final String ZERO_HH_MM_SS = " 00:00:00";
|
||||
public static final String YYMMDD = "yyMMdd";
|
||||
public static final String YYYY_CN_MM_CN_DD_CN = "yyyy年 MM 月 dd 日";
|
||||
public static final String MM_CN_DD_CN = "MM 月 dd 日";
|
||||
public static final String BEGIN_OF_DAY = "yyyy-MM-dd 00:00:00";
|
||||
public static final String END_OF_DAY = "yyyy-MM-dd 23:59:59";
|
||||
public static final String YYYYIMMIDDHHMMSS = "yyyy/MM/dd HH:mm:ss";
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package com.sunyard.chsm.utils;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
|
||||
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
|
||||
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
|
||||
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
|
||||
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
|
||||
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @version V1.0
|
||||
* @since 2020/11/25
|
||||
*/
|
||||
public abstract class JsonUtils {
|
||||
|
||||
private JsonUtils() {
|
||||
}
|
||||
|
||||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
||||
|
||||
static {
|
||||
JavaTimeModule javaTimeModule = new JavaTimeModule();
|
||||
javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DateFormatPattern.HH_MM_SS)));
|
||||
javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DateFormatPattern.YYYY_MM_DD)));
|
||||
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DateFormatPattern.YYYY_MM_DD_HH_MM_SS)));
|
||||
|
||||
javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DateFormatPattern.HH_MM_SS)));
|
||||
javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DateFormatPattern.YYYY_MM_DD)));
|
||||
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DateFormatPattern.YYYY_MM_DD_HH_MM_SS)));
|
||||
|
||||
OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
|
||||
OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
OBJECT_MAPPER.registerModules(javaTimeModule);
|
||||
}
|
||||
|
||||
public static ObjectMapper objectMapper() {
|
||||
return OBJECT_MAPPER;
|
||||
}
|
||||
|
||||
public static String toJsonString(Object obj) {
|
||||
try {
|
||||
return OBJECT_MAPPER.writeValueAsString(obj);
|
||||
} catch (JsonProcessingException e) {
|
||||
//
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] toJsonBytes(Object obj) {
|
||||
try {
|
||||
return OBJECT_MAPPER.writeValueAsBytes(obj);
|
||||
} catch (JsonProcessingException e) {
|
||||
//
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static <T> T parse(String jsonStr, Class<T> type) {
|
||||
if (jsonStr == null || jsonStr.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return OBJECT_MAPPER.readValue(jsonStr, type);
|
||||
} catch (JsonProcessingException e) {
|
||||
//
|
||||
throw new IllegalArgumentException("json格式错误");
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T parse(byte[] json, Class<T> type) {
|
||||
if (json == null || json.length == 0) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return OBJECT_MAPPER.readValue(json, type);
|
||||
} catch (IOException e) {
|
||||
//
|
||||
throw new IllegalArgumentException("json格式错误");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.sunyard.chsm.utils;
|
||||
|
||||
/**
|
||||
* @author liulu
|
||||
* @version V1.0
|
||||
* @since 2020/11/16
|
||||
*/
|
||||
public abstract class ThrowableUtils {
|
||||
|
||||
private ThrowableUtils() {
|
||||
}
|
||||
|
||||
|
||||
public static String buildErrorMessage(Throwable ex) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(ex.getMessage());
|
||||
StackTraceElement[] stackTrace = ex.getStackTrace();
|
||||
int i = 0;
|
||||
for (StackTraceElement stackTraceElement : stackTrace) {
|
||||
if (i++ >= 5) {
|
||||
break;
|
||||
}
|
||||
sb.append("\n\t").append(stackTraceElement.toString());
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
public static <T extends Exception> T findException(Class<T> exClass, Throwable ex) {
|
||||
for (int i = 0; ex != null && i < 3; i++) {
|
||||
if (exClass.isAssignableFrom(ex.getClass())) {
|
||||
//noinspection unchecked
|
||||
return (T) ex;
|
||||
}
|
||||
ex = ex.getCause();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user