liujie
2026-06-09 c96fbe6b0aeb413a17c429133ee6d81d138144b9
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
package com.xiaopeng.vehicleConvoy.util;
 
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
 
/**
 * 该API支持线程安全<br/>
 */
public class SHA1Util {
    // --------------------------计算SHA1大写字符串----------------------------------------------
    /**
     * 计算出文件的SHA1大写字符串
     * 
     * @param file
     * @return
     * @throws IOException
     */
    public static String getSHA1UpperString(File file) throws IOException {
        return HexUtil.toUpperCase(getSHA1Bytes(file));
    }
 
    /**
     * 计算出SHA1后十六进制结果(大写)
     * 
     * @param s
     * @return
     */
    public static String getSHA1UpperString(String s) {
        try {
            return getSHA1UpperString(s.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
 
    /**
     * 计算出SHA1后十六进制结果(大写)
     * 
     * @param bytes
     * @return
     */
    public static String getSHA1UpperString(byte[] bytes) {
        return HexUtil.toUpperCase(getSHA1Bytes(bytes));
    }
 
    // --------------------------计算SHA1小写字符串----------------------------------------------
    /**
     * 计算出文件的SHA1小写字符串
     * 
     * @param file
     * @return
     * @throws IOException
     */
    public static String getSHA1LowerString(File file) throws IOException {
        return HexUtil.toLowerCase(getSHA1Bytes(file));
    }
 
    /**
     * 计算出SHA1后十六进制结果(小写)
     * 
     * @param s
     * @return
     */
    public static String getSHA1LowerString(String s) {
        try {
            return getSHA1LowerString(s.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
 
    /**
     * 计算出SHA1后十六进制结果(小写)
     * 
     * @param bytes
     * @return
     */
    public static String getSHA1LowerString(byte[] bytes) {
        return HexUtil.toLowerCase(getSHA1Bytes(bytes));
    }
 
    // ------------------------计算出SHA1的byte[]------------------------------------------------
    public static byte[] getSHA1Bytes(byte[] bytes) {
        ThreadLocalSHA1.current().update(bytes);
        return ThreadLocalSHA1.current().digest();
    }
 
    public static byte[] getSHA1Bytes(File file) throws IOException {
        FileInputStream in = null;
        FileChannel ch = null;
        try {
            in = new FileInputStream(file);
            ch = in.getChannel();
 
            MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
 
            ThreadLocalSHA1.current().update(byteBuffer);
            return ThreadLocalSHA1.current().digest();
        } finally {
            ResourceUtil.closeGracefully(ch);
            ResourceUtil.closeGracefully(in);
        }
    }
 
    // ------------------------------------------------------------------------
    public static boolean checkPassword(String originContent, String sha1Content) {
        String s = getSHA1LowerString(originContent);
        return s.equals(sha1Content);
    }
 
    // ------------------------------------------------------------------------
    
}