|
@@ -0,0 +1,103 @@
|
|
|
|
+package com.dragoninfo.dcuc.duceap.service.impl;
|
|
|
|
+
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
|
+import com.dragoninfo.dcuc.duceap.bpo.CodeGmBPO;
|
|
|
|
+import com.dragoninfo.dcuc.duceap.config.CodeGmConfig;
|
|
|
|
+import com.dragoninfo.dcuc.duceap.entity.CodeGmSign;
|
|
|
|
+import com.dragoninfo.dcuc.duceap.service.ICodeGmService;
|
|
|
|
+import com.dragonsoft.duceap.base.entity.http.ResponseDTO;
|
|
|
|
+import com.dragonsoft.duceap.base.entity.http.ResponseStatus;
|
|
|
|
+import com.dragonsoft.duceap.base.entity.metadata.CodeRecord;
|
|
|
|
+import com.dragonsoft.duceap.code.util.CodeInfoUtils;
|
|
|
|
+import com.dragonsoft.duceap.commons.util.string.StringUtils;
|
|
|
|
+import com.dragonsoft.duceap.core.search.Searchable;
|
|
|
|
+import com.dragonsoft.duceap.core.search.enums.SearchOperator;
|
|
|
|
+import com.dragonsoft.smtools.enums.SM3SignStrategy;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+
|
|
|
|
+import java.util.*;
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @author mazq
|
|
|
|
+ * @date 2021/5/10
|
|
|
|
+ */
|
|
|
|
+@Service
|
|
|
|
+public class CodeGmServiceImpl implements ICodeGmService {
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ CodeGmBPO codeGmBPO;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private SM3SignStrategy sm3SignStrategy;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private CodeGmConfig gmConfig;
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public ResponseDTO codeGmCheck(String codeId) {
|
|
|
|
+ if(!gmConfig.getCheckCodeIds().contains(codeId)){
|
|
|
|
+ return ResponseDTO.success(ResponseStatus.SUCCESS_CODE,"无需校验");
|
|
|
|
+ }
|
|
|
|
+ List<CodeRecord> codeRecords = CodeInfoUtils.getCodeListByCodeDicId(codeId);
|
|
|
|
+ List<CodeGmSign> signs = getCodeGmSignByCodeId(codeId);
|
|
|
|
+ Map<String, CodeGmSign> signMap = signs
|
|
|
|
+ .stream()
|
|
|
|
+ .collect(Collectors.toMap(i -> i.getCode(), i -> i, (oldOne, lastOne) -> lastOne));
|
|
|
|
+ for (CodeRecord codeRecord : codeRecords) {
|
|
|
|
+ String code = codeRecord.getValue();
|
|
|
|
+ CodeGmSign codeGmSign = signMap.get(code);
|
|
|
|
+ if(null == codeGmSign){
|
|
|
|
+ return ResponseDTO.fail(ResponseStatus.FAIL_CODE,"缺少国密签名数据",null);
|
|
|
|
+ }
|
|
|
|
+ String digest = codeGmSign.getDigest();
|
|
|
|
+ String origin = codeRecord.getLabel() + codeRecord.getValue();
|
|
|
|
+ String summary = sm3SignStrategy.summary(origin);
|
|
|
|
+ if(!summary.equals(digest)){
|
|
|
|
+ return ResponseDTO.fail(ResponseStatus.FAIL_CODE,"国密完整性异常",null);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return ResponseDTO.success(ResponseStatus.SUCCESS_CODE,"国密数据校验成功");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public ResponseDTO codeGmCheckCodes(String codeIds) {
|
|
|
|
+ if(StringUtils.isBlank(codeIds)){
|
|
|
|
+ return ResponseDTO.fail(ResponseStatus.FAIL_CODE,"codeIds为空",null);
|
|
|
|
+ }
|
|
|
|
+ String[] codeIdArr = codeIds.split(StrUtil.COMMA);
|
|
|
|
+ for (String codeId : codeIdArr) {
|
|
|
|
+ ResponseDTO responseDTO = codeGmCheck(codeId);
|
|
|
|
+ if(!responseDTO.getStatusCode().equals(ResponseStatus.SUCCESS_CODE)){
|
|
|
|
+ return responseDTO;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return ResponseDTO.success(ResponseStatus.SUCCESS_CODE,"国密数据校验成功");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public ResponseDTO codeGmSign(String codeId) {
|
|
|
|
+ List<CodeRecord> codeRecords = CodeInfoUtils.getCodeListByCodeDicId(codeId);
|
|
|
|
+ //去重
|
|
|
|
+ ArrayList<CodeRecord> collect = codeRecords.stream()
|
|
|
|
+ .collect(Collectors.collectingAndThen(Collectors.toCollection(() ->
|
|
|
|
+ new TreeSet<>(Comparator.comparing(codeRecord -> codeRecord.getLabel() + StrUtil.COMMA + codeRecord.getValue()))), ArrayList::new));
|
|
|
|
+ for (CodeRecord codeRecord : collect) {
|
|
|
|
+ CodeGmSign codeGmSign = new CodeGmSign();
|
|
|
|
+ codeGmSign.setCodeId(codeId);
|
|
|
|
+ codeGmSign.setCode(codeRecord.getValue());
|
|
|
|
+ String summary = sm3SignStrategy.summary(codeRecord.getLabel() + codeRecord.getValue());
|
|
|
|
+ codeGmSign.setDigest(summary);
|
|
|
|
+ codeGmBPO.save(codeGmSign);
|
|
|
|
+ }
|
|
|
|
+ return ResponseDTO.success(ResponseStatus.SUCCESS_CODE,"保存成功");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public List<CodeGmSign> getCodeGmSignByCodeId(String codeId) {
|
|
|
|
+ Searchable searchable = Searchable.newSearchable();
|
|
|
|
+ searchable.addSearchFilter("code_id", SearchOperator.eq,codeId);
|
|
|
|
+ return codeGmBPO.find(CodeGmSign.class, searchable);
|
|
|
|
+ }
|
|
|
|
+}
|