Save This Page
Home » openjdk-7 » com.sun.crypto » provider » [javadoc | source]
    1   /*
    2    * Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved.
    3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    4    *
    5    * This code is free software; you can redistribute it and/or modify it
    6    * under the terms of the GNU General Public License version 2 only, as
    7    * published by the Free Software Foundation.  Oracle designates this
    8    * particular file as subject to the "Classpath" exception as provided
    9    * by Oracle in the LICENSE file that accompanied this code.
   10    *
   11    * This code is distributed in the hope that it will be useful, but WITHOUT
   12    * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   13    * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   14    * version 2 for more details (a copy is included in the LICENSE file that
   15    * accompanied this code).
   16    *
   17    * You should have received a copy of the GNU General Public License version
   18    * 2 along with this work; if not, write to the Free Software Foundation,
   19    * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   20    *
   21    * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
   22    * or visit www.oracle.com if you need additional information or have any
   23    * questions.
   24    */
   25   
   26   package com.sun.crypto.provider;
   27   
   28   import java.util;
   29   import java.io;
   30   import java.math.BigInteger;
   31   import java.security.AlgorithmParametersSpi;
   32   import java.security.spec.AlgorithmParameterSpec;
   33   import java.security.spec.InvalidParameterSpecException;
   34   import javax.crypto.spec.PBEParameterSpec;
   35   import sun.misc.HexDumpEncoder;
   36   import sun.security.util;
   37   
   38   
   39   /**
   40    * This class implements the parameter set used with password-based
   41    * encryption, which is defined in PKCS#5 as follows:
   42    *
   43    * <pre>
   44    * PBEParameter ::=  SEQUENCE {
   45    *     salt   OCTET STRING SIZE(8),
   46    *     iterationCount   INTEGER }
   47    * </pre>
   48    *
   49    * @author Jan Luehe
   50    *
   51    */
   52   
   53   public final class PBEParameters extends AlgorithmParametersSpi {
   54   
   55       // the salt
   56       private byte[] salt = null;
   57   
   58       // the iteration count
   59       private int iCount = 0;
   60   
   61       protected void engineInit(AlgorithmParameterSpec paramSpec)
   62           throws InvalidParameterSpecException
   63      {
   64          if (!(paramSpec instanceof PBEParameterSpec)) {
   65              throw new InvalidParameterSpecException
   66                  ("Inappropriate parameter specification");
   67          }
   68          this.salt = (byte[])((PBEParameterSpec)paramSpec).getSalt().clone();
   69          this.iCount = ((PBEParameterSpec)paramSpec).getIterationCount();
   70       }
   71   
   72       protected void engineInit(byte[] encoded)
   73           throws IOException
   74       {
   75           try {
   76               DerValue val = new DerValue(encoded);
   77               if (val.tag != DerValue.tag_Sequence) {
   78                   throw new IOException("PBE parameter parsing error: "
   79                                         + "not a sequence");
   80               }
   81               val.data.reset();
   82   
   83               this.salt = val.data.getOctetString();
   84               this.iCount = val.data.getInteger();
   85   
   86               if (val.data.available() != 0) {
   87                   throw new IOException
   88                       ("PBE parameter parsing error: extra data");
   89               }
   90           } catch (NumberFormatException e) {
   91               throw new IOException("iteration count too big");
   92           }
   93       }
   94   
   95       protected void engineInit(byte[] encoded, String decodingMethod)
   96           throws IOException
   97       {
   98           engineInit(encoded);
   99       }
  100   
  101       protected AlgorithmParameterSpec engineGetParameterSpec(Class paramSpec)
  102           throws InvalidParameterSpecException
  103       {
  104           if (PBEParameterSpec.class.isAssignableFrom(paramSpec)) {
  105               return new PBEParameterSpec(this.salt, this.iCount);
  106           } else {
  107               throw new InvalidParameterSpecException
  108                   ("Inappropriate parameter specification");
  109           }
  110       }
  111   
  112       protected byte[] engineGetEncoded() throws IOException {
  113           DerOutputStream out = new DerOutputStream();
  114           DerOutputStream bytes = new DerOutputStream();
  115   
  116           bytes.putOctetString(this.salt);
  117           bytes.putInteger(this.iCount);
  118   
  119           out.write(DerValue.tag_Sequence, bytes);
  120           return out.toByteArray();
  121       }
  122   
  123       protected byte[] engineGetEncoded(String encodingMethod)
  124           throws IOException
  125       {
  126           return engineGetEncoded();
  127       }
  128   
  129       /*
  130        * Returns a formatted string describing the parameters.
  131        */
  132       protected String engineToString() {
  133           String LINE_SEP = System.getProperty("line.separator");
  134           String saltString = LINE_SEP + "    salt:" + LINE_SEP + "[";
  135           HexDumpEncoder encoder = new HexDumpEncoder();
  136           saltString += encoder.encodeBuffer(salt);
  137           saltString += "]";
  138   
  139           return saltString + LINE_SEP + "    iterationCount:"
  140               + LINE_SEP + Debug.toHexString(BigInteger.valueOf(iCount))
  141               + LINE_SEP;
  142       }
  143   }

Save This Page
Home » openjdk-7 » com.sun.crypto » provider » [javadoc | source]