@@ -81,12 +81,12 @@ public class TestKeyFactory {
8181 "QKBW1TS0/2iB9zNkFMj5/+h7l2oqTT7sSQIDAQAB" ;
8282
8383
84- private static final PrivateKey CUSTOM_PRIV ;
85- private static final PublicKey CUSTOM_PUB ;
84+ private static final PrivateKey P1_PRIV ;
85+ private static final PublicKey P1_PUB ;
8686
8787 static {
8888 byte [] encodedPriv = Base64 .getDecoder ().decode (PKCS1_PRIV_STR );
89- CUSTOM_PRIV = new PrivateKey () {
89+ P1_PRIV = new PrivateKey () {
9090 @ Override
9191 public String getAlgorithm () {
9292 return "RSA" ;
@@ -102,7 +102,7 @@ public byte[] getEncoded() {
102102 }
103103 };
104104 byte [] encodedPub = Base64 .getDecoder ().decode (PKCS1_PUB_STR );
105- CUSTOM_PUB = new PublicKey () {
105+ P1_PUB = new PublicKey () {
106106 @ Override
107107 public String getAlgorithm () {
108108 return "RSA" ;
@@ -145,31 +145,30 @@ private static void testKey(Key key1, Key key2) throws Exception {
145145 }
146146 }
147147 // skip equals check when key1 is custom key
148- if (key1 != CUSTOM_PRIV && key1 != CUSTOM_PUB ) {
148+ if (key1 != P1_PRIV && key1 != P1_PUB ) {
149149 if (!key1 .equals (key2 )) {
150150 throw new Exception ("Keys not equal" );
151151 }
152152 }
153153
154154 // only compare encodings if keys are of the same format
155- if (key1 .getFormat ().equals (key2 .getFormat ())) {
156- if (!Arrays .equals (key1 .getEncoded (), key2 .getEncoded ())) {
157- throw new Exception ("Encodings not equal" );
158- }
155+ if (key1 .getFormat ().equals (key2 .getFormat ()) &&
156+ !Arrays .equals (key1 .getEncoded (), key2 .getEncoded ())) {
157+ throw new Exception ("Encodings not equal" );
159158 }
160159 }
161160
162161 private static void testPublic (KeyFactory kf , PublicKey key )
163162 throws Exception {
164- System .out .println ("Testing " + (key == CUSTOM_PUB ? "PKCS#1" : "" ) +
163+ System .out .println ("Testing " + (key == P1_PUB ? "PKCS#1" : "" ) +
165164 " public key..." );
166165
167166 PublicKey key2 = (PublicKey )kf .translateKey (key );
168167 KeySpec rsaSpec = kf .getKeySpec (key , RSAPublicKeySpec .class );
169168 PublicKey key3 = kf .generatePublic (rsaSpec );
170169 KeySpec x509Spec = kf .getKeySpec (key , X509EncodedKeySpec .class );
171170 PublicKey key4 = kf .generatePublic (x509Spec );
172- if (key != CUSTOM_PUB ) {
171+ if (key != P1_PUB ) {
173172 testKey (key , key );
174173 }
175174 testKey (key , key2 );
@@ -185,14 +184,14 @@ private static void testPublic(KeyFactory kf, PublicKey key)
185184
186185 private static void testPrivate (KeyFactory kf , PrivateKey key )
187186 throws Exception {
188- System .out .println ("Testing " + (key == CUSTOM_PRIV ? "PKCS#1" : "" ) +
187+ System .out .println ("Testing " + (key == P1_PRIV ? "PKCS#1" : "" ) +
189188 " private key..." );
190189 PrivateKey key2 = (PrivateKey )kf .translateKey (key );
191190 KeySpec rsaSpec = kf .getKeySpec (key , RSAPrivateCrtKeySpec .class );
192191 PrivateKey key3 = kf .generatePrivate (rsaSpec );
193192 KeySpec pkcs8Spec = kf .getKeySpec (key , PKCS8EncodedKeySpec .class );
194193 PrivateKey key4 = kf .generatePrivate (pkcs8Spec );
195- if (key != CUSTOM_PRIV ) {
194+ if (key != P1_PRIV ) {
196195 testKey (key , key );
197196 }
198197 testKey (key , key2 );
@@ -240,8 +239,8 @@ public static void main(String[] args) throws Exception {
240239 }
241240 }
242241 // repeat the test w/ PKCS#1 RSA Private Key
243- test (kf , CUSTOM_PRIV );
244- test (kf , CUSTOM_PUB );
242+ test (kf , P1_PRIV );
243+ test (kf , P1_PUB );
245244
246245 long stop = System .currentTimeMillis ();
247246 System .out .println ("All tests passed (" + (stop - start ) + " ms)." );
0 commit comments