OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 174 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 174 Rev 177
Line 21... Line 21...
21
import java.io.File;
21
import java.io.File;
22
import java.io.FileInputStream;
22
import java.io.FileInputStream;
23
import java.io.FileOutputStream;
23
import java.io.FileOutputStream;
24
import java.io.IOException;
24
import java.io.IOException;
25
import java.security.MessageDigest;
25
import java.security.MessageDigest;
-
 
26
import java.util.Arrays;
-
 
27
import java.util.Objects;
26
 
28
 
27
public class HashWriter {
29
public class HashWriter {
28
    public static final int BLOCK_SIZE = 1024;
30
    public static final int BLOCK_SIZE = 1024;
29
    private File in;
31
    private File in;
30
 
32
 
Line 76... Line 78...
76
        }
78
        }
77
    }
79
    }
78
 
80
 
79
    public static byte[] getHash(File f) throws IOException {
81
    public static byte[] getHash(File f) throws IOException {
80
        final MessageDigest hashSum = MessageDigestUtils.getSHA256();
82
        final MessageDigest hashSum = MessageDigestUtils.getSHA256();
81
        FileInputStream fIn = null;
-
 
82
        try {
-
 
83
            fIn = new FileInputStream(f);
-
 
84
            BufferedInputStream fb = null;
-
 
85
            try {
-
 
86
                fb = new BufferedInputStream(fIn);
-
 
87
                final byte[] buffer = new byte[BLOCK_SIZE];
-
 
88
                int readSize = fb.read(buffer);
83
        return MessageDigestUtils.getHash(hashSum, f);
89
                while (readSize > 0) {
-
 
90
                    // Update
-
 
91
                    hashSum.update(buffer, 0, readSize);
-
 
92
                    // read
-
 
93
                    readSize = fb.read(buffer);
-
 
94
                }
-
 
95
            } catch (Exception e) {
-
 
96
                throw new IOException(e);
-
 
97
            } finally {
-
 
98
                if (fb != null) {
-
 
99
                    fb.close();
-
 
100
                }
-
 
101
            }
-
 
102
        } catch (Exception e) {
-
 
103
            throw new IOException(e);
-
 
104
        } finally {
-
 
105
            if (fIn != null) {
-
 
106
                fIn.close();
-
 
107
            }
-
 
108
        }
-
 
109
 
-
 
110
        byte[] fileHash = new byte[hashSum.getDigestLength()];
-
 
111
        fileHash = hashSum.digest();
-
 
112
        return fileHash;
-
 
113
    }
84
    }
114
 
85
 
115
    public static boolean compareHash(byte[] h1, byte[] h2) {
86
    public static boolean compareHash(byte[] h1, byte[] h2) {
116
        final int length = h1.length;
-
 
117
        if (length != h2.length) {
-
 
118
            return false;
87
        Objects.requireNonNull(h1);
119
        }
-
 
120
        for (int i = 0; i < length; i++) {
-
 
121
            if (h1[i] != h2[i]) {
-
 
122
                return false;
88
        Objects.requireNonNull(h2);
123
            }
-
 
124
        }
-
 
125
        return true;
89
        return Arrays.equals(h1, h2);
126
    }
90
    }
127
 
91
 
128
    private static final char[] hexArray = "0123456789ABCDEF".toCharArray();
92
    private static final char[] hexArray = "0123456789ABCDEF".toCharArray();
129
 
93
 
130
    public static String bytesToHex(byte[] bytes) {
94
    public static String bytesToHex(byte[] bytes) {