Path: blob/master/src/java.base/share/classes/javax/crypto/spec/RC2ParameterSpec.java
41159 views
/*1* Copyright (c) 1998, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.crypto.spec;2627import java.security.spec.AlgorithmParameterSpec;2829/**30* This class specifies the parameters used with the31* <a href="http://www.ietf.org/rfc/rfc2268.txt"><i>RC2</i></a>32* algorithm.33*34* <p> The parameters consist of an effective key size and optionally35* an 8-byte initialization vector (IV) (only in feedback mode).36*37* <p> This class can be used to initialize a {@code Cipher} object that38* implements the <i>RC2</i> algorithm.39*40* @author Jan Luehe41*42* @since 1.443*/44public class RC2ParameterSpec implements AlgorithmParameterSpec {4546private byte[] iv = null;47private int effectiveKeyBits;4849/**50* Constructs a parameter set for RC2 from the given effective key size51* (in bits).52*53* @param effectiveKeyBits the effective key size in bits.54*/55public RC2ParameterSpec(int effectiveKeyBits) {56this.effectiveKeyBits = effectiveKeyBits;57}5859/**60* Constructs a parameter set for RC2 from the given effective key size61* (in bits) and an 8-byte IV.62*63* <p> The bytes that constitute the IV are those between64* {@code iv[0]} and {@code iv[7]} inclusive.65*66* @param effectiveKeyBits the effective key size in bits.67* @param iv the buffer with the 8-byte IV. The first 8 bytes of68* the buffer are copied to protect against subsequent modification.69* @exception IllegalArgumentException if {@code iv} is null.70*/71public RC2ParameterSpec(int effectiveKeyBits, byte[] iv) {72this(effectiveKeyBits, iv, 0);73}7475/**76* Constructs a parameter set for RC2 from the given effective key size77* (in bits) and IV.78*79* <p> The IV is taken from {@code iv}, starting at80* {@code offset} inclusive.81* The bytes that constitute the IV are those between82* {@code iv[offset]} and {@code iv[offset+7]} inclusive.83*84* @param effectiveKeyBits the effective key size in bits.85* @param iv the buffer with the IV. The first 8 bytes86* of the buffer beginning at {@code offset} inclusive87* are copied to protect against subsequent modification.88* @param offset the offset in {@code iv} where the 8-byte IV89* starts.90* @exception IllegalArgumentException if {@code iv} is null.91*/92public RC2ParameterSpec(int effectiveKeyBits, byte[] iv, int offset) {93this.effectiveKeyBits = effectiveKeyBits;94if (iv == null) throw new IllegalArgumentException("IV missing");95int blockSize = 8;96if (iv.length - offset < blockSize) {97throw new IllegalArgumentException("IV too short");98}99this.iv = new byte[blockSize];100System.arraycopy(iv, offset, this.iv, 0, blockSize);101}102103/**104* Returns the effective key size in bits.105*106* @return the effective key size in bits.107*/108public int getEffectiveKeyBits() {109return this.effectiveKeyBits;110}111112/**113* Returns the IV or null if this parameter set does not contain an IV.114*115* @return the IV or null if this parameter set does not contain an IV.116* Returns a new array each time this method is called.117*/118public byte[] getIV() {119return (iv == null? null:iv.clone());120}121122/**123* Tests for equality between the specified object and this124* object. Two RC2ParameterSpec objects are considered equal if their125* effective key sizes and IVs are equal.126* (Two IV references are considered equal if both are {@code null}.)127*128* @param obj the object to test for equality with this object.129*130* @return true if the objects are considered equal, false if131* {@code obj} is null or otherwise.132*/133public boolean equals(Object obj) {134if (obj == this) {135return true;136}137if (!(obj instanceof RC2ParameterSpec)) {138return false;139}140RC2ParameterSpec other = (RC2ParameterSpec) obj;141142return ((effectiveKeyBits == other.effectiveKeyBits) &&143java.util.Arrays.equals(iv, other.iv));144}145146/**147* Calculates a hash code value for the object.148* Objects that are equal will also have the same hashcode.149*/150public int hashCode() {151int retval = 0;152if (iv != null) {153for (int i = 1; i < iv.length; i++) {154retval += iv[i] * i;155}156}157return (retval += effectiveKeyBits);158}159}160161162