0N/A/*
2362N/A * Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.crypto.spec;
0N/A
0N/Aimport java.security.spec.AlgorithmParameterSpec;
0N/A
0N/A/**
0N/A * This class specifies the parameters used with the
0N/A * <a href="http://www.ietf.org/rfc/rfc2268.txt"><i>RC2</i></a>
0N/A * algorithm.
0N/A *
0N/A * <p> The parameters consist of an effective key size and optionally
0N/A * an 8-byte initialization vector (IV) (only in feedback mode).
0N/A *
0N/A * <p> This class can be used to initialize a <code>Cipher</code> object that
0N/A * implements the <i>RC2</i> algorithm.
0N/A *
0N/A * @author Jan Luehe
0N/A *
0N/A * @since 1.4
0N/A */
0N/Apublic class RC2ParameterSpec implements AlgorithmParameterSpec {
0N/A
0N/A private byte[] iv = null;
0N/A private int effectiveKeyBits;
0N/A
0N/A /**
0N/A * Constructs a parameter set for RC2 from the given effective key size
0N/A * (in bits).
0N/A *
0N/A * @param effectiveKeyBits the effective key size in bits.
0N/A */
0N/A public RC2ParameterSpec(int effectiveKeyBits) {
0N/A this.effectiveKeyBits = effectiveKeyBits;
0N/A }
0N/A
0N/A /**
0N/A * Constructs a parameter set for RC2 from the given effective key size
0N/A * (in bits) and an 8-byte IV.
0N/A *
0N/A * <p> The bytes that constitute the IV are those between
0N/A * <code>iv[0]</code> and <code>iv[7]</code> inclusive.
0N/A *
0N/A * @param effectiveKeyBits the effective key size in bits.
0N/A * @param iv the buffer with the 8-byte IV. The first 8 bytes of
0N/A * the buffer are copied to protect against subsequent modification.
0N/A * @exception IllegalArgumentException if <code>iv</code> is null.
0N/A */
0N/A public RC2ParameterSpec(int effectiveKeyBits, byte[] iv) {
0N/A this(effectiveKeyBits, iv, 0);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a parameter set for RC2 from the given effective key size
0N/A * (in bits) and IV.
0N/A *
0N/A * <p> The IV is taken from <code>iv</code>, starting at
0N/A * <code>offset</code> inclusive.
0N/A * The bytes that constitute the IV are those between
0N/A * <code>iv[offset]</code> and <code>iv[offset+7]</code> inclusive.
0N/A *
0N/A * @param effectiveKeyBits the effective key size in bits.
0N/A * @param iv the buffer with the IV. The first 8 bytes
0N/A * of the buffer beginning at <code>offset</code> inclusive
0N/A * are copied to protect against subsequent modification.
0N/A * @param offset the offset in <code>iv</code> where the 8-byte IV
0N/A * starts.
0N/A * @exception IllegalArgumentException if <code>iv</code> is null.
0N/A */
0N/A public RC2ParameterSpec(int effectiveKeyBits, byte[] iv, int offset) {
0N/A this.effectiveKeyBits = effectiveKeyBits;
0N/A if (iv == null) throw new IllegalArgumentException("IV missing");
0N/A int blockSize = 8;
0N/A if (iv.length - offset < blockSize) {
0N/A throw new IllegalArgumentException("IV too short");
0N/A }
0N/A this.iv = new byte[blockSize];
0N/A System.arraycopy(iv, offset, this.iv, 0, blockSize);
0N/A }
0N/A
0N/A /**
0N/A * Returns the effective key size in bits.
0N/A *
0N/A * @return the effective key size in bits.
0N/A */
0N/A public int getEffectiveKeyBits() {
0N/A return this.effectiveKeyBits;
0N/A }
0N/A
0N/A /**
0N/A * Returns the IV or null if this parameter set does not contain an IV.
0N/A *
0N/A * @return the IV or null if this parameter set does not contain an IV.
0N/A * Returns a new array each time this method is called.
0N/A */
0N/A public byte[] getIV() {
0N/A return (iv == null? null:(byte[])iv.clone());
0N/A }
0N/A
0N/A /**
0N/A * Tests for equality between the specified object and this
0N/A * object. Two RC2ParameterSpec objects are considered equal if their
0N/A * effective key sizes and IVs are equal.
0N/A * (Two IV references are considered equal if both are <tt>null</tt>.)
0N/A *
0N/A * @param obj the object to test for equality with this object.
0N/A *
0N/A * @return true if the objects are considered equal, false if
0N/A * <code>obj</code> is null or otherwise.
0N/A */
0N/A public boolean equals(Object obj) {
0N/A if (obj == this) {
0N/A return true;
0N/A }
0N/A if (!(obj instanceof RC2ParameterSpec)) {
0N/A return false;
0N/A }
0N/A RC2ParameterSpec other = (RC2ParameterSpec) obj;
0N/A
0N/A return ((effectiveKeyBits == other.effectiveKeyBits) &&
0N/A java.util.Arrays.equals(iv, other.iv));
0N/A }
0N/A
0N/A /**
0N/A * Calculates a hash code value for the object.
0N/A * Objects that are equal will also have the same hashcode.
0N/A */
0N/A public int hashCode() {
0N/A int retval = 0;
0N/A if (iv != null) {
0N/A for (int i = 1; i < iv.length; i++) {
0N/A retval += iv[i] * i;
0N/A }
0N/A }
0N/A return (retval += effectiveKeyBits);
0N/A }
0N/A}