Author: ogusakov
Date: Mon Mar 30 18:12:22 2009
New Revision: 760083

URL: http://svn.apache.org/viewvc?rev=760083&view=rev
Log:
[NXCM-601] - verifyStream() helper method to PgpHelper for quick binary 
verification

Modified:
    
maven/mercury/trunk/mercury-crypto/mercury-crypto-basic/src/main/java/org/apache/maven/mercury/crypto/pgp/PgpHelper.java
    
maven/mercury/trunk/mercury-crypto/mercury-crypto-basic/src/test/java/org/apache/maven/mercury/crypto/pgp/PgpStreamVerifierTest.java

Modified: 
maven/mercury/trunk/mercury-crypto/mercury-crypto-basic/src/main/java/org/apache/maven/mercury/crypto/pgp/PgpHelper.java
URL: 
http://svn.apache.org/viewvc/maven/mercury/trunk/mercury-crypto/mercury-crypto-basic/src/main/java/org/apache/maven/mercury/crypto/pgp/PgpHelper.java?rev=760083&r1=760082&r2=760083&view=diff
==============================================================================
--- 
maven/mercury/trunk/mercury-crypto/mercury-crypto-basic/src/main/java/org/apache/maven/mercury/crypto/pgp/PgpHelper.java
 (original)
+++ 
maven/mercury/trunk/mercury-crypto/mercury-crypto-basic/src/main/java/org/apache/maven/mercury/crypto/pgp/PgpHelper.java
 Mon Mar 30 18:12:22 2009
@@ -26,6 +26,9 @@
 import java.security.Security;
 import java.util.Iterator;
 
+import org.apache.maven.mercury.crypto.api.StreamObserverException;
+import org.apache.maven.mercury.crypto.api.StreamVerifierAttributes;
+import org.apache.maven.mercury.crypto.api.StreamVerifierException;
 import org.bouncycastle.jce.provider.BouncyCastleProvider;
 import org.bouncycastle.openpgp.PGPCompressedData;
 import org.bouncycastle.openpgp.PGPException;
@@ -48,6 +51,9 @@
 {
   public static final String PROVIDER = "BC";
   public static final String EXTENSION = "asc";
+  
+  private static final byte [] _buf = new byte[10240];
+
 
   private static final Language LANG = new DefaultLanguage( PgpHelper.class );
   
//---------------------------------------------------------------------------------
@@ -174,5 +180,27 @@
     }
   }
   
//---------------------------------------------------------------------------------
+  public static final boolean verifyStream( InputStream in, InputStream asc, 
InputStream publicKeyRingStream )
+  throws IOException, StreamObserverException
+  {
+      StreamVerifierAttributes attributes = new 
StreamVerifierAttributes(PgpStreamVerifierFactory.DEFAULT_EXTENSION, true, 
true);
+      
+      PgpStreamVerifierFactory svf = new PgpStreamVerifierFactory( attributes, 
publicKeyRingStream );
+
+      PgpStreamVerifier sv = (PgpStreamVerifier)svf.newInstance();
+
+      String sig = PgpHelper.streamToString( asc );
+      
+      sv.initSignature( sig );
+      
+      int nBytes = -1;
+      while( (nBytes = in.read(_buf)) != -1 )
+        sv.bytesReady( _buf, 0, nBytes );
+      
+      boolean verified = sv.verifySignature();
+
+      return verified;
+  }
+  
//---------------------------------------------------------------------------------
   
//---------------------------------------------------------------------------------
 }

Modified: 
maven/mercury/trunk/mercury-crypto/mercury-crypto-basic/src/test/java/org/apache/maven/mercury/crypto/pgp/PgpStreamVerifierTest.java
URL: 
http://svn.apache.org/viewvc/maven/mercury/trunk/mercury-crypto/mercury-crypto-basic/src/test/java/org/apache/maven/mercury/crypto/pgp/PgpStreamVerifierTest.java?rev=760083&r1=760082&r2=760083&view=diff
==============================================================================
--- 
maven/mercury/trunk/mercury-crypto/mercury-crypto-basic/src/test/java/org/apache/maven/mercury/crypto/pgp/PgpStreamVerifierTest.java
 (original)
+++ 
maven/mercury/trunk/mercury-crypto/mercury-crypto-basic/src/test/java/org/apache/maven/mercury/crypto/pgp/PgpStreamVerifierTest.java
 Mon Mar 30 18:12:22 2009
@@ -123,25 +123,32 @@
     System.out.println("BouncyCastle Signature is "+verified);
   }
   
//-------------------------------------------------------------------------------------------------
-  public void testVerifyExternalSignature()
+  public void testVerifyExternalSignature2()
   throws IOException, StreamObserverException
   {
-    PgpStreamVerifier sv = (PgpStreamVerifier)svf.newInstance();
-
     InputStream in = getClass().getResourceAsStream( "/file.gif" );
-    String sig = PgpHelper.streamToString( getClass().getResourceAsStream( 
"/file.gif.asc.external" ) );
+    InputStream sig = getClass().getResourceAsStream( "/file.gif.asc.external" 
);
+    InputStream publicKeyRingStream = getClass().getResourceAsStream( 
publicKeyFile );
     
-    sv.initSignature( sig );
+    boolean verified = PgpHelper.verifyStream( in, sig, publicKeyRingStream );
     
-    int b;
-    while( (b = in.read()) != -1 )
-      sv.byteReady( b );
+    assertTrue( verified );
     
-    boolean verified = sv.verifySignature();
+    System.out.println("3rd Party Signature is "+verified);
+  }
+  
//-------------------------------------------------------------------------------------------------
+  public void testVerifyBCSignature()
+  throws IOException, StreamObserverException
+  {
+    InputStream in = getClass().getResourceAsStream( "/file.gif" );
+    InputStream sig = getClass().getResourceAsStream( "/file.gif.asc" );
+    InputStream publicKeyRingStream = getClass().getResourceAsStream( 
publicKeyFile );
+    
+    boolean verified = PgpHelper.verifyStream( in, sig, publicKeyRingStream );
     
     assertTrue( verified );
     
-    System.out.println("3rd Party Signature is "+verified);
+    System.out.println("BC Signature is "+verified);
   }
   
//-------------------------------------------------------------------------------------------------
 }


Reply via email to