001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Alexander Y. Kleymenov
020: * @version $Revision$
021: */package javax.crypto.spec;
022:
023: import java.security.spec.AlgorithmParameterSpec;
024: import java.util.Arrays;
025:
026: import org.apache.harmony.crypto.internal.nls.Messages;
027:
028: /**
029: * @com.intel.drl.spec_ref
030: */
031: public class RC2ParameterSpec implements AlgorithmParameterSpec {
032:
033: private final int effectiveKeyBits;
034: private final byte[] iv;
035:
036: /**
037: * @com.intel.drl.spec_ref
038: */
039: public RC2ParameterSpec(int effectiveKeyBits) {
040: this .effectiveKeyBits = effectiveKeyBits;
041: iv = null;
042: }
043:
044: /**
045: * @com.intel.drl.spec_ref
046: */
047: public RC2ParameterSpec(int effectiveKeyBits, byte[] iv) {
048: if (iv == null) {
049: throw new IllegalArgumentException(Messages
050: .getString("crypto.31")); //$NON-NLS-1$
051: }
052: if (iv.length < 8) {
053: throw new IllegalArgumentException(Messages
054: .getString("crypto.41")); //$NON-NLS-1$
055: }
056: this .effectiveKeyBits = effectiveKeyBits;
057: this .iv = new byte[8];
058: System.arraycopy(iv, 0, this .iv, 0, 8);
059: }
060:
061: /**
062: * @com.intel.drl.spec_ref
063: */
064: public RC2ParameterSpec(int effectiveKeyBits, byte[] iv, int offset) {
065: if (iv == null) {
066: throw new IllegalArgumentException(Messages
067: .getString("crypto.31")); //$NON-NLS-1$
068: }
069: if (iv.length - offset < 8) {
070: throw new IllegalArgumentException(Messages
071: .getString("crypto.41")); //$NON-NLS-1$
072: }
073: this .effectiveKeyBits = effectiveKeyBits;
074: this .iv = new byte[8];
075: System.arraycopy(iv, offset, this .iv, 0, 8);
076: }
077:
078: /**
079: * @com.intel.drl.spec_ref
080: */
081: public int getEffectiveKeyBits() {
082: return effectiveKeyBits;
083: }
084:
085: /**
086: * @com.intel.drl.spec_ref
087: */
088: public byte[] getIV() {
089: if (iv == null) {
090: return null;
091: }
092: byte[] result = new byte[iv.length];
093: System.arraycopy(iv, 0, result, 0, iv.length);
094: return result;
095: }
096:
097: /**
098: * @com.intel.drl.spec_ref
099: */
100: @Override
101: public boolean equals(Object obj) {
102: if (obj == this ) {
103: return true;
104: }
105: if (!(obj instanceof RC2ParameterSpec)) {
106: return false;
107: }
108: RC2ParameterSpec ps = (RC2ParameterSpec) obj;
109: return (effectiveKeyBits == ps.effectiveKeyBits)
110: && (Arrays.equals(iv, ps.iv));
111: }
112:
113: /**
114: * @com.intel.drl.spec_ref
115: */
116: @Override
117: public int hashCode() {
118: int result = effectiveKeyBits;
119: if (iv == null) {
120: return result;
121: }
122: for (byte element : iv) {
123: result += element;
124: }
125: return result;
126: }
127: }
|