37 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LICENSOR BE
38 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
39 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
40 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
41 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
42 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
43 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
45 * POSSIBILITY OF SUCH DAMAGE.
46 */
47
48 package sun.security.pkcs11.wrapper;
49
50 import java.math.BigInteger;
51
52 import java.util.*;
53
54 import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
55
56 /**
57 * This class contains onyl static methods. It is the place for all functions
58 * that are used by several classes in this package.
59 *
60 * @author Karl Scheibelhofer <Karl.Scheibelhofer@iaik.at>
61 * @author Martin Schlaeffer <schlaeff@sbox.tugraz.at>
62 */
63 public class Functions {
64
65 // maps between ids and their names, forward and reverse
66 // ids are stored as Integers to save space
67 // since only the lower 32 bits are ever used anyway
68
69 // mechanisms (CKM_*)
70 private static final Map<Integer,String> mechNames =
71 new HashMap<Integer,String>();
72
73 private static final Map<String,Integer> mechIds =
74 new HashMap<String,Integer>();
75
76 // key types (CKK_*)
77 private static final Map<Integer,String> keyNames =
79
80 private static final Map<String,Integer> keyIds =
81 new HashMap<String,Integer>();
82
83 // attributes (CKA_*)
84 private static final Map<Integer,String> attributeNames =
85 new HashMap<Integer,String>();
86
87 private static final Map<String,Integer> attributeIds =
88 new HashMap<String,Integer>();
89
90 // object classes (CKO_*)
91 private static final Map<Integer,String> objectClassNames =
92 new HashMap<Integer,String>();
93
94 private static final Map<String,Integer> objectClassIds =
95 new HashMap<String,Integer>();
96
97
98 /**
99 * For converting numbers to their hex presentation.
100 */
101 private static final char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray();
102
103 /**
104 * Converts a long value to a hexadecimal String of length 16. Includes
105 * leading zeros if necessary.
106 *
107 * @param value The long value to be converted.
108 * @return The hexadecimal string representation of the long value.
109 */
110 public static String toFullHexString(long value) {
111 long currentValue = value;
112 StringBuffer stringBuffer = new StringBuffer(16);
113 for(int j = 0; j < 16; j++) {
114 int currentDigit = (int) currentValue & 0xf;
115 stringBuffer.append(HEX_DIGITS[currentDigit]);
116 currentValue >>>= 4;
117 }
118
119 return stringBuffer.reverse().toString();
120 }
121
122 /**
123 * Converts a int value to a hexadecimal String of length 8. Includes
124 * leading zeros if necessary.
125 *
126 * @param value The int value to be converted.
127 * @return The hexadecimal string representation of the int value.
128 */
129 public static String toFullHexString(int value) {
130 int currentValue = value;
131 StringBuffer stringBuffer = new StringBuffer(8);
132 for(int i = 0; i < 8; i++) {
133 int currentDigit = currentValue & 0xf;
134 stringBuffer.append(HEX_DIGITS[currentDigit]);
135 currentValue >>>= 4;
136 }
137
138 return stringBuffer.reverse().toString();
139 }
140
141 /**
142 * converts a long value to a hexadecimal String
143 *
144 * @param value the long value to be converted
145 * @return the hexadecimal string representation of the long value
146 */
147 public static String toHexString(long value) {
148 return Long.toHexString(value);
149 }
150
151 /**
152 * Converts a byte array to a hexadecimal String. Each byte is presented by
153 * its two digit hex-code; 0x0A -> "0a", 0x00 -> "00". No leading "0x" is
154 * included in the result.
155 *
156 * @param value the byte array to be converted
157 * @return the hexadecimal string representation of the byte array
158 */
159 public static String toHexString(byte[] value) {
160 if (value == null) {
161 return null;
162 }
163
164 StringBuffer buffer = new StringBuffer(2 * value.length);
165 int single;
166
167 for (int i = 0; i < value.length; i++) {
168 single = value[i] & 0xFF;
169
170 if (single < 0x10) {
171 buffer.append('0');
172 }
173
174 buffer.append(Integer.toString(single, 16));
175 }
176
177 return buffer.toString();
178 }
179
180 /**
181 * converts a long value to a binary String
182 *
183 * @param value the long value to be converted
184 * @return the binary string representation of the long value
185 */
186 public static String toBinaryString(long value) {
187 return Long.toString(value, 2);
188 }
189
190 /**
191 * converts a byte array to a binary String
192 *
193 * @param value the byte array to be converted
194 * @return the binary string representation of the byte array
195 */
196 public static String toBinaryString(byte[] value) {
197 BigInteger helpBigInteger = new BigInteger(1, value);
|
37 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LICENSOR BE
38 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
39 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
40 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
41 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
42 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
43 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
45 * POSSIBILITY OF SUCH DAMAGE.
46 */
47
48 package sun.security.pkcs11.wrapper;
49
50 import java.math.BigInteger;
51
52 import java.util.*;
53
54 import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
55
56 /**
57 * This class contains only static methods. It is the place for all functions
58 * that are used by several classes in this package.
59 *
60 * @author Karl Scheibelhofer <Karl.Scheibelhofer@iaik.at>
61 * @author Martin Schlaeffer <schlaeff@sbox.tugraz.at>
62 */
63 public class Functions {
64
65 // maps between ids and their names, forward and reverse
66 // ids are stored as Integers to save space
67 // since only the lower 32 bits are ever used anyway
68
69 // mechanisms (CKM_*)
70 private static final Map<Integer,String> mechNames =
71 new HashMap<Integer,String>();
72
73 private static final Map<String,Integer> mechIds =
74 new HashMap<String,Integer>();
75
76 // key types (CKK_*)
77 private static final Map<Integer,String> keyNames =
79
80 private static final Map<String,Integer> keyIds =
81 new HashMap<String,Integer>();
82
83 // attributes (CKA_*)
84 private static final Map<Integer,String> attributeNames =
85 new HashMap<Integer,String>();
86
87 private static final Map<String,Integer> attributeIds =
88 new HashMap<String,Integer>();
89
90 // object classes (CKO_*)
91 private static final Map<Integer,String> objectClassNames =
92 new HashMap<Integer,String>();
93
94 private static final Map<String,Integer> objectClassIds =
95 new HashMap<String,Integer>();
96
97
98 /**
99 * Converts a long value to a hexadecimal String of length 16. Includes
100 * leading zeros if necessary.
101 *
102 * @param value The long value to be converted.
103 * @return The hexadecimal string representation of the long value.
104 */
105 public static String toFullHexString(long value) {
106 return Long.toHexString(value, 16)
107 .toUpperCase();
108 }
109
110 /**
111 * Converts a int value to a hexadecimal String of length 8. Includes
112 * leading zeros if necessary.
113 *
114 * @param value The int value to be converted.
115 * @return The hexadecimal string representation of the int value.
116 */
117 public static String toFullHexString(int value) {
118 return Integer.toHexString(value, 8)
119 .toUpperCase();
120 }
121
122 /**
123 * converts a long value to a hexadecimal String
124 *
125 * @param value the long value to be converted
126 * @return the hexadecimal string representation of the long value
127 */
128 public static String toHexString(long value) {
129 return Long.toHexString(value);
130 }
131
132 /**
133 * Converts a byte array to a hexadecimal String. Each byte is presented by
134 * its two digit hex-code; 0x0A -> "0a", 0x00 -> "00". No leading "0x" is
135 * included in the result.
136 *
137 * @param value the byte array to be converted
138 * @return the hexadecimal string representation of the byte array
139 */
140 public static String toHexString(byte[] value) {
141 if (value == null) {
142 return null;
143 }
144
145 StringBuilder sb = new StringBuilder(2 * value.length);
146 for (byte b : value) {
147 sb.append(Integer.toHexString((int)b & 0xFF, 2));
148 }
149 return sb.toString();
150 }
151
152 /**
153 * converts a long value to a binary String
154 *
155 * @param value the long value to be converted
156 * @return the binary string representation of the long value
157 */
158 public static String toBinaryString(long value) {
159 return Long.toString(value, 2);
160 }
161
162 /**
163 * converts a byte array to a binary String
164 *
165 * @param value the byte array to be converted
166 * @return the binary string representation of the byte array
167 */
168 public static String toBinaryString(byte[] value) {
169 BigInteger helpBigInteger = new BigInteger(1, value);
|