OpenSSLDigest.java revision ed8b1c77e5a631584cd74382a645c9ada09c155f
1/*
2 * Copyright (C) 2008 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;
20import java.security.DigestException;
21import java.security.MessageDigest;
22
23/**
24 * Implements the BouncyCastle Digest interface using OpenSSL's EVP API.
25 */
26public class OpenSSLDigest implements Digest {
27    private final MessageDigest delegate;
28
29    public OpenSSLDigest(String algorithm) {
30        try {
31            delegate = MessageDigest.getInstance(algorithm, "AndroidOpenSSL");
32        } catch (Exception e) {
33            throw new RuntimeException(e);
34        }
35    }
36
37    public String getAlgorithmName() {
38        return delegate.getAlgorithm();
39    }
40
41    public int getDigestSize() {
42        return delegate.getDigestLength();
43    }
44
45    public void reset() {
46        delegate.reset();
47    }
48
49    public void update(byte in) {
50        delegate.update(in);
51    }
52
53    public void update(byte[] in, int inOff, int len) {
54        delegate.update(in, inOff, len);
55    }
56
57    public int doFinal(byte[] out, int outOff) {
58        try {
59            return delegate.digest(out, outOff, out.length);
60        } catch (DigestException e) {
61            throw new RuntimeException(e);
62        }
63    }
64
65    public static class MD5 extends OpenSSLDigest {
66        public MD5() { super("MD5"); }
67    }
68
69    public static class SHA1 extends OpenSSLDigest {
70        public SHA1() { super("SHA-1"); }
71    }
72
73    public static class SHA224 extends OpenSSLDigest {
74        public SHA224() { super("SHA-224"); }
75    }
76
77    public static class SHA256 extends OpenSSLDigest {
78        public SHA256() { super("SHA-256"); }
79    }
80
81    public static class SHA384 extends OpenSSLDigest {
82        public SHA384() { super("SHA-384"); }
83    }
84
85    public static class SHA512 extends OpenSSLDigest {
86        public SHA512() { super("SHA-512"); }
87    }
88}
89