/* * SignHandler.java (för 10.1) * Karl-Adam Karlsson * 06-10-05 * * java SignHandler som signerar filen med (den privata) nyckeln och * sparar signaturen i * */ import javax.crypto.*; import java.security.*; import java.io.*; public class SignHandler{ public SignHandler(String dataName, String keyName, String sigName){ signAndSave(getInS(dataName),loadKey(keyName), sigName); }//end of constructor private void signAndSave(FileInputStream inStream, Key aKey, String sigName){ File aFile = new File(sigName); Signature aSig = null; byte[] signature = null; try{ aSig = Signature.getInstance( "SHA1withDSA" ); aSig.initSign( (PrivateKey)aKey ); }catch(NoSuchAlgorithmException noAl){ System.err.println(noAl); System.exit(1); }catch(InvalidKeyException inKey){ System.err.println(inKey); System.exit(1); } int aLen; byte[] buffLn = new byte[1024]; try{ while ((aLen = inStream.read(buffLn)) != -1) { aSig.update( buffLn ); } signature = aSig.sign(); saveSig(sigName, signature); inStream.close(); }catch(IOException ioe){ System.err.println(ioe); System.exit(1); }catch(SignatureException sigEx){ System.err.println(sigEx); System.exit(1); } } /* * getInS * Returns a FileInputStream connected to a file * @param String fimeName The name of the file to connect the stream to. * @return FileInputStream A stream connected to the file. */ private FileInputStream getInS(String fileName){ FileInputStream inFstream = null; try{ inFstream = new FileInputStream(fileName); }catch(FileNotFoundException fEx){ System.err.print(fEx); System.exit(1); } return inFstream; }//end of getInS /* * SaveSig saves a Signature to file. * Both Signature to be saved and filename are parameters. * * @param String fileName The file to save to. * @param byte[] aSig The Signature, in bytes, to be saved. * */ private void saveSig(String fileName, byte[] aSig){ File outFile = new File(fileName); FileOutputStream outStream; try{ outStream = new FileOutputStream(fileName); outStream.write(aSig); outStream.close(); } catch(IOException e) { System.err.println("IO Exception"); e.printStackTrace(); System.exit(1); } }//endof saveSig /* * loadKey * Loads a Key Object from the file named * @param keyFileName Name of file to load key from * @return Key the loaded key. */ private Key loadKey(String keyFileName){ ObjectInputStream oInStr = null; Key theKey = null; try{ FileInputStream inFstream = new FileInputStream(keyFileName); oInStr = new ObjectInputStream(inFstream); }catch(FileNotFoundException fEx){ System.err.print(fEx); System.exit(1); }catch(IOException ioe){ System.err.print(ioe); System.exit(1); } try{ theKey = (PrivateKey)oInStr.readObject(); oInStr.close(); }catch(IOException io2){ System.err.print(io2); System.exit(1); }catch(ClassNotFoundException cNf){ System.err.print(cNf); System.exit(1); } return theKey; }//end of loadKey /* * main * */ public static void main (String[] args) { if(args==null || args.length < 3 ||args.length>3){ System.out.println("Usage: java SignHandler "); System.out.println("Where is the file to sign."); System.out.println("Where is the name of private keyfile to use for signing."); System.out.println("Where is desired name of the signature file."); } else if(args.length==3&&args[0]!=null&&args[1]!=null&&args[2]!=null){ new SignHandler(args[0],args[1],args[2]); } else if(args[0]==null||args[1]==null||args[2]==null){ System.out.println("Usage: java SignHandler "); System.out.println("Where is the file to sign."); System.out.println("Where is the name of private keyfile to use for signing."); System.out.println("Where is desired name of the signature file."); } }//end of main }//end of class