From 20f4bdbb4c22d4c85bf0479ec65a2b76d2323ec9 Mon Sep 17 00:00:00 2001 From: Patrick McIlroy Date: Mon, 17 Jan 2011 23:48:21 -0800 Subject: [PATCH] java tool for signing machine revisions --- src/verify_signature/java/SignFile.java | 60 +++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 7 deletions(-) diff --git a/src/verify_signature/java/SignFile.java b/src/verify_signature/java/SignFile.java index 795fa97c..39670722 100755 --- a/src/verify_signature/java/SignFile.java +++ b/src/verify_signature/java/SignFile.java @@ -19,24 +19,70 @@ public class SignFile /* Instantiates the key paths and signature algorithm. */ public SignFile() { - try { + try + { // Get the instance of Signature Engine. sign = Signature.getInstance("SHA1withRSA"); } - catch (NoSuchAlgorithmException nsa) { + catch (NoSuchAlgorithmException nsa) + { System.out.println("" + nsa.getMessage()); } } - public boolean loadPrivateKey(privatekey) + public boolean loadPrivateKey(byte[] privatekey) { - // XXX STUB + KeyFactory kf; + try + { + kf = KeyFactory.getInstance("RSA"); + EncodedKeySpec eks = new PKCS8EncodedKeySpec(privatekey); + privateKey = kf.generatePrivate(eks); + } + catch ( NoSuchAlgorithmException e ) + { + System.err.println("Error creating RSA key factory: "+e.getMessage()); + return false; + } + catch ( InvalidKeySpecException e ) + { + System.err.println("Error decoding private key: "+e.getMessage()); + return false; + } return true; } - public boolean signDataFile(data, privatekey, outputfn) + public boolean signDataFile(byte[] data, String outputfn) { - // XXX STUB + FileOutputStream fos = null; + try + { + sign.initSign(privateKey); + fos = new FileOutputStream(outputfn); + sign.update(data); + fos.write( sign.sign() ); + fos.close(); + } + catch ( FileNotFoundException e ) + { + System.err.println("Error opening file: "+e.getMessage()); + return false; + } + catch ( InvalidKeyException e ) + { + System.err.println("Invalid Key Exception: "+e.getMessage()); + return false; + } + catch ( SignatureException e ) + { + System.err.println("Signature exception: "+e.getMessage()); + return false; + } + catch ( IOException e ) + { + System.err.println("IOException: "+e.getMessage()); + return false; + } return true; } @@ -63,7 +109,7 @@ public class SignFile System.exit(1); } - if( !sm.signDataFile(data,privatekey,signaturefn) ) + if( !sm.signDataFile(data,signaturefn) ) { System.err.println("Could not generate signature!"); System.exit(1); -- 2.11.0