]> matita.cs.unibo.it Git - logicplayer.git/blobdiff - server/tesi/src/com/company/MD5.java
Shuffling.
[logicplayer.git] / server / tesi / src / com / company / MD5.java
diff --git a/server/tesi/src/com/company/MD5.java b/server/tesi/src/com/company/MD5.java
new file mode 100644 (file)
index 0000000..623f11f
--- /dev/null
@@ -0,0 +1,46 @@
+package com.company;
+
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+
+
+public class MD5 {
+
+    MD5(){}
+    public static String digest(String filePath) {
+        InputStream inputStream = null;
+        try {
+            inputStream = new FileInputStream(global.locationEsercizi+filePath);
+            byte[] buffer = new byte[1024];
+            MessageDigest digest = MessageDigest.getInstance("MD5");
+            int numRead = 0;
+            while (numRead != -1) {
+                numRead = inputStream.read(buffer);
+                if (numRead > 0)
+                    digest.update(buffer, 0, numRead);
+            }
+            byte [] md5Bytes = digest.digest();
+            System.out.println(convertHashToString(md5Bytes));
+            return convertHashToString(md5Bytes);
+        } catch (Exception e) {
+            return null;
+        } finally {
+            if (inputStream != null) {
+                try {
+                    inputStream.close();
+                } catch (Exception e) { }
+            }
+        }
+    }
+
+    private static String convertHashToString(byte[] md5Bytes) {
+        String returnVal = "";
+        for (int i = 0; i < md5Bytes.length; i++) {
+            returnVal += Integer.toString(( md5Bytes[i] & 0xff ) + 0x100, 16).substring(1);
+        }
+        return returnVal;
+    }
+}