AndroidDigestFactory.java revision e6bf3e8dfa2804891a82075cb469b736321b4827
1/* 2 * Copyright (C) 2012 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 org.bouncycastle.crypto.digests; 18 19import org.bouncycastle.crypto.Digest; 20 21/** 22 * Level of indirection to let us select OpenSSLDigest implementations 23 * for libcore but fallback to BouncyCastle ones on the RI. 24 */ 25public final class AndroidDigestFactory { 26 private static final String OpenSSLFactoryClassName 27 = AndroidDigestFactory.class.getName() + "OpenSSL"; 28 private static final String BouncyCastleFactoryClassName 29 = AndroidDigestFactory.class.getName() + "BouncyCastle"; 30 31 private static final AndroidDigestFactoryInterface FACTORY; 32 static { 33 Class factoryImplementationClass; 34 try { 35 factoryImplementationClass = Class.forName(OpenSSLFactoryClassName); 36 } catch (ClassNotFoundException e1) { 37 try { 38 factoryImplementationClass = Class.forName(BouncyCastleFactoryClassName); 39 } catch (ClassNotFoundException e2) { 40 throw new AssertionError("Failed to find AndroidDigestFactoryInterface " 41 + "implementation. Looked for " 42 + OpenSSLFactoryClassName + " and " 43 + BouncyCastleFactoryClassName); 44 } 45 } 46 if (!AndroidDigestFactoryInterface.class.isAssignableFrom(factoryImplementationClass)) { 47 throw new AssertionError(factoryImplementationClass 48 + "does not implement AndroidDigestFactoryInterface"); 49 } 50 try { 51 FACTORY = (AndroidDigestFactoryInterface) factoryImplementationClass.newInstance(); 52 } catch (InstantiationException e) { 53 throw new AssertionError(e); 54 } catch (IllegalAccessException e) { 55 throw new AssertionError(e); 56 } 57 } 58 59 public static Digest getMD5() { 60 return FACTORY.getMD5(); 61 } 62 63 public static Digest getSHA1() { 64 return FACTORY.getSHA1(); 65 } 66 67 public static Digest getSHA256() { 68 return FACTORY.getSHA256(); 69 } 70 71 public static Digest getSHA384() { 72 return FACTORY.getSHA384(); 73 } 74 75 public static Digest getSHA512() { 76 return FACTORY.getSHA512(); 77 } 78} 79