001/* RC2ParameterSpec.java -- Wrapper for RC2 parameters.
002   Copyright (C) 2004  Free Software Foundation, Inc.
003
004This file is part of GNU Classpath.
005
006GNU Classpath is free software; you can redistribute it and/or modify
007it under the terms of the GNU General Public License as published by
008the Free Software Foundation; either version 2, or (at your option)
009any later version.
010
011GNU Classpath is distributed in the hope that it will be useful, but
012WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014General Public License for more details.
015
016You should have received a copy of the GNU General Public License
017along with GNU Classpath; see the file COPYING.  If not, write to the
018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
01902110-1301 USA.
020
021Linking this library statically or dynamically with other modules is
022making a combined work based on this library.  Thus, the terms and
023conditions of the GNU General Public License cover the whole
024combination.
025
026As a special exception, the copyright holders of this library give you
027permission to link this library with independent modules to produce an
028executable, regardless of the license terms of these independent
029modules, and to copy and distribute the resulting executable under
030terms of your choice, provided that you also meet, for each linked
031independent module, the terms and conditions of the license of that
032module.  An independent module is a module which is not derived from
033or based on this library.  If you modify this library, you may extend
034this exception to your version of the library, but you are not
035obligated to do so.  If you do not wish to do so, delete this
036exception statement from your version. */
037
038
039package javax.crypto.spec;
040
041import java.security.spec.AlgorithmParameterSpec;
042
043/**
044 * A wrapper for parameters for the <a
045 * href="http://www.rsasecurity.com/rsalabs/faq/3-6-2.html">RC2</a>
046 * block cipher ("RC" means either "Rivest Cipher" or "Ron's Code",
047 * depending upon who you ask and when).
048 *
049 * @author Casey Marshall (csm@gnu.org)
050 * @since 1.4
051 */
052public class RC2ParameterSpec implements AlgorithmParameterSpec
053{
054
055  // Constants and fields.
056  // ------------------------------------------------------------------------
057
058  /** The length of an RC2 IV, in bytes. */
059  private static final int RC2_IV_LENGTH = 8;
060
061  /** The effective key length, in bits. */
062  private int effectiveKeyBits;
063
064  /** The initialization vector. */
065  private byte[] iv;
066
067  // Constructors.
068  // ------------------------------------------------------------------------
069
070  /**
071   * Create RC2 parameters without an IV.
072   *
073   * @param effectiveKeyBits The number of effective key bits.
074   */
075  public RC2ParameterSpec(int effectiveKeyBits)
076  {
077    this.effectiveKeyBits = effectiveKeyBits;
078  }
079
080  /**
081   * Create RC2 parameters with an IV.
082   *
083   * @param effectiveKeyBits The number of effective key bits.
084   * @param iv               The IV; the first eight bytes of this array
085   *                         are used.
086   */
087  public RC2ParameterSpec(int effectiveKeyBits, byte[] iv)
088  {
089    this(effectiveKeyBits, iv, 0);
090  }
091
092  /**
093   * Create RC2 parameters with an IV.
094   *
095   * @param effectiveKeyBits The number of effective key bits.
096   * @param iv               The IV; the first eight bytes of this array
097   *                         after <code>offset</code> are used.
098   * @param offset           From whence to start in the array.
099   */
100  public RC2ParameterSpec(int effectiveKeyBits, byte[] iv, int offset)
101  {
102    if (iv.length - offset < RC2_IV_LENGTH)
103      {
104        throw new IllegalArgumentException("IV too short");
105      }
106    this.effectiveKeyBits = effectiveKeyBits;
107    this.iv = new byte[RC2_IV_LENGTH];
108    System.arraycopy(iv, offset, this.iv, 0, RC2_IV_LENGTH);
109  }
110
111  // Instance methods.
112  // ------------------------------------------------------------------------
113
114  /**
115   * Get the number of effective key bits.
116   *
117   * @return The numer of effective key bits.
118   */
119  public int getEffectiveKeyBits()
120  {
121    return effectiveKeyBits;
122  }
123
124  /**
125   * Return the initialization vector, or <code>null</code> if none was
126   * specified.
127   *
128   * @return The IV, or null.
129   */
130  public byte[] getIV()
131  {
132    return iv;
133  }
134
135  public boolean equals(Object o)
136  {
137    if (this == o) return true;
138    byte[] oiv = ((RC2ParameterSpec) o).getIV();
139    if (iv != oiv)
140      {
141        if (iv == null || oiv == null) return false;
142        if (iv.length != oiv.length) return false;
143        for (int i = 0; i < iv.length; i++)
144          {
145            if (iv[i] != oiv[i])
146              {
147                return false;
148              }
149          }
150      }
151    return effectiveKeyBits == ((RC2ParameterSpec) o).getEffectiveKeyBits();
152  }
153
154  public int hashCode()
155  {
156    int code = effectiveKeyBits;
157    if (iv != null)
158      {
159        for (int i = 0; i < RC2_IV_LENGTH; i++)
160          {
161            code += iv[i];
162          }
163      }
164    return code;
165  }
166}