• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.certinstaller;
18 
19 import android.util.Log;
20 
21 import java.io.ByteArrayInputStream;
22 import java.io.ByteArrayOutputStream;
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.ObjectInputStream;
26 import java.io.ObjectOutputStream;
27 import java.security.MessageDigest;
28 import java.security.NoSuchAlgorithmException;
29 
30 class Util {
31     private static final String TAG = "certinstaller.Util";
32 
33     public static final String SETTINGS_PACKAGE = "com.android.settings";
34 
toBytes(Object object)35     static byte[] toBytes(Object object) {
36         ByteArrayOutputStream baos = new ByteArrayOutputStream();
37         try {
38             ObjectOutputStream os = new ObjectOutputStream(baos);
39             os.writeObject(object);
40             os.close();
41         } catch (Exception e) {
42             Log.w(TAG, "toBytes(): " + e + ": " + object);
43         }
44         return baos.toByteArray();
45     }
46 
fromBytes(byte[] bytes)47     static <T> T fromBytes(byte[] bytes) {
48         if (bytes == null) return null;
49         try {
50             ObjectInputStream is =
51                     new ObjectInputStream(new ByteArrayInputStream(bytes));
52             return (T) is.readObject();
53         } catch (Exception e) {
54             Log.w(TAG, "fromBytes(): " + e);
55             return null;
56         }
57     }
58 
toMd5(byte[] bytes)59     static String toMd5(byte[] bytes) {
60         try {
61             MessageDigest algorithm = MessageDigest.getInstance("MD5");
62             algorithm.reset();
63             algorithm.update(bytes);
64             return toHexString(algorithm.digest(), "");
65         } catch(NoSuchAlgorithmException e){
66             // should not occur
67             Log.w(TAG, "toMd5(): " + e);
68             throw new RuntimeException(e);
69         }
70     }
71 
toHexString(byte[] bytes, String separator)72     private static String toHexString(byte[] bytes, String separator) {
73         StringBuilder hexString = new StringBuilder();
74         for (byte b : bytes) {
75             hexString.append(Integer.toHexString(0xFF & b)).append(separator);
76         }
77         return hexString.toString();
78     }
79 
readFile(File file)80     static byte[] readFile(File file) {
81         try {
82             byte[] data = new byte[(int) file.length()];
83             FileInputStream fis = new FileInputStream(file);
84             fis.read(data);
85             fis.close();
86             return data;
87         } catch (Exception e) {
88             Log.w(TAG, "cert file read error: " + e);
89             return null;
90         }
91     }
92 
deleteFile(File file)93     static boolean deleteFile(File file) {
94         if ((file != null) && !file.delete()) {
95             Log.w(TAG, "cannot delete cert: " + file);
96             return false;
97         } else {
98             return true;
99         }
100     }
101 
Util()102     private Util() {
103     }
104 }
105