Path: blob/master/src/java.base/share/classes/javax/crypto/spec/IvParameterSpec.java
41159 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.crypto.spec;2627import java.security.spec.AlgorithmParameterSpec;2829/**30* This class specifies an <i>initialization vector</i> (IV).31* Examples which use IVs are ciphers in feedback mode,32* e.g., DES in CBC mode and RSA ciphers with OAEP encoding33* operation.34*35* @author Jan Luehe36*37* @since 1.438*/39public class IvParameterSpec implements AlgorithmParameterSpec {4041private byte[] iv;4243/**44* Creates an IvParameterSpec object using the bytes in <code>iv</code>45* as the IV.46*47* @param iv the buffer with the IV. The contents of the48* buffer are copied to protect against subsequent modification.49* @throws NullPointerException if <code>iv</code> is <code>null</code>50*/51public IvParameterSpec(byte[] iv) {52this(iv, 0, iv.length);53}5455/**56* Creates an IvParameterSpec object using the first <code>len</code>57* bytes in <code>iv</code>, beginning at <code>offset</code>58* inclusive, as the IV.59*60* <p> The bytes that constitute the IV are those between61* <code>iv[offset]</code> and <code>iv[offset+len-1]</code> inclusive.62*63* @param iv the buffer with the IV. The first <code>len</code>64* bytes of the buffer beginning at <code>offset</code> inclusive65* are copied to protect against subsequent modification.66* @param offset the offset in <code>iv</code> where the IV67* starts.68* @param len the number of IV bytes.69* @throws IllegalArgumentException if <code>iv</code> is <code>null</code>70* or {@code (iv.length - offset < len)}71* @throws ArrayIndexOutOfBoundsException is thrown if <code>offset</code>72* or <code>len</code> index bytes outside the <code>iv</code>.73*/74public IvParameterSpec(byte[] iv, int offset, int len) {75if (iv == null) {76throw new IllegalArgumentException("IV missing");77}78if (iv.length - offset < len) {79throw new IllegalArgumentException80("IV buffer too short for given offset/length combination");81}82if (len < 0) {83throw new ArrayIndexOutOfBoundsException("len is negative");84}85this.iv = new byte[len];86System.arraycopy(iv, offset, this.iv, 0, len);87}8889/**90* Returns the initialization vector (IV).91*92* @return the initialization vector (IV). Returns a new array93* each time this method is called.94*/95public byte[] getIV() {96return this.iv.clone();97}98}99100101