/*
 * Copyright © WebServices pour l'Éducation, 2014
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package fr.wseduc.webutils.security;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

public class Md5 {

	public static String hash(String input) throws NoSuchAlgorithmException {
		if(input == null) return null;
		MessageDigest digest = MessageDigest.getInstance("MD5");
		digest.update(input.getBytes(), 0, input.length());
		String hash = new BigInteger(1, digest.digest()).toString(16);
		while (hash.length() < 32) {
			hash = "0" + hash;
		}
		return hash;
	}

	public static byte[] hash(byte [] input) throws NoSuchAlgorithmException {
		if(input == null) return null;
		MessageDigest digest = MessageDigest.getInstance("MD5");
		digest.update(input, 0, input.length);
		return digest.digest();
	}

	public static boolean equality(byte [] input, byte [] sum) throws NoSuchAlgorithmException {
		return Arrays.equals(hash(input), sum);
	}

}

@@ -663,131 +663,506 @@
 ava.
-math.BigInteger;%0Aimport java.security.MessageDigest;%0Aimport java.security.NoSuchAlgorithmException;%0Aimport java.util.Arrays
+io.ByteArrayInputStream;%0Aimport java.io.File;%0Aimport java.io.FileInputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.io.SequenceInputStream;%0Aimport java.math.BigInteger;%0Aimport java.nio.charset.StandardCharsets;%0Aimport java.security.DigestInputStream;%0Aimport java.security.MessageDigest;%0Aimport java.security.NoSuchAlgorithmException;%0Aimport java.util.Arrays;%0Aimport java.util.Collections;%0Aimport java.util.Comparator;%0Aimport java.util.LinkedList;%0Aimport java.util.List
 ;%0A%0Ap
@@ -1777,24 +1777,1230 @@
 gest();%0A%09%7D%0A%0A
+%09public static String hashFile(String path) throws IOException, NoSuchAlgorithmException%0A%09%7B%0A%09%09%09File file = new File(path);%0A%09%09%09List%3CInputStream%3E fileStreams = new LinkedList%3CInputStream%3E();%0A%0A%09%09%09MessageDigest digest = MessageDigest.getInstance(%22MD5%22);%0A%0A%09%09%09if (!file.isDirectory())%0A%09%09%09%09fileStreams.add(new FileInputStream(file));%0A%09%09%09else%0A%09%09%09%09collectFiles(file, fileStreams);%0A%0A%09%09%09DigestInputStream dis = new DigestInputStream(new SequenceInputStream(Collections.enumeration(fileStreams)), digest);%0A%0A%09%09%09while(dis.read() != -1);%0A%09%09%09dis.close();%0A%09%09%09String hash = new BigInteger(1, digest.digest()).toString(16);%0A%09%09%09while (hash.length() %3C 32) %7B%0A%09%09%09%09hash = %220%22 + hash;%0A%09%09%09%7D%0A%09%09%09return hash;%0A%09%7D%0A%0A%09private static void collectFiles(File directory, List%3CInputStream%3E fileInputStreams) throws IOException%0A%09%7B%0A%09%09File%5B%5D files = directory.listFiles();%0A%0A%09%09fileInputStreams.add(new ByteArrayInputStream(directory.getName().getBytes(StandardCharsets.UTF_8)));%0A%0A%09%09if (files != null)%0A%09%09%7B%0A%09%09%09Arrays.sort(files, Comparator.comparing(File::getName));%0A%0A%09%09%09for (File file : files)%0A%09%09%09%7B%0A%09%09%09%09if (file.isDirectory())%0A%09%09%09%09%09%09collectFiles(file, fileInputStreams);%0A%09%09%09%09else%0A%09%09%09%09%09%09fileInputStreams.add(new FileInputStream(file));%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%0A
 %09public stat

Add a MD5 function to hash a file