public static File generate(String file, String certLoc, String privateKey){

    	//adapted from http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/security/apisign/examples/GenSig.java
        /* Generate a DSA signature */

    	File storedSig = null;
    	String  result = file.replaceAll("[^\\p{L}\\p{Z}]","");
    	System.out.println(file);
    	System.out.println(result);
    	try {

        	@SuppressWarnings("resource")
			FileInputStream fis = new FileInputStream(certLoc);
        	ByteArrayInputStream bis = null;

        	byte value[] = new byte[fis.available()];
        	  fis.read(value);
        	  bis = new ByteArrayInputStream(value);
        	  
        	  CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
        	  
        	  X509Certificate cert = (X509Certificate)certFactory.generateCertificate(bis);
    		
    		String name = cert.getSubjectDN().getName();
    		
        	KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA", "SUN");
        	SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
        	keyGen.initialize(1024, random);
        	      	
        	KeyPair loaded = LoadKeyPair("DSA", name);
        	
        	Signature dsa = Signature.getInstance("SHA1withDSA", "SUN");
        	dsa.initSign(loaded.getPrivate());
        	
        	FileInputStream fios = new FileInputStream(file);
        	BufferedInputStream bufin = new BufferedInputStream(fios);
        	byte[] buffer = new byte[1024];
        	int len;
        	while((len = bufin.read(buffer)) >=0){
        		dsa.update(buffer, 0, len);
        	};
        	bufin.close();
        	
        	byte[] realSig = dsa.sign();
        	
        	/* save the signature in a file */
        	FileOutputStream sigfos = new FileOutputStream(result + "_" + name + ".sig");
        	sigfos.write(realSig);
        	sigfos.close();
        	
        	storedSig = new File(result + "_" + name + ".sig");
        	
        	return storedSig;

        } catch (Exception e) {
            System.err.println("Caught exception " + e.toString());
        }
		return storedSig;
    }