Source for javax.crypto.spec.RC2ParameterSpec

   1: /* RC2ParameterSpec.java -- Wrapper for RC2 parameters.
   2:    Copyright (C) 2004  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package javax.crypto.spec;
  40: 
  41: import java.security.spec.AlgorithmParameterSpec;
  42: 
  43: /**
  44:  * A wrapper for parameters for the <a
  45:  * href="http://www.rsasecurity.com/rsalabs/faq/3-6-2.html">RC2</a>
  46:  * block cipher ("RC" means either "Rivest Cipher" or "Ron's Code",
  47:  * depending upon who you ask and when).
  48:  *
  49:  * @author Casey Marshall (csm@gnu.org)
  50:  * @since 1.4
  51:  */
  52: public class RC2ParameterSpec implements AlgorithmParameterSpec
  53: {
  54: 
  55:   // Constants and fields.
  56:   // ------------------------------------------------------------------------
  57: 
  58:   /** The length of an RC2 IV, in bytes. */
  59:   private static final int RC2_IV_LENGTH = 8;
  60: 
  61:   /** The effective key length, in bits. */
  62:   private int effectiveKeyBits;
  63: 
  64:   /** The initialization vector. */
  65:   private byte[] iv;
  66: 
  67:   // Constructors.
  68:   // ------------------------------------------------------------------------
  69: 
  70:   /**
  71:    * Create RC2 parameters without an IV.
  72:    *
  73:    * @param effectiveKeyBits The number of effective key bits.
  74:    */
  75:   public RC2ParameterSpec(int effectiveKeyBits)
  76:   {
  77:     this.effectiveKeyBits = effectiveKeyBits;
  78:   }
  79: 
  80:   /**
  81:    * Create RC2 parameters with an IV.
  82:    *
  83:    * @param effectiveKeyBits The number of effective key bits.
  84:    * @param iv               The IV; the first eight bytes of this array
  85:    *                         are used.
  86:    */
  87:   public RC2ParameterSpec(int effectiveKeyBits, byte[] iv)
  88:   {
  89:     this(effectiveKeyBits, iv, 0);
  90:   }
  91: 
  92:   /**
  93:    * Create RC2 parameters with an IV.
  94:    *
  95:    * @param effectiveKeyBits The number of effective key bits.
  96:    * @param iv               The IV; the first eight bytes of this array
  97:    *                         after <code>offset</code> are used.
  98:    * @param offset           From whence to start in the array.
  99:    */
 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: }