99import java .util .Calendar ;
1010import java .util .Date ;
1111import java .util .Iterator ;
12+ import java .util .TimeZone ;
1213
1314import org .bouncycastle .bcpg .AEADAlgorithmTags ;
1415import org .bouncycastle .bcpg .ArmoredInputStream ;
2829import org .bouncycastle .crypto .params .X25519KeyGenerationParameters ;
2930import org .bouncycastle .jce .provider .BouncyCastleProvider ;
3031import org .bouncycastle .openpgp .PGPEncryptedDataList ;
32+ import org .bouncycastle .openpgp .PGPLiteralData ;
3133import org .bouncycastle .openpgp .PGPObjectFactory ;
3234import org .bouncycastle .openpgp .PGPPBEEncryptedData ;
3335import org .bouncycastle .openpgp .PGPPublicKey ;
3436import org .bouncycastle .openpgp .bc .BcPGPObjectFactory ;
3537import org .bouncycastle .openpgp .operator .bc .BcPGPKeyPair ;
38+ import org .bouncycastle .openpgp .operator .jcajce .JcaKeyFingerprintCalculator ;
3639import org .bouncycastle .util .Arrays ;
3740import org .bouncycastle .util .Pack ;
3841import org .bouncycastle .util .Strings ;
4144public class BcpgGeneralTest
4245 extends SimpleTest
4346{
47+ /*
48+ * Format: Binary data
49+ Filename: "hello.txt"
50+ Timestamp: 2104-06-26 14:42:55 UTC
51+ Content: "Hello, world!\n"
52+ * */
53+ byte [] message = Strings .toUTF8ByteArray ("-----BEGIN PGP MESSAGE-----\n " +
54+ "\n " +
55+ "yx1iCWhlbGxvLnR4dPz1TW9IZWxsbywgd29ybGQhCg==\n " +
56+ "=3swl\n " +
57+ "-----END PGP MESSAGE-----" );
58+
59+
60+
4461 public static void main (String [] args )
4562 {
4663 Security .addProvider (new BouncyCastleProvider ());
@@ -59,18 +76,25 @@ public void performTest()
5976 throws Exception
6077 {
6178 testReadTime ();
79+ testReadTime2 ();
6280 //testS2K();
6381 testExceptions ();
6482 testECDHPublicBCPGKey ();
6583 // Tests for PreferredAEADCiphersuites
6684 testPreferredAEADCiphersuites ();
6785 }
6886
87+ static int read4OctetLength (InputStream in )
88+ throws IOException
89+ {
90+ return (in .read () << 24 ) | (in .read () << 16 ) | (in .read () << 8 ) | in .read ();
91+ }
92+
6993 // StreamUtil.readTime
7094 static long readTime (BCPGInputStream in )
7195 throws IOException
7296 {
73- return ((( long ) in . read () << 24 | in . read () << 16 | in . read () << 8 | in . read () ) & 0xFFFFFFFFL ) * 1000L ;
97+ return ((long )read4OctetLength ( in ) & 0xFFFFFFFFL ) * 1000L ;
7498 }
7599
76100 public void testReadTime ()
@@ -98,6 +122,22 @@ public void testReadTime()
98122 isTrue (Arrays .areEqual (date , date2 ));
99123 }
100124
125+ public void testReadTime2 ()
126+ throws Exception
127+ {
128+ PGPObjectFactory pgpObjectFactoryOfTestFile = new PGPObjectFactory (
129+ new ArmoredInputStream (new ByteArrayInputStream (message )), new JcaKeyFingerprintCalculator ());
130+ PGPLiteralData ld = (PGPLiteralData )pgpObjectFactoryOfTestFile .nextObject ();
131+ Date date = ld .getModificationTime ();
132+
133+ Calendar calendar = Calendar .getInstance (TimeZone .getTimeZone ("UTC" ));
134+ calendar .set (2104 , Calendar .JUNE , 26 , 14 , 42 , 55 );
135+ calendar .set (Calendar .MILLISECOND , 0 );
136+ Date expected = calendar .getTime ();
137+
138+ isTrue (date .equals (expected ));
139+ }
140+
101141 public void testPreferredAEADCiphersuites ()
102142 throws Exception
103143 {
0 commit comments