rain
2024-06-12 a0e7cf2d16dc23b1d6edf3b69678ab66ccacb54d
图斑生成航线,图片哈希值
5 files modified
1 files added
173 ■■■■ changed files
src/main/java/com/dji/sample/media/service/impl/FileServiceImpl.java 3 ●●●● patch | view | raw | blame | history
src/main/java/com/dji/sample/patches/controller/PatchesController.java 19 ●●●● patch | view | raw | blame | history
src/main/java/com/dji/sample/territory/service/impl/TbFjServiceImpl.java 20 ●●●●● patch | view | raw | blame | history
src/main/java/com/dji/sample/territory/utils/HashUtil.java 3 ●●●● patch | view | raw | blame | history
src/main/java/com/dji/sample/territory/utils/SM2SignUtil.java 49 ●●●●● patch | view | raw | blame | history
src/main/java/com/dji/sample/territory/utils/SM3HashExample.java 79 ●●●●● patch | view | raw | blame | history
src/main/java/com/dji/sample/media/service/impl/FileServiceImpl.java
@@ -138,7 +138,6 @@
        String name = "mark" + filename;
        MediaFileMarkEntity entity = markMapper.selectOne(new LambdaQueryWrapper<MediaFileMarkEntity>()
                .eq(MediaFileMarkEntity::getFileName, name));
        return entity.getDronedata();
    }
