1 /** 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package com.android.inputmethod.dictionarypack; 18 19 import java.io.InputStream; 20 import java.io.IOException; 21 import java.security.MessageDigest; 22 23 public final class MD5Calculator { MD5Calculator()24 private MD5Calculator() {} // This helper class is not instantiable 25 checksum(final InputStream in)26 public static String checksum(final InputStream in) throws IOException { 27 // This code from the Android documentation for MessageDigest. Nearly verbatim. 28 MessageDigest digester; 29 try { 30 digester = MessageDigest.getInstance("MD5"); 31 } catch (java.security.NoSuchAlgorithmException e) { 32 return null; // Platform does not support MD5 : can't check, so return null 33 } 34 final byte[] bytes = new byte[8192]; 35 int byteCount; 36 while ((byteCount = in.read(bytes)) > 0) { 37 digester.update(bytes, 0, byteCount); 38 } 39 final byte[] digest = digester.digest(); 40 final StringBuilder s = new StringBuilder(); 41 for (int i = 0; i < digest.length; ++i) { 42 s.append(String.format("%1$02x", digest[i])); 43 } 44 return s.toString(); 45 } 46 } 47