aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java
blob: 58601c49a1393f8de3fe16685debbda998140223 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package org.sufficientlysecure.keychain.pgp;

import org.spongycastle.openpgp.PGPObjectFactory;
import org.spongycastle.openpgp.PGPPublicKeyRing;
import org.spongycastle.openpgp.PGPSecretKeyRing;
import org.spongycastle.openpgp.PGPUtil;
import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

public class UncachedKeyRing {

    final PGPPublicKeyRing mPublicRing;
    final PGPSecretKeyRing mSecretRing;

    UncachedKeyRing(PGPPublicKeyRing publicRing, PGPSecretKeyRing secretRing) {
        mPublicRing = publicRing;
        mSecretRing = secretRing;
    }

    UncachedKeyRing(PGPPublicKeyRing publicRing) {
        this(publicRing, null);
    }

    public PGPPublicKeyRing getPublicRing() {
        return mPublicRing;
    }

    public PGPSecretKeyRing getSecretRing() {
        return mSecretRing;
    }

    public byte[] getFingerprint() {
        return mPublicRing.getPublicKey().getFingerprint();
    }

    public static UncachedKeyRing decodePubkeyFromData(byte[] data)
            throws PgpGeneralException, IOException {
        BufferedInputStream bufferedInput =
                new BufferedInputStream(new ByteArrayInputStream(data));
        if (bufferedInput.available() > 0) {
            InputStream in = PGPUtil.getDecoderStream(bufferedInput);
            PGPObjectFactory objectFactory = new PGPObjectFactory(in);

            // get first object in block
            Object obj;
            if ((obj = objectFactory.nextObject()) != null && obj instanceof PGPPublicKeyRing) {
                return new UncachedKeyRing((PGPPublicKeyRing) obj);
            } else {
                throw new PgpGeneralException("Object not recognized as PGPPublicKeyRing!");
            }
        } else {
            throw new PgpGeneralException("Object not recognized as PGPPublicKeyRing!");
        }
    }

}