2
2
3
3
import org .bouncycastle .bcpg .EdDSAPublicBCPGKey ;
4
4
import org .bouncycastle .bcpg .EdSecretBCPGKey ;
5
+ import org .bouncycastle .bcpg .HashAlgorithmTags ;
5
6
import org .bouncycastle .bcpg .PublicKeyAlgorithmTags ;
6
7
import org .bouncycastle .crypto .AsymmetricCipherKeyPair ;
7
8
import org .bouncycastle .crypto .generators .Ed25519KeyPairGenerator ;
8
9
import org .bouncycastle .crypto .params .Ed25519KeyGenerationParameters ;
9
10
import org .bouncycastle .jcajce .spec .EdDSAParameterSpec ;
10
11
import org .bouncycastle .jce .provider .BouncyCastleProvider ;
11
12
import org .bouncycastle .openpgp .PGPException ;
13
+ import org .bouncycastle .openpgp .PGPKeyPair ;
14
+ import org .bouncycastle .openpgp .PGPSignature ;
15
+ import org .bouncycastle .openpgp .PGPSignatureGenerator ;
16
+ import org .bouncycastle .openpgp .operator .PGPContentSignerBuilder ;
17
+ import org .bouncycastle .openpgp .operator .PGPContentVerifierBuilderProvider ;
18
+ import org .bouncycastle .openpgp .operator .bc .BcPGPContentSignerBuilder ;
19
+ import org .bouncycastle .openpgp .operator .bc .BcPGPContentVerifierBuilderProvider ;
12
20
import org .bouncycastle .openpgp .operator .bc .BcPGPKeyPair ;
21
+ import org .bouncycastle .openpgp .operator .jcajce .JcaPGPContentSignerBuilder ;
22
+ import org .bouncycastle .openpgp .operator .jcajce .JcaPGPContentVerifierBuilderProvider ;
13
23
import org .bouncycastle .openpgp .operator .jcajce .JcaPGPKeyPair ;
14
24
15
25
import java .io .IOException ;
26
+ import java .nio .charset .StandardCharsets ;
16
27
import java .security .*;
17
28
import java .util .Date ;
18
29
@@ -31,6 +42,60 @@ public void performTest()
31
42
{
32
43
testConversionOfJcaKeyPair ();
33
44
testConversionOfBcKeyPair ();
45
+ testV4SigningVerificationWithJcaKey ();
46
+ testV4SigningVerificationWithBcKey ();
47
+ }
48
+
49
+ private void testV4SigningVerificationWithJcaKey ()
50
+ throws NoSuchAlgorithmException , InvalidAlgorithmParameterException , PGPException
51
+ {
52
+ Date date = currentTimeRounded ();
53
+ KeyPairGenerator gen = KeyPairGenerator .getInstance ("EDDSA" , new BouncyCastleProvider ());
54
+ gen .initialize (new EdDSAParameterSpec ("Ed25519" ));
55
+ KeyPair kp = gen .generateKeyPair ();
56
+ PGPKeyPair keyPair = new JcaPGPKeyPair (PublicKeyAlgorithmTags .EDDSA_LEGACY , kp , date );
57
+
58
+ byte [] data = "Hello, World!\n " .getBytes (StandardCharsets .UTF_8 );
59
+
60
+ PGPContentSignerBuilder contSigBuilder = new JcaPGPContentSignerBuilder (
61
+ keyPair .getPublicKey ().getAlgorithm (),
62
+ HashAlgorithmTags .SHA512 )
63
+ .setProvider (new BouncyCastleProvider ());
64
+ PGPSignatureGenerator sigGen = new PGPSignatureGenerator (contSigBuilder );
65
+ sigGen .init (PGPSignature .BINARY_DOCUMENT , keyPair .getPrivateKey ());
66
+ sigGen .update (data );
67
+ PGPSignature signature = sigGen .generate ();
68
+
69
+ PGPContentVerifierBuilderProvider contVerBuilder = new JcaPGPContentVerifierBuilderProvider ()
70
+ .setProvider (new BouncyCastleProvider ());
71
+ signature .init (contVerBuilder , keyPair .getPublicKey ());
72
+ signature .update (data );
73
+ isTrue (signature .verify ());
74
+ }
75
+
76
+ private void testV4SigningVerificationWithBcKey ()
77
+ throws PGPException
78
+ {
79
+ Date date = currentTimeRounded ();
80
+ Ed25519KeyPairGenerator gen = new Ed25519KeyPairGenerator ();
81
+ gen .init (new Ed25519KeyGenerationParameters (new SecureRandom ()));
82
+ AsymmetricCipherKeyPair kp = gen .generateKeyPair ();
83
+ BcPGPKeyPair keyPair = new BcPGPKeyPair (PublicKeyAlgorithmTags .EDDSA_LEGACY , kp , date );
84
+
85
+ byte [] data = "Hello, World!\n " .getBytes (StandardCharsets .UTF_8 );
86
+
87
+ PGPContentSignerBuilder contSigBuilder = new BcPGPContentSignerBuilder (
88
+ keyPair .getPublicKey ().getAlgorithm (),
89
+ HashAlgorithmTags .SHA512 );
90
+ PGPSignatureGenerator sigGen = new PGPSignatureGenerator (contSigBuilder );
91
+ sigGen .init (PGPSignature .BINARY_DOCUMENT , keyPair .getPrivateKey ());
92
+ sigGen .update (data );
93
+ PGPSignature signature = sigGen .generate ();
94
+
95
+ PGPContentVerifierBuilderProvider contVerBuilder = new BcPGPContentVerifierBuilderProvider ();
96
+ signature .init (contVerBuilder , keyPair .getPublicKey ());
97
+ signature .update (data );
98
+ isTrue (signature .verify ());
34
99
}
35
100
36
101
private void testConversionOfJcaKeyPair ()
0 commit comments