1/* 2 * Copyright (C) 2007 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 17package android.security; 18 19import java.security.NoSuchAlgorithmException; 20 21/** 22 * Base class for producing a message digest from different hash encryptions. 23 */ 24public abstract class MessageDigest 25{ 26 /** 27 * Returns a digest object of the specified type. 28 * 29 * @param algorithm The type of hash function to use. Valid values are 30 * <em>SHA-1</em> and <em>MD5</em>. 31 * @return The respective MessageDigest object. Either a 32 * {@link android.security.Sha1MessageDigest} or 33 * {@link android.security.Md5MessageDigest} object. 34 * @throws NoSuchAlgorithmException If an invalid <var>algorithm</var> 35 * is given. 36 */ 37 public static MessageDigest getInstance(String algorithm) 38 throws NoSuchAlgorithmException 39 { 40 if (algorithm == null) { 41 return null; 42 } 43 44 if (algorithm.equals("SHA-1")) { 45 return new Sha1MessageDigest(); 46 } 47 else if (algorithm.equals("MD5")) { 48 return new Md5MessageDigest(); 49 } 50 51 throw new NoSuchAlgorithmException(); 52 } 53 54 public abstract void update(byte[] input); 55 public abstract byte[] digest(); 56 57 /** 58 * Produces a message digest for the given input. 59 * 60 * @param input The message to encrypt. 61 * @return The digest (hash sum). 62 */ 63 public abstract byte[] digest(byte[] input); 64} 65