All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.security.DSAUtil Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.security;

import java.io.*;
import java.security.*;
import java.security.spec.X509EncodedKeySpec;
import java.security.spec.PKCS8EncodedKeySpec;


//------------------------------------------------------------------------------
/**
 * DSA-related functions.
 *
 * @author J. Alex Taylor, hairyfatguy.com
 */
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------

public class DSAUtil
{
   //--------------------------------------------------------------------------
   public static void generateDSAKeyPair(File inOutputDir)
   {
      try
      {
         KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA", "SUN");

         SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
         keyGen.initialize(1024, random);

         KeyPair pair = keyGen.generateKeyPair();
         PrivateKey priv = pair.getPrivate();
         PublicKey pub = pair.getPublic();

         // Save the public key in a file
         byte[] key = pub.getEncoded();
         FileOutputStream keyfos = new FileOutputStream(new File(inOutputDir, "DSAKey.pub"));
         keyfos.write(key);
         keyfos.close();

         // Save the private key in a file
         key = priv.getEncoded();
         keyfos = new FileOutputStream(new File(inOutputDir, "DSAKey.priv"));
         keyfos.write(key);
         keyfos.close();
      }
      catch (Exception e)
      {
         throw new RuntimeException(e);
      }
   }

   //--------------------------------------------------------------------------
   public static byte[] signData(PrivateKey inPrivateKey, byte[] inData)
   {
      return signData(inPrivateKey, new ByteArrayInputStream(inData));
   }

   //--------------------------------------------------------------------------
   public static byte[] signData(PrivateKey inPrivateKey, InputStream inData)
   {
      byte[] sig;
      try
      {
         try
         {
            Signature dsa = Signature.getInstance("SHA1withDSA", "SUN");
            dsa.initSign(inPrivateKey);

            byte[] buffer = new byte[1024];
            int len;
            while (inData.available() != 0)
            {
               len = inData.read(buffer);
               dsa.update(buffer, 0, len);
            }

            sig = dsa.sign();
         }
         finally
         {
            if (inData != null) inData.close();
         }
      }
      catch (Exception e)
      {
         throw new RuntimeException(e);
      }

      return sig;
   }


   //--------------------------------------------------------------------------
   public static boolean verifySignature(PublicKey inPublicKey, byte[] inData, byte[] inSignature)
   {
      return verifySignature(inPublicKey, new ByteArrayInputStream(inData), inSignature);
   }

   //--------------------------------------------------------------------------
   public static boolean verifySignature(PublicKey inPublicKey, InputStream inData, byte[] inSignature)
   {
      boolean result = false;
      try
      {
         try
         {
            Signature sig = Signature.getInstance("SHA1withDSA", "SUN");
            sig.initVerify(inPublicKey);

            byte[] buffer = new byte[1024];
            int len;
            while (inData.available() != 0)
            {
               len = inData.read(buffer);
               sig.update(buffer, 0, len);
            }

            result = sig.verify(inSignature);
         }
         finally
         {
            if (inData != null) inData.close();
         }
      }
      catch (Exception e)
      {
         throw new RuntimeException(e);
      }

      return result;
   }

   //--------------------------------------------------------------------------
   public static PublicKey readPublicKey(File inX509EncodedPublicKeyFile)
   {
      byte[] encKey;

      try
      {
         FileInputStream keyfis = new FileInputStream(inX509EncodedPublicKeyFile);
         encKey = new byte[keyfis.available()];
         keyfis.read(encKey);
         keyfis.close();
      }
      catch (Exception e)
      {
         throw new RuntimeException(e);
      }

      return readPublicKey(encKey);
   }

   //--------------------------------------------------------------------------
   public static PublicKey readPublicKey(InputStream inX509EncodedPublicKeyStream)
   {
      byte[] encKey;

      try
      {
         try
         {
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len;
            while ((len = inX509EncodedPublicKeyStream.read(buffer)) != -1)
            {
               outStream.write(buffer, 0, len);
            }

            outStream.close();
            encKey = outStream.toByteArray();
         }
         finally
         {
            inX509EncodedPublicKeyStream.close();
         }
      }
      catch (Exception e)
      {
         throw new RuntimeException(e);
      }

      return readPublicKey(encKey);
   }

   //--------------------------------------------------------------------------
   public static PublicKey readPublicKey(byte[] inX509EncodedPublicKey)
   {
      PublicKey pubKey;

      try
      {
         X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(inX509EncodedPublicKey);
         KeyFactory keyFactory = KeyFactory.getInstance("DSA", "SUN");
         pubKey = keyFactory.generatePublic(pubKeySpec);
      }
      catch (Exception e)
      {
         throw new RuntimeException(e);
      }

      return pubKey;
   }


   //--------------------------------------------------------------------------
   public static PrivateKey readPrivateKey(File inPKCS8EncodedPrivateKeyFile)
   {
      byte[] encKey;

      try
      {
         FileInputStream keyfis = new FileInputStream(inPKCS8EncodedPrivateKeyFile);
         encKey = new byte[keyfis.available()];
         keyfis.read(encKey);
         keyfis.close();
      }
      catch (Exception e)
      {
         throw new RuntimeException(e);
      }

      return readPrivateKey(encKey);
   }


   //--------------------------------------------------------------------------
   public static PrivateKey readPrivateKey(InputStream inPKCS8EncodedPrivateKeyStream)
   {
      byte[] encKey = null;

      if (inPKCS8EncodedPrivateKeyStream != null)
      {
         try
         {
            try
            {
               ByteArrayOutputStream outStream = new ByteArrayOutputStream();
               byte[] buffer = new byte[1024];
               int len;
               while ((len = inPKCS8EncodedPrivateKeyStream.read(buffer)) != -1)
               {
                  outStream.write(buffer, 0, len);
               }

               outStream.close();
               encKey = outStream.toByteArray();
            }
            finally
            {
               inPKCS8EncodedPrivateKeyStream.close();
            }
         }
         catch (Exception e)
         {
            throw new RuntimeException(e);
         }
      }

      return readPrivateKey(encKey);
   }

   //--------------------------------------------------------------------------
   public static PrivateKey readPrivateKey(byte[] inPKCS8EncodedPrivateKey)
   {
      PrivateKey privKey = null;

      if (inPKCS8EncodedPrivateKey != null)
      {
         try
         {
            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(inPKCS8EncodedPrivateKey);
            KeyFactory keyFactory = KeyFactory.getInstance("DSA", "SUN");
            privKey = keyFactory.generatePrivate(keySpec);
         }
         catch (Exception e)
         {
            throw new RuntimeException(e);
         }
      }

      return privKey;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy