rain
2024-06-07 3753c46104303d8c5e36b3068650b1e0d136f447
SM3加密
2 files modified
31 ■■■■ changed files
src/main/java/com/dji/sample/territory/service/impl/TbFjServiceImpl.java 5 ●●●●● patch | view | raw | blame | history
src/main/java/com/dji/sample/territory/utils/HashUtil.java 26 ●●●● patch | view | raw | blame | history
src/main/java/com/dji/sample/territory/service/impl/TbFjServiceImpl.java
@@ -145,9 +145,8 @@
            file1 = VideoZipUtil.compressVideo(file, 800000, 128000, 1280, 720);
            FJ = fileToByteArray(file1);
        }
        String sm3 = Sm3Util.calculateSM3Hash(fjhxz + "," + pssj + "," + lng + "," + lat + "," + gimbalYawDegree + "," + psjd + "," + pshgj + "," + psry + "," + zsdm);
        byte[] hash = sm3.getBytes(); // 注意:使用SM3或其他哈希算法来计算数据的哈希值
        String hxz = HashUtil.calculateHash((fjhxz + "," + pssj + "," + lng + "," + lat + "," + gimbalYawDegree + "," + psjd + "," + pshgj + "," + psry + "," + zsdm).getBytes());
        byte[] hash = hxz.getBytes(); // 注意:使用SM3或其他哈希算法来计算数据的哈希值
        // 加载私钥
        ECPrivateKeyParameters sm2PrivateKey = getSM2PrivateKey();
        // 使用SM2私钥对哈希值进行签名
src/main/java/com/dji/sample/territory/utils/HashUtil.java
@@ -10,18 +10,15 @@
public class HashUtil {
    public static String SM3Hash(File file) {
        String filePath = String.valueOf(file);
        String hashHex = "";
        try {
            byte[] fileBytes = readFile(filePath);
            byte[] hash = calculateHash(fileBytes);
             hashHex = Hex.toHexString(hash);
            return calculateHash(fileBytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return hashHex;
        return "";
    }
    private static byte[] readFile(String filePath) throws IOException {
    public static byte[] readFile(String filePath) throws IOException {
        FileInputStream fis = new FileInputStream(filePath);
        byte[] buffer = new byte[1024];
        int bytesRead;
@@ -32,12 +29,17 @@
        fis.close();
        return sb.toString().getBytes();
    }
    public static String calculateHash(byte[] data) {
        byte[] md = new byte[32];
        SM3Digest sm3 = new SM3Digest();
        sm3.update(data, 0, data.length);
        sm3.doFinal(md, 0);
        String s = new String(Hex.encode(md));
        return s.toUpperCase();
    }
    private static byte[] calculateHash(byte[] input) {
        SM3Digest digest = new SM3Digest();
        digest.update(input, 0, input.length);
        byte[] hash = new byte[digest.getDigestSize()];
        digest.doFinal(hash, 0);
        return hash;
    public static void main(String[] args) {
        File file = new File("D:\\drone\\src\\main\\resources\\FJ_1s.jpeg");
        System.out.println(SM3Hash(file));
    }
}