OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
 * 
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 */
 
 package org.openconcerto.utils.sync;

import org.openconcerto.utils.MessageDigestUtils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Objects;

public class HashWriter {
    public static final int BLOCK_SIZE = 1024;
    private File in;

    public HashWriter(File inputFile) {
        this.in = inputFile;
    }

    public void saveHash(File outputFile) {
        BufferedInputStream fb = null;
        try {
            if (!outputFile.exists()) {
                new File(outputFile.getParent()).mkdirs();
            }
            final DataOutputStream bOut = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(outputFile)));
            bOut.writeInt((int) this.in.length());
            final MessageDigest hashSum = MessageDigestUtils.getSHA256();
            final MessageDigest md5 = MessageDigestUtils.getMD5();
            fb = new BufferedInputStream(new FileInputStream(in));
            final RollingChecksum32 r32 = new RollingChecksum32();
            final byte[] buffer = new byte[BLOCK_SIZE];
            int readSize = fb.read(buffer);
            while (readSize > 0) {
                // Update
                r32.check(buffer, 0, readSize);
                md5.reset();
                md5.update(buffer, 0, readSize);
                hashSum.update(buffer, 0, readSize);
                // read
                readSize = fb.read(buffer);
                final byte[] engineDigest = md5.digest();
                bOut.writeInt(r32.getValue());
                bOut.write(engineDigest);
            }
            byte[] fileHash = new byte[hashSum.getDigestLength()];
            fileHash = hashSum.digest();
            bOut.write(fileHash);
            bOut.close();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fb != null) {
                try {
                    fb.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static byte[] getHash(File f) throws IOException {
        final MessageDigest hashSum = MessageDigestUtils.getSHA256();
        return MessageDigestUtils.getHash(hashSum, f);
    }

    public static boolean compareHash(byte[] h1, byte[] h2) {
        Objects.requireNonNull(h1);
        Objects.requireNonNull(h2);
        return Arrays.equals(h1, h2);
    }

    private static final char[] hexArray = "0123456789ABCDEF".toCharArray();

    public static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }
}