Home | History | Annotate | Download | only in certinstaller
      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     static byte[] toBytes(Object object) {
     34         ByteArrayOutputStream baos = new ByteArrayOutputStream();
     35         try {
     36             ObjectOutputStream os = new ObjectOutputStream(baos);
     37             os.writeObject(object);
     38             os.close();
     39         } catch (Exception e) {
     40             Log.w(TAG, "toBytes(): " + e + ": " + object);
     41         }
     42         return baos.toByteArray();
     43     }
     44 
     45     static <T> T fromBytes(byte[] bytes) {
     46         if (bytes == null) return null;
     47         try {
     48             ObjectInputStream is =
     49                     new ObjectInputStream(new ByteArrayInputStream(bytes));
     50             return (T) is.readObject();
     51         } catch (Exception e) {
     52             Log.w(TAG, "fromBytes(): " + e);
     53             return null;
     54         }
     55     }
     56 
     57     static String toMd5(byte[] bytes) {
     58         try {
     59             MessageDigest algorithm = MessageDigest.getInstance("MD5");
     60             algorithm.reset();
     61             algorithm.update(bytes);
     62             return toHexString(algorithm.digest(), "");
     63         } catch(NoSuchAlgorithmException e){
     64             // should not occur
     65             Log.w(TAG, "toMd5(): " + e);
     66             throw new RuntimeException(e);
     67         }
     68     }
     69 
     70     private static String toHexString(byte[] bytes, String separator) {
     71         StringBuilder hexString = new StringBuilder();
     72         for (byte b : bytes) {
     73             hexString.append(Integer.toHexString(0xFF & b)).append(separator);
     74         }
     75         return hexString.toString();
     76     }
     77 
     78     static byte[] readFile(File file) {
     79         try {
     80             byte[] data = new byte[(int) file.length()];
     81             FileInputStream fis = new FileInputStream(file);
     82             fis.read(data);
     83             fis.close();
     84             return data;
     85         } catch (Exception e) {
     86             Log.w(TAG, "cert file read error: " + e);
     87             return null;
     88         }
     89     }
     90 
     91     static boolean deleteFile(File file) {
     92         if ((file != null) && !file.delete()) {
     93             Log.w(TAG, "cannot delete cert: " + file);
     94             return false;
     95         } else {
     96             return true;
     97         }
     98     }
     99 
    100     private Util() {
    101     }
    102 }
    103