forked from neuhalje/bouncy-gpg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BouncyGPG.java
90 lines (80 loc) · 2.98 KB
/
BouncyGPG.java
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package name.neuhalfen.projects.crypto.bouncycastle.openpgp;
import java.security.Security;
import name.neuhalfen.projects.crypto.bouncycastle.openpgp.keys.generation.KeyRingBuilder;
import name.neuhalfen.projects.crypto.bouncycastle.openpgp.keys.generation.KeyRingBuilderImpl;
import name.neuhalfen.projects.crypto.bouncycastle.openpgp.keys.generation.SimpleKeyRingBuilder;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
@SuppressWarnings({"PMD.AtLeastOneConstructor", "PMD.AccessorMethodGeneration", "PMD.LawOfDemeter",
"PMD.ClassNamingConventions"})
public final class BouncyGPG {
private BouncyGPG() {
}
/**
* Entry point for stream based decryption. Ultimately an encryption output stream is placed
* before a user supplied output stream so that plaintext written to the encryption stream is
* encrypted and written to the user supplied output stream. . Example:
* https://github.com/neuhalje/bouncy-gpg/tree/master/examples/decrypt . Usage:
* <pre>
* final
* OutputStream encryptionStream = BouncyGPG.encryptToStream()
* .withConfig(Configs.keyringConfigFromFilesForSender())
* .withDefaultAlgorithms()
* .toRecipient("[email protected]")
* .andSignWith("[email protected]")
* .armorAsciiOutput()
* .andWriteTo(cipherText);
*
* encryptionStream.write(expectedPlaintext);
* encryptionStream.close();
* cipherText.close();
* </pre>
*
* @return The next build step. In the end the encryption stream.
*/
public static BuildDecryptionInputStreamAPI decryptAndVerifyStream() {
return new BuildDecryptionInputStreamAPI();
}
/**
* <p>Entry point for stream based encryption. Ultimately a decrypting input stream is placed
* before
* a user supplied stream with encrypted data.
* </p><p>
* Example: https://github.com/neuhalje/bouncy-gpg/tree/master/examples/encrypt
* </p>
*
* @return The next build step. In the end the decryption stream.
*/
public static BuildEncryptionOutputStreamAPI encryptToStream() {
return new BuildEncryptionOutputStreamAPI();
}
/**
* Generate a new OpenPGP key ring.
*
* @return builder
*/
public static KeyRingBuilder createKeyring() {
return new KeyRingBuilderImpl();
}
/**
* Generate a new OpenPGP key ring.
*
* @return builder
*/
public static SimpleKeyRingBuilder createSimpleKeyring() {
return new KeyRingBuilderImpl();
}
/**
* <p>Register the BouncyCastle provider as first provider. If another instance of the
* BouncyCastle provider is already registered it is removed.
* </p>
* <p>The BouncyCastle provider needs to be registered for BouncyGPG to work.</p>
* <p>
* This procedure also makes it possible to use BC on older Android devices that ship their own BC
* implementation.
* </p>
*/
public static synchronized void registerProvider() {
Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME);
Security.insertProviderAt(new BouncyCastleProvider(), 0);
}
}