From 8b14209e043b037f8e7a5a18138fdf2140885bdb Mon Sep 17 00:00:00 2001
From: aix <vip_xiaobin810@163.com>
Date: Tue, 23 Jul 2024 19:14:27 +0800
Subject: [PATCH] 航测功能
---
src/main/java/com/dji/sample/territory/utils/SM3HashExample.java | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++------
1 files changed, 53 insertions(+), 6 deletions(-)
diff --git a/src/main/java/com/dji/sample/territory/utils/SM3HashExample.java b/src/main/java/com/dji/sample/territory/utils/SM3HashExample.java
index ba2a71d..25498c1 100644
--- a/src/main/java/com/dji/sample/territory/utils/SM3HashExample.java
+++ b/src/main/java/com/dji/sample/territory/utils/SM3HashExample.java
@@ -1,18 +1,22 @@
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 org.bouncycastle.jce.provider.BouncyCastleProvider;
+
+import java.io.*;
import java.math.BigInteger;
+import java.nio.charset.StandardCharsets;
import java.security.Security;
+import java.util.Scanner;
public class SM3HashExample {
+ public static void main(String[] args) {
+ File file=new File("C:\\Users\\a1679\\Desktop\\pic.txt");
+ System.out.println(HaXi(file));
+ System.out.println(HaXi("123"));
+ }
public static StringBuilder HaXi(File file) {
// 向Java安全提供者列表中添加Bouncy Castle提供者
Security.addProvider(new BouncyCastleProvider());
@@ -66,6 +70,48 @@
}
return hexString;
}
+ public static StringBuilder HaXi(String input) {
+ // 向Java安全提供者列表中添加Bouncy Castle提供者
+ Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
+
+ try {
+ SM3Digest digest = new SM3Digest();
+
+ // 将字符串转换为字节数组
+ byte[] inputBytes = input.getBytes("UTF-8");
+
+ // 使用ByteArrayInputStream包装字节数组
+ try (ByteArrayInputStream bis = new ByteArrayInputStream(inputBytes)) {
+ byte[] buffer = new byte[1024];
+ int bytesRead;
+
+ // 读取字节数组并更新哈希值
+ while ((bytesRead = bis.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 byte[] calculateSM3Hash(String input) {
SM3Digest digest = new SM3Digest();
@@ -77,3 +123,4 @@
}
}
+
--
Gitblit v1.9.3