1   /* $RCSfile: DESOperationModeInitializationVectorSpec.java,v $
2    * $Revision: 1.7 $
3    * $Date: 2002/01/17 19:24:55 $
4    * $Author: uwe $
5    * $State: Exp $
6    *
7    * Created on August 13, 2001 12:42 PM
8    *
9    * Copyright (C) 2001 Uwe Guenther <uwe@cscc.de>
10   *
11   * This file is part of the jhbci JCE-ServiceProvider. The jhbci JCE-
12   * ServiceProvider is a library, written in JavaTM, that should be 
13   * used in HBCI banking applications (clients and may be servers),
14   * to do cryptographic operations.
15   *
16   * The jhbci library is free software; you can redistribute it and/or
17   * modify it under the terms of the GNU Lesser General Public
18   * License as published by the Free Software Foundation; either
19   * version 2.1 of the License, or (at your option) any later version.
20   *
21   * The jhbci library is distributed in the hope that it will be useful,
22   * but WITHOUT ANY WARRANTY; without even the implied warranty of
23   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24   * Lesser General Public License for more details.
25   *
26   * You should have received a copy of the GNU Lesser General Public
27   * License along with this library; if not, write to the Free Software
28   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29   *
30   */
31  
32  package de.cscc.crypto.provider.spec;
33  
34  import java.security.spec.AlgorithmParameterSpec;
35  import java.util.Arrays;
36  
37  import javax.crypto.IllegalBlockSizeException;
38  
39  import de.cscc.crypto.util.ByteUtil;
40  
41  /** 
42   * DES Initialization Vector (64 bit) class.
43   *
44   * <p>This class is immutable.
45   *
46   * @author  <a href=mailto:uwe@cscc.de>Uwe G&uuml;nther</a>
47   * @version $Revision: 1.7 $
48   */
49  public class DESOperationModeInitializationVectorSpec 
50          implements AlgorithmParameterSpec, Cloneable {
51  
52      /**
53       * The byte[8] long internal data representation of the initialization
54       * vector.
55       */    
56      private byte[] iv = new byte[8];
57  
58      /** 
59       * Creates a new DESOperationModeInitializationVectorSpec. 
60       *
61       * @param iv the initialization vector to set.
62       * @throws IllegalBlockSizeException if length of iv shorter than 8 bytes. 
63       * If it is longer, we use only the first 8 bytes from iv.
64       */
65      public DESOperationModeInitializationVectorSpec(byte[] iv) 
66              throws IllegalBlockSizeException {
67          this(iv, 0);
68      }
69      
70      /** 
71       * Creates a new DESOperationModeInitializationVectorSpec.
72       *
73       * @param iv the initialization vector to set.
74       * @param offset the offset in iv, where iv starts.
75       * @throws IllegalBlockSizeException if length of iv shorter than 
76       * offset+8 bytes. If it is longer, we use only the first 8 bytes from iv, 
77       * starting at offset.
78       */
79      public DESOperationModeInitializationVectorSpec(byte[] iv, int offset) 
80              throws IllegalBlockSizeException {
81          if (iv.length-offset < 8){
82              throw new IllegalBlockSizeException(
83                  "Usable byte range is " + (iv.length-offset) + 
84                  " bytes large, but it should be 8 bytes or larger.");            
85          }
86          System.arraycopy(iv, offset, this.iv, 0, 8);
87      }
88      
89      /**
90       * Creates a new DESOperationModeInitializationVectorSpec from an existing
91       * one. 
92       *
93       * @param iv the initialization vector to set.
94       */
95      public DESOperationModeInitializationVectorSpec
96             (DESOperationModeInitializationVectorSpec  iv) {    
97          System.arraycopy(iv.iv, 0, this.iv, 0, 8);        
98      }
99  
100     /**
101      * Creates and returns a deep copy of this object.
102      *
103      * @return a clone of this instance.
104      * @see java.lang.Cloneable
105      * @exception CloneNotSupportedException if the object's class does not
106      *             support the <code>Cloneable</code> interface. Subclasses
107      *             that override the <code>clone</code> method can also
108      *             throw this exception to indicate that an instance cannot
109      *             be cloned. */
110     public Object clone() throws CloneNotSupportedException {
111         DESOperationModeInitializationVectorSpec result = 
112                 (DESOperationModeInitializationVectorSpec) super.clone();
113         result.iv = (byte[]) this.iv.clone();
114         return result;
115     }
116     
117     /** 
118      * Indicates whether some other object is "equal to" this one.
119      *
120      * @param   obj   the reference object with which to compare.
121      * @return  <code>true</code> if this object is the same as the obj
122      *         argument; <code>false</code> otherwise.
123      * @see     #hashCode()
124      * @see     java.util.Hashtable
125      */
126     public boolean equals(Object obj) {
127         //Only for performance.
128         if (this == obj) {
129             return true;
130         } 
131         
132         //If obj == null then instanceof returns false, see JLS 15.20.2
133         if (!(obj instanceof DESOperationModeInitializationVectorSpec)) {            
134             return false;
135         }
136         
137         DESOperationModeInitializationVectorSpec other = 
138                 (DESOperationModeInitializationVectorSpec) obj;
139         return Arrays.equals(this.iv, other.iv);
140     }
141     
142     /**
143      * Returns a hash code value for the object. 
144      *
145      * @return  a hash code value for this object.
146      * @see     #equals(java.lang.Object)
147      * @see     java.util.Hashtable
148      */
149     public int hashCode() {
150         int result = 17;
151         for (int i = 0; i < this.iv.length; i++) {
152             result = 37*result + this.iv[i];
153         }
154         return result;
155     }
156     
157     /**
158      * Returns a string representation of the object. 
159      *
160      * @return  a string representation of the object.
161      */
162     public String toString() {
163         return ByteUtil.toHex(this.iv);
164     }
165     
166     /**
167      * Returns the initialization vector.
168      *
169      * @return a initialization vector as byte[8].
170      */
171     public byte[] getIv() {
172         return (byte[]) this.iv.clone();
173     }
174 }
175