jdk/src/share/classes/javax/crypto/spec/RC2ParameterSpec.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1998-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.crypto.spec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.spec.AlgorithmParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * This class specifies the parameters used with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * <a href="http://www.ietf.org/rfc/rfc2268.txt"><i>RC2</i></a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <p> The parameters consist of an effective key size and optionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * an 8-byte initialization vector (IV) (only in feedback mode).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p> This class can be used to initialize a <code>Cipher</code> object that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * implements the <i>RC2</i> algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class RC2ParameterSpec implements AlgorithmParameterSpec {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private byte[] iv = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private int effectiveKeyBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * Constructs a parameter set for RC2 from the given effective key size
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * (in bits).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * @param effectiveKeyBits the effective key size in bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public RC2ParameterSpec(int effectiveKeyBits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        this.effectiveKeyBits = effectiveKeyBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * Constructs a parameter set for RC2 from the given effective key size
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * (in bits) and an 8-byte IV.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * <p> The bytes that constitute the IV are those between
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * <code>iv[0]</code> and <code>iv[7]</code> inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @param effectiveKeyBits the effective key size in bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @param iv the buffer with the 8-byte IV. The first 8 bytes of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * the buffer are copied to protect against subsequent modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @exception IllegalArgumentException if <code>iv</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public RC2ParameterSpec(int effectiveKeyBits, byte[] iv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        this(effectiveKeyBits, iv, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * Constructs a parameter set for RC2 from the given effective key size
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * (in bits) and IV.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * <p> The IV is taken from <code>iv</code>, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * <code>offset</code> inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * The bytes that constitute the IV are those between
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * <code>iv[offset]</code> and <code>iv[offset+7]</code> inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @param effectiveKeyBits the effective key size in bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @param iv the buffer with the IV. The first 8 bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * of the buffer beginning at <code>offset</code> inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * are copied to protect against subsequent modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @param offset the offset in <code>iv</code> where the 8-byte IV
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * starts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @exception IllegalArgumentException if <code>iv</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public RC2ParameterSpec(int effectiveKeyBits, byte[] iv, int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        this.effectiveKeyBits = effectiveKeyBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (iv == null) throw new IllegalArgumentException("IV missing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        int blockSize = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (iv.length - offset < blockSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            throw new IllegalArgumentException("IV too short");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        this.iv = new byte[blockSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        System.arraycopy(iv, offset, this.iv, 0, blockSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Returns the effective key size in bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @return the effective key size in bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public int getEffectiveKeyBits() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return this.effectiveKeyBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Returns the IV or null if this parameter set does not contain an IV.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @return the IV or null if this parameter set does not contain an IV.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Returns a new array each time this method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public byte[] getIV() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        return (iv == null? null:(byte[])iv.clone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Tests for equality between the specified object and this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * object. Two RC2ParameterSpec objects are considered equal if their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * effective key sizes and IVs are equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * (Two IV references are considered equal if both are <tt>null</tt>.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param obj the object to test for equality with this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @return true if the objects are considered equal, false if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * <code>obj</code> is null or otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (obj == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (!(obj instanceof RC2ParameterSpec)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        RC2ParameterSpec other = (RC2ParameterSpec) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return ((effectiveKeyBits == other.effectiveKeyBits) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                java.util.Arrays.equals(iv, other.iv));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Calculates a hash code value for the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Objects that are equal will also have the same hashcode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        int retval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (iv != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            for (int i = 1; i < iv.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                retval += iv[i] * i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        return (retval += effectiveKeyBits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
}