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 RC5ParameterSpec implements AlgorithmParameterSpec {
032:
033: private final int version;
034: private final int rounds;
035: private final int wordSize;
036: private final byte[] iv;
037:
038: /**
039: * @com.intel.drl.spec_ref
040: */
041: public RC5ParameterSpec(int version, int rounds, int wordSize) {
042: this .version = version;
043: this .rounds = rounds;
044: this .wordSize = wordSize;
045: this .iv = null;
046: }
047:
048: /**
049: * @com.intel.drl.spec_ref
050: */
051: public RC5ParameterSpec(int version, int rounds, int wordSize,
052: byte[] iv) {
053: if (iv == null) {
054: throw new IllegalArgumentException(Messages
055: .getString("crypto.31")); //$NON-NLS-1$
056: }
057: if (iv.length < 2 * (wordSize / 8)) {
058: throw new IllegalArgumentException(Messages
059: .getString("crypto.32")); //$NON-NLS-1$
060: }
061: this .version = version;
062: this .rounds = rounds;
063: this .wordSize = wordSize;
064: this .iv = new byte[2 * (wordSize / 8)];
065: System.arraycopy(iv, 0, this .iv, 0, 2 * (wordSize / 8));
066: }
067:
068: /**
069: * @com.intel.drl.spec_ref
070: */
071: public RC5ParameterSpec(int version, int rounds, int wordSize,
072: byte[] iv, int offset) {
073: if (iv == null) {
074: throw new IllegalArgumentException(Messages
075: .getString("crypto.31")); //$NON-NLS-1$
076: }
077: if (offset < 0) {
078: throw new ArrayIndexOutOfBoundsException(Messages
079: .getString("crypto.33")); //$NON-NLS-1$
080: }
081: if (iv.length - offset < 2 * (wordSize / 8)) {
082: throw new IllegalArgumentException(Messages
083: .getString("crypto.34")); //$NON-NLS-1$
084: }
085: this .version = version;
086: this .rounds = rounds;
087: this .wordSize = wordSize;
088: this .iv = new byte[offset + 2 * (wordSize / 8)];
089: System.arraycopy(iv, offset, this .iv, 0, 2 * (wordSize / 8));
090: }
091:
092: /**
093: * @com.intel.drl.spec_ref
094: */
095: public int getVersion() {
096: return version;
097: }
098:
099: /**
100: * @com.intel.drl.spec_ref
101: */
102: public int getRounds() {
103: return rounds;
104: }
105:
106: /**
107: * @com.intel.drl.spec_ref
108: */
109: public int getWordSize() {
110: return wordSize;
111: }
112:
113: /**
114: * @com.intel.drl.spec_ref
115: */
116: public byte[] getIV() {
117: if (iv == null) {
118: return null;
119: }
120: byte[] result = new byte[iv.length];
121: System.arraycopy(iv, 0, result, 0, iv.length);
122: return result;
123: }
124:
125: /**
126: * @com.intel.drl.spec_ref
127: */
128: @Override
129: public boolean equals(Object obj) {
130: if (obj == this ) {
131: return true;
132: }
133: if (!(obj instanceof RC5ParameterSpec)) {
134: return false;
135: }
136: RC5ParameterSpec ps = (RC5ParameterSpec) obj;
137: return (version == ps.version) && (rounds == ps.rounds)
138: && (wordSize == ps.wordSize)
139: && (Arrays.equals(iv, ps.iv));
140: }
141:
142: /**
143: * @com.intel.drl.spec_ref
144: */
145: @Override
146: public int hashCode() {
147: int result = version + rounds + wordSize;
148: if (iv == null) {
149: return result;
150: }
151: for (byte element : iv) {
152: result += element & 0xFF;
153: }
154: return result;
155: }
156: }
|