home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / security1.1 / api / example / testSig.java < prev   
Encoding:
Java Source  |  1997-07-13  |  2.5 KB  |  88 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.io.*;
  18. import java.security.*;
  19.  
  20. class testSig {
  21.  
  22.     public static void main(String[] args) {
  23.  
  24.         /* Test generating and verifying a DSA signature */
  25.  
  26.         try {
  27.  
  28.             /* generate a key pair */
  29.  
  30.             KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
  31.             keyGen.initialize(1024, new SecureRandom());
  32.             KeyPair pair = keyGen.generateKeyPair();
  33.  
  34.             /* create a Signature object to use for signing and verifying */
  35.  
  36.             Signature dsa = Signature.getInstance("SHA/DSA"); 
  37.  
  38.             /* initialize the Signature object for signing */
  39.  
  40.             PrivateKey priv = pair.getPrivate();
  41.  
  42.             dsa.initSign(priv);
  43.  
  44.             /* Update and sign the data */
  45.  
  46.             FileInputStream fis = new FileInputStream(args[0]);
  47.             byte b;
  48.             while (fis.available() != 0) {
  49.                 b = (byte) fis.read();
  50.                 dsa.update(b);
  51.                 };
  52.  
  53.             fis.close();
  54.  
  55.             /* Now that all the data to be signed has been read in, sign it */
  56.             byte[] sig = dsa.sign();
  57.  
  58.  
  59.             /* Verify the signature */
  60.  
  61.             /* Initialize the Signature object for verification */
  62.             PublicKey pub = pair.getPublic();
  63.             dsa.initVerify(pub);
  64.  
  65.             /* Update and verify the data */
  66.  
  67.             fis = new FileInputStream(args[0]);
  68.             while (fis.available() != 0) {
  69.                 b = (byte) fis.read();
  70.                 dsa.update(b);
  71.                 };
  72.  
  73.             fis.close();
  74.  
  75.             boolean verifies = dsa.verify(sig);
  76.  
  77.             System.out.println("signature verifies: " + verifies);
  78.  
  79.         } catch (Exception e) {
  80.             System.err.println("Caught exception " + e.toString());
  81.         }
  82.  
  83.     }
  84.  
  85. }
  86.  
  87.  
  88.