rain
2024-08-02 dfae9043b11e788fa4d442e4576d73571e976cea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package com.dji.sample.territory.utils;
 
import org.bouncycastle.crypto.digests.SM3Digest;
import org.bouncycastle.util.encoders.Hex;
 
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());
        // 图片文件路径
        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 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();
        byte[] inputBytes = input.getBytes();
        digest.update(inputBytes, 0, inputBytes.length);
        byte[] hash = new byte[digest.getDigestSize()];
        digest.doFinal(hash, 0);
        return hash;
    }
 
}