OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 174 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
61 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.utils.sync;
15
 
174 ilm 16
import org.openconcerto.utils.MessageDigestUtils;
17
 
61 ilm 18
import java.io.BufferedInputStream;
19
import java.io.BufferedOutputStream;
20
import java.io.DataOutputStream;
21
import java.io.File;
22
import java.io.FileInputStream;
23
import java.io.FileOutputStream;
93 ilm 24
import java.io.IOException;
61 ilm 25
import java.security.MessageDigest;
177 ilm 26
import java.util.Arrays;
27
import java.util.Objects;
61 ilm 28
 
29
public class HashWriter {
93 ilm 30
    public static final int BLOCK_SIZE = 1024;
61 ilm 31
    private File in;
32
 
33
    public HashWriter(File inputFile) {
34
        this.in = inputFile;
35
    }
36
 
37
    public void saveHash(File outputFile) {
93 ilm 38
        BufferedInputStream fb = null;
61 ilm 39
        try {
40
            if (!outputFile.exists()) {
41
                new File(outputFile.getParent()).mkdirs();
42
            }
93 ilm 43
            final DataOutputStream bOut = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(outputFile)));
61 ilm 44
            bOut.writeInt((int) this.in.length());
174 ilm 45
            final MessageDigest hashSum = MessageDigestUtils.getSHA256();
46
            final MessageDigest md5 = MessageDigestUtils.getMD5();
93 ilm 47
            fb = new BufferedInputStream(new FileInputStream(in));
48
            final RollingChecksum32 r32 = new RollingChecksum32();
49
            final byte[] buffer = new byte[BLOCK_SIZE];
61 ilm 50
            int readSize = fb.read(buffer);
51
            while (readSize > 0) {
52
                // Update
53
                r32.check(buffer, 0, readSize);
54
                md5.reset();
55
                md5.update(buffer, 0, readSize);
56
                hashSum.update(buffer, 0, readSize);
57
                // read
58
                readSize = fb.read(buffer);
59
                final byte[] engineDigest = md5.digest();
60
                bOut.writeInt(r32.getValue());
61
                bOut.write(engineDigest);
62
            }
63
            byte[] fileHash = new byte[hashSum.getDigestLength()];
64
            fileHash = hashSum.digest();
65
            bOut.write(fileHash);
66
            bOut.close();
67
 
68
        } catch (Exception e) {
69
            e.printStackTrace();
93 ilm 70
        } finally {
71
            if (fb != null) {
72
                try {
73
                    fb.close();
74
                } catch (IOException e) {
75
                    e.printStackTrace();
76
                }
77
            }
61 ilm 78
        }
79
    }
80
 
174 ilm 81
    public static byte[] getHash(File f) throws IOException {
82
        final MessageDigest hashSum = MessageDigestUtils.getSHA256();
177 ilm 83
        return MessageDigestUtils.getHash(hashSum, f);
61 ilm 84
    }
85
 
86
    public static boolean compareHash(byte[] h1, byte[] h2) {
177 ilm 87
        Objects.requireNonNull(h1);
88
        Objects.requireNonNull(h2);
89
        return Arrays.equals(h1, h2);
61 ilm 90
    }
91
 
174 ilm 92
    private static final char[] hexArray = "0123456789ABCDEF".toCharArray();
93
 
94
    public static String bytesToHex(byte[] bytes) {
95
        char[] hexChars = new char[bytes.length * 2];
96
        for (int j = 0; j < bytes.length; j++) {
97
            int v = bytes[j] & 0xFF;
98
            hexChars[j * 2] = hexArray[v >>> 4];
99
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
100
        }
101
        return new String(hexChars);
102
    }
61 ilm 103
}