OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 94 | Rev 177 | 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
 
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;
26
 
27
public class HashWriter {
93 ilm 28
    public static final int BLOCK_SIZE = 1024;
61 ilm 29
    private File in;
30
 
31
    public HashWriter(File inputFile) {
32
        this.in = inputFile;
33
    }
34
 
35
    public void saveHash(File outputFile) {
93 ilm 36
        BufferedInputStream fb = null;
61 ilm 37
        try {
38
            if (!outputFile.exists()) {
39
                new File(outputFile.getParent()).mkdirs();
40
            }
93 ilm 41
            final DataOutputStream bOut = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(outputFile)));
61 ilm 42
            bOut.writeInt((int) this.in.length());
174 ilm 43
            final MessageDigest hashSum = MessageDigestUtils.getSHA256();
44
            final MessageDigest md5 = MessageDigestUtils.getMD5();
93 ilm 45
            fb = new BufferedInputStream(new FileInputStream(in));
46
            final RollingChecksum32 r32 = new RollingChecksum32();
47
            final byte[] buffer = new byte[BLOCK_SIZE];
61 ilm 48
            int readSize = fb.read(buffer);
49
            while (readSize > 0) {
50
                // Update
51
                r32.check(buffer, 0, readSize);
52
                md5.reset();
53
                md5.update(buffer, 0, readSize);
54
                hashSum.update(buffer, 0, readSize);
55
                // read
56
                readSize = fb.read(buffer);
57
                final byte[] engineDigest = md5.digest();
58
                bOut.writeInt(r32.getValue());
59
                bOut.write(engineDigest);
60
            }
61
            byte[] fileHash = new byte[hashSum.getDigestLength()];
62
            fileHash = hashSum.digest();
63
            bOut.write(fileHash);
64
            bOut.close();
65
 
66
        } catch (Exception e) {
67
            e.printStackTrace();
93 ilm 68
        } finally {
69
            if (fb != null) {
70
                try {
71
                    fb.close();
72
                } catch (IOException e) {
73
                    e.printStackTrace();
74
                }
75
            }
61 ilm 76
        }
77
    }
78
 
174 ilm 79
    public static byte[] getHash(File f) throws IOException {
80
        final MessageDigest hashSum = MessageDigestUtils.getSHA256();
94 ilm 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);
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
            }
61 ilm 108
        }
94 ilm 109
 
61 ilm 110
        byte[] fileHash = new byte[hashSum.getDigestLength()];
111
        fileHash = hashSum.digest();
112
        return fileHash;
113
    }
114
 
115
    public static boolean compareHash(byte[] h1, byte[] h2) {
116
        final int length = h1.length;
117
        if (length != h2.length) {
118
            return false;
119
        }
120
        for (int i = 0; i < length; i++) {
121
            if (h1[i] != h2[i]) {
122
                return false;
123
            }
124
        }
125
        return true;
126
    }
127
 
174 ilm 128
    private static final char[] hexArray = "0123456789ABCDEF".toCharArray();
129
 
130
    public static String bytesToHex(byte[] bytes) {
131
        char[] hexChars = new char[bytes.length * 2];
132
        for (int j = 0; j < bytes.length; j++) {
133
            int v = bytes[j] & 0xFF;
134
            hexChars[j * 2] = hexArray[v >>> 4];
135
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
136
        }
137
        return new String(hexChars);
138
    }
61 ilm 139
}