OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 93 | Rev 174 | Go to most recent revision | 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
 
16
import java.io.BufferedInputStream;
17
import java.io.BufferedOutputStream;
18
import java.io.DataOutputStream;
19
import java.io.File;
20
import java.io.FileInputStream;
21
import java.io.FileOutputStream;
93 ilm 22
import java.io.IOException;
61 ilm 23
import java.security.MessageDigest;
24
 
25
public class HashWriter {
93 ilm 26
    public static final int BLOCK_SIZE = 1024;
61 ilm 27
    private File in;
28
 
29
    public HashWriter(File inputFile) {
30
        this.in = inputFile;
31
    }
32
 
33
    public void saveHash(File outputFile) {
93 ilm 34
        BufferedInputStream fb = null;
61 ilm 35
        try {
36
            if (!outputFile.exists()) {
37
                new File(outputFile.getParent()).mkdirs();
38
            }
93 ilm 39
            final DataOutputStream bOut = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(outputFile)));
61 ilm 40
            bOut.writeInt((int) this.in.length());
93 ilm 41
            final MessageDigest hashSum = MessageDigest.getInstance("SHA-256");
42
            final MessageDigest md5 = MessageDigest.getInstance("MD5");
43
            fb = new BufferedInputStream(new FileInputStream(in));
44
            final RollingChecksum32 r32 = new RollingChecksum32();
45
            final byte[] buffer = new byte[BLOCK_SIZE];
61 ilm 46
            int readSize = fb.read(buffer);
47
            while (readSize > 0) {
48
                // Update
49
                r32.check(buffer, 0, readSize);
50
                md5.reset();
51
                md5.update(buffer, 0, readSize);
52
                hashSum.update(buffer, 0, readSize);
53
                // read
54
                readSize = fb.read(buffer);
55
                final byte[] engineDigest = md5.digest();
56
                bOut.writeInt(r32.getValue());
57
                bOut.write(engineDigest);
58
            }
59
            byte[] fileHash = new byte[hashSum.getDigestLength()];
60
            fileHash = hashSum.digest();
61
            bOut.write(fileHash);
62
            bOut.close();
63
 
64
        } catch (Exception e) {
65
            e.printStackTrace();
93 ilm 66
        } finally {
67
            if (fb != null) {
68
                try {
69
                    fb.close();
70
                } catch (IOException e) {
71
                    e.printStackTrace();
72
                }
73
            }
61 ilm 74
        }
75
    }
76
 
77
    public static byte[] getHash(File f) throws Exception {
93 ilm 78
        final MessageDigest hashSum = MessageDigest.getInstance("SHA-256");
94 ilm 79
        FileInputStream fIn = null;
80
        try {
81
            fIn = new FileInputStream(f);
82
            BufferedInputStream fb = null;
83
            try {
84
                fb = new BufferedInputStream(fIn);
85
                final byte[] buffer = new byte[BLOCK_SIZE];
86
                int readSize = fb.read(buffer);
87
                while (readSize > 0) {
88
                    // Update
89
                    hashSum.update(buffer, 0, readSize);
90
                    // read
91
                    readSize = fb.read(buffer);
92
                }
93
            } catch (Exception e) {
94
                throw new IOException(e);
95
            } finally {
96
                if (fb != null) {
97
                    fb.close();
98
                }
99
            }
100
        } catch (Exception e) {
101
            throw new IOException(e);
102
        } finally {
103
            if (fIn != null) {
104
                fIn.close();
105
            }
61 ilm 106
        }
94 ilm 107
 
61 ilm 108
        byte[] fileHash = new byte[hashSum.getDigestLength()];
109
        fileHash = hashSum.digest();
110
        return fileHash;
111
    }
112
 
113
    public static boolean compareHash(byte[] h1, byte[] h2) {
114
        final int length = h1.length;
115
        if (length != h2.length) {
116
            return false;
117
        }
118
        for (int i = 0; i < length; i++) {
119
            if (h1[i] != h2[i]) {
120
                return false;
121
            }
122
        }
123
        return true;
124
    }
125
 
126
}