FileDocCategorySizeDatePackage
AEVerifier.javaAPI DocAzureus 3.0.3.44491Tue Jun 13 10:27:56 BST 2006org.gudy.azureus2.core3.util

AEVerifier

public class AEVerifier extends Object

Fields Summary
private static final String
pub_exp
private static final String
modulus
Constructors Summary
Methods Summary
public static voidverifyData(java.io.File file)

    
	  
	
				 
	
		  
	
		KeyFactory key_factory = KeyFactory.getInstance("RSA");
		
		RSAPublicKeySpec 	public_key_spec = 
			new RSAPublicKeySpec( new BigInteger(modulus,16), new BigInteger(pub_exp,16));

		RSAPublicKey public_key 	= (RSAPublicKey)key_factory.generatePublic( public_key_spec );

		verifyData( file, public_key );
	
protected static voidverifyData(java.io.File file, java.security.interfaces.RSAPublicKey key)

		ZipInputStream	zis = null;
		
		try{
			zis = new ZipInputStream( 
					new BufferedInputStream( new FileInputStream( file ) ));
				
			byte[]		signature	= null;
			
			Signature	sig = Signature.getInstance("MD5withRSA" );

			sig.initVerify( key );
			
			while( true ){
				
				ZipEntry	entry = zis.getNextEntry();
					
				if ( entry == null ){
					
					break;
				}
			
				if ( entry.isDirectory()){
					
					continue;
				}
				
				String	name = entry.getName();
			
				ByteArrayOutputStream	output = null;
				
				if ( name.equalsIgnoreCase("azureus.sig")){
					
					output	= new ByteArrayOutputStream();
				}
												
				byte[]	buffer = new byte[65536];
				
				while( true ){
				
					int	len = zis.read( buffer );
					
					if ( len <= 0 ){
						
						break;
					}
					
					if ( output == null ){
						
						sig.update( buffer, 0, len );
						
					}else{
						
						output.write( buffer, 0, len );
					}
				}
				
				if ( output != null ){
					
					signature = output.toByteArray();
				}
			}
						
			if ( signature == null ){
								
				throw( new AEVerifierException( AEVerifierException.FT_SIGNATURE_MISSING, "Signature missing from file" ));
			}
			
			if ( !sig.verify( signature )){
				
				throw( new AEVerifierException( AEVerifierException.FT_SIGNATURE_BAD, "Signature doesn't match data" ));
			}
		}finally{
			
			if ( zis != null ){
				
				zis.close();
			}
		}
	
public static voidverifyData(java.lang.String data, byte[] signature)

		KeyFactory key_factory = KeyFactory.getInstance("RSA");
		
		RSAPublicKeySpec 	public_key_spec = 
			new RSAPublicKeySpec( new BigInteger(modulus,16), new BigInteger(pub_exp,16));

		RSAPublicKey public_key 	= (RSAPublicKey)key_factory.generatePublic( public_key_spec );
		
		Signature	sig = Signature.getInstance("MD5withRSA" );

		sig.initVerify( public_key );
		
		sig.update( data.getBytes( "UTF-8" ));
			
		if ( !sig.verify( signature )){
			
			throw( new AEVerifierException( AEVerifierException.FT_SIGNATURE_BAD, "Data verification failed, signature doesn't match data" ));
		}