@@ -158,7 +157,7 @@
    }
    public List<MediaFileEntity> listByIsadd(String dkbh, String workspaceId) {
        return mapper.selectList(new LambdaQueryWrapper<MediaFileEntity>().eq(MediaFileEntity::getIsadd, 0)
        return mapper.selectList(new LambdaQueryWrapper<MediaFileEntity>()
                .eq(MediaFileEntity::getWorkspaceId, workspaceId)
                .like(MediaFileEntity::getFileName, dkbh + "~"));
    }
src/main/java/com/dji/sample/patches/controller/PatchesController.java
@@ -107,11 +107,20 @@
    public ResponseResult<LotInfo> wayAndPatchesId(@RequestParam String patchesId) {
        return ResponseResult.success(getPatchesService.getPatchesFromId(patchesId));
    }
    @GetMapping("/getWayline")
    public ResponseResult getWayline(@RequestParam String waylineName, @RequestBody List<LotInfo> list){
        try {
            MultipartFile file=timerUtil.getFile(waylineName,list);
            return ResponseResult.success(file);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    @PostMapping("/patchesToWayline")
    public ResponseResult patchesToWayline(List<LotInfo> list,
                                           String waylineName,
                                           String workspaceId,
    public ResponseResult patchesToWayline(@RequestBody  List<LotInfo> list,
                                           @RequestParam String waylineName,
                                           @RequestParam String workspaceId,
                                           HttpServletRequest request) throws IOException {
        CustomClaim customClaim = (CustomClaim)request.getAttribute(TOKEN_CLAIM);
        String creator = customClaim.getUsername();
@@ -137,13 +146,13 @@
            timerUtil.myTask2();
            return ResponseResult.success();
        } catch (Exception e) {
            throw new RuntimeException("db存储发送出现异常");
            throw new RuntimeException("db存储发送出现异常"+e);
        }
    }
    @GetMapping ("/tests")
    public ResponseResult use()  {
        try {
            TimerUtil.sendPostWithFileAndParameter("src/main/resources/tmp/20240607/103145_635148ea-0ddb-4b23-945c-8a67abd813c9.db",
            TimerUtil.sendPostWithFileAndParameter("src/main/resources/1.db",
                    "635148ea-0ddb-4b23-945c-8a67abd813c9");
        } catch (IOException e) {
            throw new RuntimeException(e);
src/main/java/com/dji/sample/territory/service/impl/TbFjServiceImpl.java
@@ -10,10 +10,7 @@
import com.dji.sample.territory.dao.ITbFjMapper;
import com.dji.sample.territory.model.entity.TbFjEntity;
import com.dji.sample.territory.service.ITbFJService;
import com.dji.sample.territory.utils.HashUtil;
import com.dji.sample.territory.utils.Sm3Util;
import com.dji.sample.territory.utils.VideoZipUtil;
import com.dji.sample.territory.utils.WaterMarkUtil;
import com.dji.sample.territory.utils.*;
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -132,25 +129,22 @@
        String numberPart = focal.replaceAll("[^\\d]", "");
        int psjj = Integer.parseInt(numberPart);
        json.getString("GimbalYawDegree");
        String fjhxz = HashUtil.SM3Hash(file);
        BufferedImage image = ImageIO.read(file);
        int width = image.getWidth();
        int height = image.getHeight();
        String fjhxz = null;
        //对应图片和视频文件进行不同处理
        boolean endsWith = key.endsWith(".mp4");
        if (!endsWith) {
            file1 = WaterMarkUtil.addWatermark(file, sj, lat, lng, gimbalYawDegree);
            fjhxz = String.valueOf(SM3HashExample.HaXi(new File(file1.toURI())));
            FJ = fileToByteArray(file1);
        } else {
            file1 = VideoZipUtil.compressVideo(file, 800000, 128000, 1280, 720);
            FJ = fileToByteArray(file1);
        }
        String hxz = HashUtil.calculateHash((fjhxz + "," + pssj + "," + lng + "," + lat + "," + gimbalYawDegree + "," + psjd + "," + pshgj + "," + psry + "," + zsdm).getBytes());
        byte[] hash = hxz.getBytes(); // 注意:使用SM3或其他哈希算法来计算数据的哈希值
        // 加载私钥
        ECPrivateKeyParameters sm2PrivateKey = getSM2PrivateKey();
        // 使用SM2私钥对哈希值进行签名
        String signatureHex = signWithSM2(hash, sm2PrivateKey);
        String jym=SM2SignExamples.jym(hxz);
        TbFjEntity.TbFjEntityBuilder builder = TbFjEntity.builder();
        if (lotInfo != null) {
            builder.bsm(bsm)
@@ -172,7 +166,7 @@
                    .pshgj(pshgj)
                    .zsdm(zsdm)
                    .psry(psry)
                    .jym(signatureHex)
                    .jym(jym)
                    .url(url)
                    .pstz(pstz)
                    .fjyskd(width)
@@ -258,7 +252,7 @@
    }
    public static void main(String[] args) {
        System.out.println(truncateToSevenDecimalPlaces(152.00666565));
        System.out.println(truncateToTwoDecimalPlaces(12.006));
        String s= String.valueOf(SM3HashExample.HaXi(new File("src/main/resources/FJ_10.jpeg")));
        System.out.println(s);
    }
}
src/main/java/com/dji/sample/territory/utils/HashUtil.java
@@ -39,7 +39,6 @@
    }
    public static void main(String[] args) {
        File file = new File("D:\\drone\\src\\main\\resources\\FJ_1s.jpeg");
        System.out.println(SM3Hash(file));
        System.out.println(SM3Hash(new File("src/main/resources/FJ_10.jpeg")));
    }
}
src/main/java/com/dji/sample/territory/utils/SM2SignUtil.java
@@ -14,6 +14,7 @@
import org.bouncycastle.math.ec.ECPoint;
import org.bouncycastle.math.ec.FixedPointCombMultiplier;
import java.io.File;
import java.math.BigInteger;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
@@ -109,32 +110,40 @@
        return hexString.toString().toUpperCase();
    }
//    public static void main(String[] args) throws Exception {
//        // 示例哈希值(这里只是一个占位符,你应该使用实际的哈希值)
////        String sm3 = Sm3Util.calculateSM3Hash("FJHXZ,PSSJ,Longitude,Latitude,PSFYJ,PSJD,PSHGJ,PSRY,ZSDM");
////        System.out.println("sm3 in hash: " + sm3);
    public static void main(String[] args) throws Exception {
        // 示例哈希值(这里只是一个占位符,你应该使用实际的哈希值)
        String hxz = String.valueOf(SM3HashExample.str(("B69200D500E57054E987FFF855204B30CE6BD32FE54937C531D6F943633A97A6"
                + "," + "2024-06-05 10:04:16" + "," + "115.8506321" + "," + "28.6193951" + "," + "-83" + "," + "135" +
                "," + "0" + "," + "中图智绘无人机" + "," + "23C8CCC61E3042FBA6A658F319337B1A")));
//        System.out.println("sm3 in hash: " + sm3);
//        byte[] hash = sm3.getBytes(); // 注意:使用SM3或其他哈希算法来计算数据的哈希值
//
//        // 加载私钥
        System.out.println(hxz);
        byte[] hash = hxz.getBytes(); // 注意:使用SM3或其他哈希算法来计算数据的哈希值
        // 加载私钥
        ECPrivateKeyParameters sm2PrivateKey = getSM2PrivateKey();
        // 使用SM2私钥对哈希值进行签名
        String signatureHex = signWithSM2(hash, sm2PrivateKey);
        System.out.println(signatureHex);
        // 加载私钥
//        ECPrivateKeyParameters sm2PrivateKey = getSM2PrivateKey();
//
//        // 使用SM2私钥对哈希值进行签名
//        String signatureHex = signWithSM2(hash, sm2PrivateKey);
////        System.out.println("Signature in hex (uppercase): " + signatureHex);
//        System.out.println("Signature in hex (uppercase): " + signatureHex);
//        //验证
//        // 示例数据哈希(SM3哈希值)
//        byte[] dataHash = hash; // 注意:这里仅作为示例,你需要用实际的SM3哈希值
//
////        //验证
////        // 示例数据哈希(SM3哈希值)
////        byte[] dataHash = hash; // 注意:这里仅作为示例,你需要用实际的SM3哈希值
////
////        // 示例签名(字节数组)
////        byte[] signature = signatureHex.getBytes(); // 注意:这里仅作为示例,你需要用实际的签名值
////
////        // 验证签名
////        boolean isValid = verify(dataHash, signature);
////
////        // 输出验证结果
////        System.out.println("Signature is valid: " + isValid);
//        // 示例签名(字节数组)
//        byte[] signature = signatureHex.getBytes(); // 注意:这里仅作为示例,你需要用实际的签名值
//
//    }
//        // 验证签名
//        boolean isValid = verify(dataHash, signature);
//
//        // 输出验证结果
//        System.out.println("Signature is valid: " + isValid);
    }
}
src/main/java/com/dji/sample/territory/utils/SM3HashExample.java
New file
@@ -0,0 +1,79 @@
package com.dji.sample.territory.utils;
import org.bouncycastle.crypto.digests.SM3Digest;
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.bouncycastle.crypto.signers.SM2Signer;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.security.Security;
public class SM3HashExample {
    public static StringBuilder HaXi(File file) {
        // 向Java安全提供者列表中添加Bouncy Castle提供者
        Security.addProvider(new BouncyCastleProvider());
        // 图片文件路径
        try (FileInputStream fis = new FileInputStream(file)) {
            SM3Digest digest = new SM3Digest();
            byte[] buffer = new byte[1024];
            int bytesRead;
            // 读取文件并更新哈希值
            while ((bytesRead = fis.read(buffer)) != -1) {
                digest.update(buffer, 0, bytesRead);
            }
            // 计算哈希值
            byte[] hash = new byte[digest.getDigestSize()];
            digest.doFinal(hash, 0);
            // 将哈希值转换为十六进制字符串
            StringBuilder hexString = new StringBuilder();
            for (byte b : hash) {
                String hex = Integer.toHexString(0xff & b).toUpperCase();
                if (hex.length() == 1) hexString.append('0');
                hexString.append(hex);
            }
            // 输出哈希值
            return (hexString);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static StringBuilder str(String s) {
        // 向Java安全提供者列表中添加Bouncy Castle提供者
        Security.addProvider(new BouncyCastleProvider());
        // 要进行哈希计算的字符串
        String input = s;
        // 计算SM3哈希值
        byte[] hash = calculateSM3Hash(input);
        // 将哈希值转换为十六进制字符串
        StringBuilder hexString = new StringBuilder();
        for (byte b : hash) {
            String hex = Integer.toHexString(0xff & b).toUpperCase();
            if (hex.length() == 1) hexString.append('0');
            hexString.append(hex);
        }
        return hexString;
    }
    public static byte[] calculateSM3Hash(String input) {
        SM3Digest digest = new SM3Digest();
        byte[] inputBytes = input.getBytes();
        digest.update(inputBytes, 0, inputBytes.length);
        byte[] hash = new byte[digest.getDigestSize()];
        digest.doFinal(hash, 0);
        return hash;
    }
}