--- /dev/null
+/*
+ * <p>MuPdf Demo<br/>
+ * Original work MuPDF Copyright (C) 2006-2016 Artifex Software, Inc.<br/>
+ * Modified work Copyright (C) 2016 Adullact-Projet.</p>
+ *
+ * <p>This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.</p>
+ *
+ * <p>This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.</p>
+ *
+ * <p>You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.</p>
+ */
+package com.artifex.mupdfdemo;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.math.BigInteger;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+
+
+public class MD5 {
+
+ public static boolean checkMD5(String md5, File updateFile) throws IOException {
+ if (md5 == null || md5 == "" || updateFile == null) {
+ return false;
+ }
+
+ String calculatedDigest = calculateMD5(updateFile);
+
+ if (calculatedDigest == null) {
+ return false;
+ }
+ return calculatedDigest.equalsIgnoreCase(md5);
+ }
+
+ public static String calculateMD5(File updateFile) {
+ MessageDigest digest = null;
+ try {
+ digest = MessageDigest.getInstance("MD5");
+ }
+ catch (NoSuchAlgorithmException e) {
+ return null;
+ }
+ InputStream is = null;
+ try {
+ is = new FileInputStream(updateFile);
+ }
+ catch (FileNotFoundException e) {
+ return null;
+ }
+ byte[] buffer = new byte[8192];
+ int read = 0;
+ try {
+ while ((read = is.read(buffer)) > 0) {
+ digest.update(buffer, 0, read);
+ }
+ byte[] md5sum = digest.digest();
+ BigInteger bigInt = new BigInteger(1, md5sum);
+ String output = bigInt.toString(16);
+ // Fill to 32 chars
+ output = String.format("%32s", output).replace(' ', '0');
+ return output;
+ }
+ catch (IOException e) {
+ throw new RuntimeException("Unable to process file for MD5", e);
+ }
+ finally {
+ try {
+ is.close();
+ }
+ catch (IOException e) {
+ throw new RuntimeException("Unable to close input stream for MD5 calculation", e);
+ }
+ }
+ }
+
+ public static String MD5Hash(String s) {
+ MessageDigest m = null;
+
+ try {
+ m = MessageDigest.getInstance("MD5");
+ }
+ catch (NoSuchAlgorithmException e) {
+ e.printStackTrace();
+ return "";
+ }
+
+ m.update(s.getBytes(), 0, s.length());
+ String hash = new BigInteger(1, m.digest()).toString(16);
+ return hash;
+ }
+}