AndroidDigestFactory.java revision 580c719a4c5ff483af625fcffab41678e091971d
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            // Double check for NativeCrypto in case we are running on RI for testing
37            Class.forName("com.android.org.conscrypt.NativeCrypto");
38        } catch (ClassNotFoundException e1) {
39            try {
40                factoryImplementationClass = Class.forName(BouncyCastleFactoryClassName);
41            } catch (ClassNotFoundException e2) {
42                AssertionError e = new AssertionError("Failed to load "
43                                         + "AndroidDigestFactoryInterface "
44                                         + "implementation. Looked for "
45                                         + OpenSSLFactoryClassName + " and "
46                                         + BouncyCastleFactoryClassName);
47                e.initCause(e1);
48                throw e;
49            }
50        }
51        if (!AndroidDigestFactoryInterface.class.isAssignableFrom(factoryImplementationClass)) {
52            throw new AssertionError(factoryImplementationClass
53                                     + "does not implement AndroidDigestFactoryInterface");
54        }
55        try {
56            FACTORY = (AndroidDigestFactoryInterface) factoryImplementationClass.newInstance();
57        } catch (InstantiationException e) {
58            throw new AssertionError(e);
59        } catch (IllegalAccessException e) {
60            throw new AssertionError(e);
61        }
62    }
63
64    public static Digest getMD5() {
65        return FACTORY.getMD5();
66    }
67
68    public static Digest getSHA1() {
69        return FACTORY.getSHA1();
70    }
71
72    public static Digest getSHA256() {
73        return FACTORY.getSHA256();
74    }
75
76    public static Digest getSHA384() {
77        return FACTORY.getSHA384();
78    }
79
80    public static Digest getSHA512() {
81        return FACTORY.getSHA512();
82    }
83}
84