import java.io.*; import java.security.*; public class SignHandler { public static void main(String[] args) { // Läs in privata nyckeln PrivateKey privateKey = (PrivateKey) KeyHandler.loadKey(args[1]); System.out.println("DONE LOADING PRIVATE KEY"); // Gör en signatur och verifikations maskin Signature signAndVerifyEngine = null; try { signAndVerifyEngine = Signature.getInstance("SHA1withDSA", "SUN"); System.out.println("DONE MAKING SIGNATURE/VERIFYING ENGINE"); } catch(NoSuchAlgorithmException nsae) { System.out.println("NoSuchAlgorithmException generated"); } catch(NoSuchProviderException nspe) { System.out.println("NoSuchProviderException generated"); } // Initialisera maskinen try { signAndVerifyEngine.initSign(privateKey); } catch(InvalidKeyException ike) { System.out.println("InvalidKeyException generated"); } // Tillför maskinen text-meddelandet try { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(args[0])); byte[] buffer = new byte[1024]; int length; while(bis.available() != 0) { length = bis.read(buffer); signAndVerifyEngine.update(buffer, 0, length); } bis.close(); System.out.println("DONE DELIVERING DATA TO SIGNATURE/VERIFYING ENGINE"); } catch(SignatureException ike) { System.out.println("SignatureException generated"); } catch(IOException ioe) { System.out.println("IOException generated"); } // Signera byte[] signature = null; try { signature = signAndVerifyEngine.sign(); System.out.println("DONE MESSAGE SIGNED"); } catch(SignatureException se) { System.out.println("SignatureException"); } // Spara signaturen try { FileOutputStream fos = new FileOutputStream(args[2]); fos.write(signature); fos.close(); System.out.println("DONE SAVING SIGNATURE"); } catch (IOException ioe) { System.out.println("IOException generated"); } } }