alpha 0.0.1
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / Tools.java
1 package de.dhbwloe.campusapp;
2
3 import android.view.View;
4 import android.view.ViewGroup;
5 import android.widget.AdapterView;
6
7 import java.security.MessageDigest;
8 import java.security.NoSuchAlgorithmException;
9
10 import javax.crypto.Cipher;
11 import javax.crypto.spec.SecretKeySpec;
12
13 /**
14  * Created by pk910 on 25.01.2016.
15  */
16 public class Tools {
17
18     public static final String md5(final String s) {
19         final String MD5 = "MD5";
20         try {
21             // Create MD5 Hash
22             MessageDigest digest = java.security.MessageDigest.getInstance(MD5);
23             digest.update(s.getBytes());
24             byte messageDigest[] = digest.digest();
25
26             // Create Hex String
27             StringBuilder hexString = new StringBuilder();
28             for (byte aMessageDigest : messageDigest) {
29                 String h = Integer.toHexString(0xFF & aMessageDigest);
30                 while (h.length() < 2)
31                     h = "0" + h;
32                 hexString.append(h);
33             }
34             return hexString.toString();
35
36         } catch (NoSuchAlgorithmException e) {
37             e.printStackTrace();
38         }
39         return "";
40     }
41
42     public static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
43         SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
44         Cipher cipher = Cipher.getInstance("AES");
45         cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
46         byte[] encrypted = cipher.doFinal(clear);
47         return encrypted;
48     }
49
50     public static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
51         SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
52         Cipher cipher = Cipher.getInstance("AES");
53         cipher.init(Cipher.DECRYPT_MODE, skeySpec);
54         byte[] decrypted = cipher.doFinal(encrypted);
55         return decrypted;
56     }
57
58     public static void removeAllChildViews(ViewGroup viewGroup) {
59         for (int i = 0; i < viewGroup.getChildCount(); i++) {
60             View child = viewGroup.getChildAt(i);
61             if (child instanceof ViewGroup) {
62                 if (child instanceof AdapterView) {
63                     viewGroup.removeView(child);
64                     return;
65                 }
66                 removeAllChildViews(((ViewGroup) child));
67             } else {
68                 viewGroup.removeView(child);
69             }
70         }
71     }
72
73 }