< prev index next >

src/java.base/share/classes/javax/crypto/spec/RC2ParameterSpec.java

Print this page




  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.crypto.spec;
  27 
  28 import java.security.spec.AlgorithmParameterSpec;
  29 
  30 /**
  31  * This class specifies the parameters used with the
  32  * <a href="http://www.ietf.org/rfc/rfc2268.txt"><i>RC2</i></a>
  33  * algorithm.
  34  *
  35  * <p> The parameters consist of an effective key size and optionally
  36  * an 8-byte initialization vector (IV) (only in feedback mode).
  37  *
  38  * <p> This class can be used to initialize a <code>Cipher</code> object that
  39  * implements the <i>RC2</i> algorithm.
  40  *
  41  * @author Jan Luehe
  42  *
  43  * @since 1.4
  44  */
  45 public class RC2ParameterSpec implements AlgorithmParameterSpec {
  46 
  47     private byte[] iv = null;
  48     private int effectiveKeyBits;
  49 
  50     /**
  51      * Constructs a parameter set for RC2 from the given effective key size
  52      * (in bits).
  53      *
  54      * @param effectiveKeyBits the effective key size in bits.
  55      */
  56     public RC2ParameterSpec(int effectiveKeyBits) {
  57         this.effectiveKeyBits = effectiveKeyBits;
  58     }
  59 
  60     /**
  61      * Constructs a parameter set for RC2 from the given effective key size
  62      * (in bits) and an 8-byte IV.
  63      *
  64      * <p> The bytes that constitute the IV are those between
  65      * <code>iv[0]</code> and <code>iv[7]</code> inclusive.
  66      *
  67      * @param effectiveKeyBits the effective key size in bits.
  68      * @param iv the buffer with the 8-byte IV. The first 8 bytes of
  69      * the buffer are copied to protect against subsequent modification.
  70      * @exception IllegalArgumentException if <code>iv</code> is null.
  71      */
  72     public RC2ParameterSpec(int effectiveKeyBits, byte[] iv) {
  73         this(effectiveKeyBits, iv, 0);
  74     }
  75 
  76     /**
  77      * Constructs a parameter set for RC2 from the given effective key size
  78      * (in bits) and IV.
  79      *
  80      * <p> The IV is taken from <code>iv</code>, starting at
  81      * <code>offset</code> inclusive.
  82      * The bytes that constitute the IV are those between
  83      * <code>iv[offset]</code> and <code>iv[offset+7]</code> inclusive.
  84      *
  85      * @param effectiveKeyBits the effective key size in bits.
  86      * @param iv the buffer with the IV. The first 8 bytes
  87      * of the buffer beginning at <code>offset</code> inclusive
  88      * are copied to protect against subsequent modification.
  89      * @param offset the offset in <code>iv</code> where the 8-byte IV
  90      * starts.
  91      * @exception IllegalArgumentException if <code>iv</code> is null.
  92      */
  93     public RC2ParameterSpec(int effectiveKeyBits, byte[] iv, int offset) {
  94         this.effectiveKeyBits = effectiveKeyBits;
  95         if (iv == null) throw new IllegalArgumentException("IV missing");
  96         int blockSize = 8;
  97         if (iv.length - offset < blockSize) {
  98             throw new IllegalArgumentException("IV too short");
  99         }
 100         this.iv = new byte[blockSize];
 101         System.arraycopy(iv, offset, this.iv, 0, blockSize);
 102     }
 103 
 104     /**
 105      * Returns the effective key size in bits.
 106      *
 107      * @return the effective key size in bits.
 108      */
 109     public int getEffectiveKeyBits() {
 110         return this.effectiveKeyBits;
 111     }
 112 
 113     /**
 114      * Returns the IV or null if this parameter set does not contain an IV.
 115      *
 116      * @return the IV or null if this parameter set does not contain an IV.
 117      * Returns a new array each time this method is called.
 118      */
 119     public byte[] getIV() {
 120         return (iv == null? null:iv.clone());
 121     }
 122 
 123    /**
 124      * Tests for equality between the specified object and this
 125      * object. Two RC2ParameterSpec objects are considered equal if their
 126      * effective key sizes and IVs are equal.
 127      * (Two IV references are considered equal if both are <tt>null</tt>.)
 128      *
 129      * @param obj the object to test for equality with this object.
 130      *
 131      * @return true if the objects are considered equal, false if
 132      * <code>obj</code> is null or otherwise.
 133      */
 134     public boolean equals(Object obj) {
 135         if (obj == this) {
 136             return true;
 137         }
 138         if (!(obj instanceof RC2ParameterSpec)) {
 139             return false;
 140         }
 141         RC2ParameterSpec other = (RC2ParameterSpec) obj;
 142 
 143         return ((effectiveKeyBits == other.effectiveKeyBits) &&
 144                 java.util.Arrays.equals(iv, other.iv));
 145     }
 146 
 147     /**
 148      * Calculates a hash code value for the object.
 149      * Objects that are equal will also have the same hashcode.
 150      */
 151     public int hashCode() {
 152         int retval = 0;


  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.crypto.spec;
  27 
  28 import java.security.spec.AlgorithmParameterSpec;
  29 
  30 /**
  31  * This class specifies the parameters used with the
  32  * <a href="http://www.ietf.org/rfc/rfc2268.txt"><i>RC2</i></a>
  33  * algorithm.
  34  *
  35  * <p> The parameters consist of an effective key size and optionally
  36  * an 8-byte initialization vector (IV) (only in feedback mode).
  37  *
  38  * <p> This class can be used to initialize a {@code Cipher} object that
  39  * implements the <i>RC2</i> algorithm.
  40  *
  41  * @author Jan Luehe
  42  *
  43  * @since 1.4
  44  */
  45 public class RC2ParameterSpec implements AlgorithmParameterSpec {
  46 
  47     private byte[] iv = null;
  48     private int effectiveKeyBits;
  49 
  50     /**
  51      * Constructs a parameter set for RC2 from the given effective key size
  52      * (in bits).
  53      *
  54      * @param effectiveKeyBits the effective key size in bits.
  55      */
  56     public RC2ParameterSpec(int effectiveKeyBits) {
  57         this.effectiveKeyBits = effectiveKeyBits;
  58     }
  59 
  60     /**
  61      * Constructs a parameter set for RC2 from the given effective key size
  62      * (in bits) and an 8-byte IV.
  63      *
  64      * <p> The bytes that constitute the IV are those between
  65      * {@code iv[0]} and {@code iv[7]} inclusive.
  66      *
  67      * @param effectiveKeyBits the effective key size in bits.
  68      * @param iv the buffer with the 8-byte IV. The first 8 bytes of
  69      * the buffer are copied to protect against subsequent modification.
  70      * @exception IllegalArgumentException if {@code iv} is null.
  71      */
  72     public RC2ParameterSpec(int effectiveKeyBits, byte[] iv) {
  73         this(effectiveKeyBits, iv, 0);
  74     }
  75 
  76     /**
  77      * Constructs a parameter set for RC2 from the given effective key size
  78      * (in bits) and IV.
  79      *
  80      * <p> The IV is taken from {@code iv}, starting at
  81      * {@code offset} inclusive.
  82      * The bytes that constitute the IV are those between
  83      * {@code iv[offset]} and {@code iv[offset+7]} inclusive.
  84      *
  85      * @param effectiveKeyBits the effective key size in bits.
  86      * @param iv the buffer with the IV. The first 8 bytes
  87      * of the buffer beginning at {@code offset} inclusive
  88      * are copied to protect against subsequent modification.
  89      * @param offset the offset in {@code iv} where the 8-byte IV
  90      * starts.
  91      * @exception IllegalArgumentException if {@code iv} is null.
  92      */
  93     public RC2ParameterSpec(int effectiveKeyBits, byte[] iv, int offset) {
  94         this.effectiveKeyBits = effectiveKeyBits;
  95         if (iv == null) throw new IllegalArgumentException("IV missing");
  96         int blockSize = 8;
  97         if (iv.length - offset < blockSize) {
  98             throw new IllegalArgumentException("IV too short");
  99         }
 100         this.iv = new byte[blockSize];
 101         System.arraycopy(iv, offset, this.iv, 0, blockSize);
 102     }
 103 
 104     /**
 105      * Returns the effective key size in bits.
 106      *
 107      * @return the effective key size in bits.
 108      */
 109     public int getEffectiveKeyBits() {
 110         return this.effectiveKeyBits;
 111     }
 112 
 113     /**
 114      * Returns the IV or null if this parameter set does not contain an IV.
 115      *
 116      * @return the IV or null if this parameter set does not contain an IV.
 117      * Returns a new array each time this method is called.
 118      */
 119     public byte[] getIV() {
 120         return (iv == null? null:iv.clone());
 121     }
 122 
 123    /**
 124      * Tests for equality between the specified object and this
 125      * object. Two RC2ParameterSpec objects are considered equal if their
 126      * effective key sizes and IVs are equal.
 127      * (Two IV references are considered equal if both are {@code null}.)
 128      *
 129      * @param obj the object to test for equality with this object.
 130      *
 131      * @return true if the objects are considered equal, false if
 132      * {@code obj} is null or otherwise.
 133      */
 134     public boolean equals(Object obj) {
 135         if (obj == this) {
 136             return true;
 137         }
 138         if (!(obj instanceof RC2ParameterSpec)) {
 139             return false;
 140         }
 141         RC2ParameterSpec other = (RC2ParameterSpec) obj;
 142 
 143         return ((effectiveKeyBits == other.effectiveKeyBits) &&
 144                 java.util.Arrays.equals(iv, other.iv));
 145     }
 146 
 147     /**
 148      * Calculates a hash code value for the object.
 149      * Objects that are equal will also have the same hashcode.
 150      */
 151     public int hashCode() {
 152         int retval = 0;
< prev index next >