admin
2021-08-13 cdc3690a0354e01b44852f4c9da3b7204128d2eb
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
package com.yeshi.buwan.util;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
 
public class SHA1Util {
    public static String getFileMD5(File file) {
        if (!file.isFile()) {
            return null;
        }
        MessageDigest digest = null;
        FileInputStream in = null;
        byte buffer[] = new byte[1024];
        int len;
        try {
            digest = MessageDigest.getInstance("SHA-1");
            in = new FileInputStream(file);
            while ((len = in.read(buffer, 0, 1024)) != -1) {
                digest.update(buffer, 0, len);
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return byte2hex(digest.digest());
    }
 
    public static String byte2hex(byte abyte0[]) {
        StringBuilder stringbuilder = new StringBuilder("");
        int i = 0;
        while (i < abyte0.length) {
            String s = Integer.toHexString(0xff & abyte0[i]);
            String s1;
            if (s.length() == 1)
                s1 = (new StringBuilder()).append("0").append(s).toString();
            else
                s1 = s;
            stringbuilder.append(s1);
            i++;
        }
 
        return stringbuilder.toString().trim();
    }
 
    public static String leshiYunSHA1(File file)
            throws NoSuchAlgorithmException, IOException {
        MessageDigest messagedigest = MessageDigest.getInstance("SHA-1");
        RandomAccessFile randomaccessfile1 = new RandomAccessFile(file, "r");
        byte abyte0[] = new byte[0x10000];
        long l;
        int j;
        int k;
        if (file.length() % 8L == 0L)
            l = file.length() / 8L;
        else
            l = 1L + file.length() / 8L;
        for (int i = 0; i < 8; i++) {
            randomaccessfile1.seek(l * (long) i);
            j = 0;
            do {
                if (j >= 0x10000)
                    break;
                k = randomaccessfile1.read(abyte0);
                if (k <= 0)
                    break;
                messagedigest.update(abyte0, 0, k);
                j += k;
            } while (true);
        }
        messagedigest.update(String.valueOf(file.length()).getBytes());
        return byte2hex(messagedigest.digest());
    }
    
    
    
    
 
}