Home | History | Annotate | Download | only in spec
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 /**
     18 * @author Alexander Y. Kleymenov
     19 * @version $Revision$
     20 */
     21 
     22 package javax.crypto.spec;
     23 
     24 import java.security.spec.AlgorithmParameterSpec;
     25 import java.util.Arrays;
     26 
     27 /**
     28  * The algorithm parameter specification for an <i>initialization vector</i>.
     29  */
     30 public class IvParameterSpec implements AlgorithmParameterSpec {
     31 
     32     private final byte[] iv;
     33 
     34     /**
     35      * Creates a new <code>IvParameterSpec</code> instance with the bytes from
     36      * the specified buffer <i>iv</i> used as <i>initialization vector</i>.
     37      *
     38      * @param iv
     39      *            the buffer used as initialization vector.
     40      * @throws NullPointerException
     41      *             if the specified buffer is null.
     42      */
     43     public IvParameterSpec(byte[] iv) {
     44         if (iv == null) {
     45             throw new NullPointerException("iv == null");
     46         }
     47         this.iv = new byte[iv.length];
     48         System.arraycopy(iv, 0, this.iv, 0, iv.length);
     49     }
     50 
     51     /**
     52      * Creates a new <code>IvParameterSpec</code> instance with <code>byteCount</code>
     53      * bytes from the specified buffer <code>iv</code> starting at
     54      * <code>offset</code>.
     55      *
     56      * @throws IllegalArgumentException
     57      *             if the specified buffer is null or <code>offset</code> and
     58      *             <code>byteCount</code> do not specify a valid chunk in the
     59      *             specified buffer.
     60      * @throws ArrayIndexOutOfBoundsException
     61      *             if <code>offset</code> or <code>byteCount</code> are negative.
     62      */
     63     public IvParameterSpec(byte[] iv, int offset, int byteCount) {
     64         if ((iv == null) || (iv.length - offset < byteCount)) {
     65             throw new IllegalArgumentException();
     66         }
     67         Arrays.checkOffsetAndCount(iv.length, offset, byteCount);
     68         this.iv = new byte[byteCount];
     69         System.arraycopy(iv, offset, this.iv, 0, byteCount);
     70     }
     71 
     72     /**
     73      * Returns a copy of the <i>initialization vector</i> data.
     74      *
     75      * @return a copy of the initialization vector data.
     76      */
     77     public byte[] getIV() {
     78         byte[] res = new byte[iv.length];
     79         System.arraycopy(iv, 0, res, 0, iv.length);
     80         return res;
     81     }
     82 }
     83