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