1 | |
package org.apache.commons.openpgp; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
import java.io.IOException; |
21 | |
import java.io.InputStream; |
22 | |
import java.util.HashMap; |
23 | |
import java.util.Map; |
24 | |
|
25 | |
import org.bouncycastle.openpgp.PGPException; |
26 | |
import org.bouncycastle.openpgp.PGPObjectFactory; |
27 | |
import org.bouncycastle.openpgp.PGPPublicKey; |
28 | |
import org.bouncycastle.openpgp.PGPPublicKeyRing; |
29 | |
import org.bouncycastle.openpgp.PGPSecretKey; |
30 | |
import org.bouncycastle.openpgp.PGPSecretKeyRing; |
31 | |
import org.bouncycastle.openpgp.PGPUtil; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public class BouncyCastleKeyRing implements KeyRing |
40 | |
{ |
41 | |
private String firstKeyId; |
42 | |
|
43 | 22 | private final Map<Long, PGPSecretKey> pgpSec = new HashMap<Long, PGPSecretKey>(); |
44 | |
|
45 | |
private char[] password; |
46 | |
|
47 | 22 | private final Map<Long, PGPPublicKey> pgpPub = new HashMap<Long, PGPPublicKey>(); |
48 | |
|
49 | |
private static final long MASK = 0xFFFFFFFFL; |
50 | |
|
51 | |
public BouncyCastleKeyRing() |
52 | 0 | { |
53 | 0 | } |
54 | |
|
55 | |
public BouncyCastleKeyRing( InputStream secretKeyRingStream, InputStream publicKeyRingStream, char[] password ) |
56 | |
throws IOException, PGPException |
57 | 22 | { |
58 | 22 | addSecretKeyRing( secretKeyRingStream, password ); |
59 | |
|
60 | 22 | addPublicKeyRing( publicKeyRingStream ); |
61 | 22 | } |
62 | |
|
63 | |
public void addPublicKeyRing( InputStream publicKeyRingStream ) |
64 | |
throws IOException, PGPException |
65 | |
{ |
66 | 22 | PGPObjectFactory pgpFact = new PGPObjectFactory( PGPUtil.getDecoderStream( publicKeyRingStream ) ); |
67 | |
Object obj; |
68 | |
|
69 | 88 | while ( ( obj = pgpFact.nextObject() ) != null ) |
70 | |
{ |
71 | 66 | if ( !( obj instanceof PGPPublicKeyRing ) ) |
72 | |
{ |
73 | 0 | throw new PGPException( obj.getClass().getName() + " found where PGPPublicKeyRing expected" ); |
74 | |
} |
75 | |
|
76 | 66 | PGPPublicKeyRing keyRing = (PGPPublicKeyRing) obj; |
77 | 66 | long key = keyRing.getPublicKey().getKeyID() & MASK; |
78 | |
|
79 | 66 | pgpPub.put( key, keyRing.getPublicKey() ); |
80 | 66 | } |
81 | 22 | } |
82 | |
|
83 | |
public void addSecretKeyRing( InputStream secretKeyRingStream, char[] password ) |
84 | |
throws IOException, PGPException |
85 | |
{ |
86 | 22 | PGPObjectFactory pgpFact = new PGPObjectFactory( PGPUtil.getDecoderStream( secretKeyRingStream ) ); |
87 | |
Object obj; |
88 | |
|
89 | 66 | while ( ( obj = pgpFact.nextObject() ) != null ) |
90 | |
{ |
91 | 44 | if ( !( obj instanceof PGPSecretKeyRing ) ) |
92 | |
{ |
93 | 0 | throw new PGPException( obj.getClass().getName() + " found where PGPSecretKeyRing expected" ); |
94 | |
} |
95 | |
|
96 | 44 | PGPSecretKeyRing pgpSecret = (PGPSecretKeyRing) obj; |
97 | 44 | long key = pgpSecret.getSecretKey().getKeyID() & MASK; |
98 | 44 | if ( pgpSec.isEmpty() ) |
99 | |
{ |
100 | |
|
101 | 22 | firstKeyId = Long.toHexString( key ).toUpperCase(); |
102 | |
} |
103 | |
|
104 | 44 | pgpSec.put( key, pgpSecret.getSecretKey() ); |
105 | 44 | } |
106 | |
|
107 | 22 | this.password = password; |
108 | 22 | } |
109 | |
|
110 | |
public String getFirstKeyId() |
111 | |
{ |
112 | 0 | return firstKeyId.toString(); |
113 | |
} |
114 | |
|
115 | |
public char[] getPassword() |
116 | |
{ |
117 | 4 | return password; |
118 | |
} |
119 | |
|
120 | |
public PGPSecretKey getSecretKey( String keyId ) |
121 | |
{ |
122 | 6 | return pgpSec.get( Long.valueOf( keyId, 16 ) ); |
123 | |
} |
124 | |
|
125 | |
public PGPPublicKey getPublicKey( String keyId ) |
126 | |
{ |
127 | 3 | return pgpPub.get( Long.valueOf( keyId, 16 ) ); |
128 | |
} |
129 | |
|
130 | |
public PGPSecretKey getSecretKey( long keyId ) |
131 | |
{ |
132 | 0 | return pgpSec.get(keyId & MASK); |
133 | |
} |
134 | |
|
135 | |
public PGPPublicKey getPublicKey( long keyId ) |
136 | |
{ |
137 | 23 | return pgpPub.get(keyId & MASK); |
138 | |
} |
139 | |
} |