1 package iaik.pkcs.pkcs11.parameters;
2 
3 import iaik.pkcs.pkcs11.wrapper.CK_RC2_CBC_PARAMS;
4 import iaik.pkcs.pkcs11.wrapper.Constants;
5 import iaik.pkcs.pkcs11.wrapper.Functions;
6 
7 /**
8  * This class encapsulates parameters for the algorithm Mechanism.RC2_CBC.
9  *
10  * @author Karl Scheibelhofer
11  * @version 1.0
12  * @invariants (initializationVector_ <> null)
13  */
14 public class RC2CbcParameters extends RC2Parameters {
15 
16 	/**
17 	 * The initialization vector.
18 	 */
19 	protected byte[] initializationVector_;
20 
21 	/**
22 	 * Create a new RC2CbcParameters object with the given effective bits and the
23 	 * initialization vector.
24 	 *
25 	 * @param effectiveBits The effective number of bits in the RC2 search space.
26 	 * @param initializationVector The initialization vector.
27 	 * @preconditions (effectiveBits >= 1) and (effectiveBits <= 1024)
28 	 *                and (initializationVector <> null)
29 	 * @postconditions
30 	 */
RC2CbcParameters(long effectiveBits, byte[] initializationVector)31 	public RC2CbcParameters(long effectiveBits, byte[] initializationVector) {
32 		super(effectiveBits);
33 		if (initializationVector == null) {
34 			throw new NullPointerException(
35 			    "Argument \"initializationVector\" must not be null.");
36 		}
37 		initializationVector_ = initializationVector;
38 	}
39 
40 	/**
41 	 * Create a (deep) clone of this object.
42 	 *
43 	 * @return A clone of this object.
44 	 * @preconditions
45 	 * @postconditions (result <> null)
46 	 *                 and (result instanceof RC2CbcParameters)
47 	 *                 and (result.equals(this))
48 	 */
clone()49 	public java.lang.Object clone() {
50 		RC2CbcParameters clone = (RC2CbcParameters) super.clone();
51 
52 		clone.initializationVector_ = (byte[]) this.initializationVector_.clone();
53 
54 		return clone;
55 	}
56 
57 	/**
58 	 * Get this parameters object as CK_RC2_CBC_PARAMS object.
59 	 *
60 	 * @return This object as CK_RC2_CBC_PARAMS object.
61 	 * @preconditions
62 	 * @postconditions (result <> null)
63 	 */
getPKCS11ParamsObject()64 	public Object getPKCS11ParamsObject() {
65 		CK_RC2_CBC_PARAMS params = new CK_RC2_CBC_PARAMS();
66 
67 		params.ulEffectiveBits = effectiveBits_;
68 		params.iv = initializationVector_;
69 
70 		return params;
71 	}
72 
73 	/**
74 	 * Get the initialization vector.
75 	 *
76 	 * @return The initialization vector.
77 	 * @preconditions
78 	 * @postconditions (result <> null)
79 	 */
getInitializationVector()80 	public byte[] getInitializationVector() {
81 		return initializationVector_;
82 	}
83 
84 	/**
85 	 * Set the initialization vector.
86 	 *
87 	 * @param initializationVector The initialization vector.
88 	 * @preconditions (initializationVector <> null)
89 	 * @postconditions
90 	 */
setInitializationVector(byte[] initializationVector)91 	public void setInitializationVector(byte[] initializationVector) {
92 		if (initializationVector == null) {
93 			throw new NullPointerException(
94 			    "Argument \"initializationVector\" must not be null.");
95 		}
96 		initializationVector_ = initializationVector;
97 	}
98 
99 	/**
100 	 * Returns the string representation of this object. Do not parse data from
101 	 * this string, it is for debugging only.
102 	 *
103 	 * @return A string representation of this object.
104 	 */
toString()105 	public String toString() {
106 		StringBuffer buffer = new StringBuffer();
107 
108 		buffer.append(Constants.INDENT);
109 		buffer.append("Effective Bits (dec): ");
110 		buffer.append(effectiveBits_);
111 		buffer.append(Constants.NEWLINE);
112 
113 		buffer.append(Constants.INDENT);
114 		buffer.append("Initialization Vector (hex): ");
115 		buffer.append(Functions.toHexString(initializationVector_));
116 		// buffer.append(Constants.NEWLINE);
117 
118 		return buffer.toString();
119 	}
120 
121 	/**
122 	 * Compares all member variables of this object with the other object.
123 	 * Returns only true, if all are equal in both objects.
124 	 *
125 	 * @param otherObject The other object to compare to.
126 	 * @return True, if other is an instance of this class and all member
127 	 *         variables of both objects are equal. False, otherwise.
128 	 * @preconditions
129 	 * @postconditions
130 	 */
equals(java.lang.Object otherObject)131 	public boolean equals(java.lang.Object otherObject) {
132 		boolean equal = false;
133 
134 		if (otherObject instanceof RC2CbcParameters) {
135 			RC2CbcParameters other = (RC2CbcParameters) otherObject;
136 			equal = (this == other)
137 			    || (super.equals(other) && Functions.equals(this.initializationVector_,
138 			        other.initializationVector_));
139 		}
140 
141 		return equal;
142 	}
143 
144 	/**
145 	 * The overriding of this method should ensure that the objects of this class
146 	 * work correctly in a hashtable.
147 	 *
148 	 * @return The hash code of this object.
149 	 * @preconditions
150 	 * @postconditions
151 	 */
hashCode()152 	public int hashCode() {
153 		return super.hashCode() ^ Functions.hashCode(initializationVector_);
154 	}
155 
156 }
157