author | stefank |
Mon, 25 Aug 2014 09:10:13 +0200 | |
changeset 26314 | f8bc1966fb30 |
parent 25859 | 3317bb8137f4 |
child 37782 | ad8fe7507ecc |
permissions | -rw-r--r-- |
2 | 1 |
/* |
24578
bbf15045dfbb
8043342: Replace uses of StringBuffer with StringBuilder within crypto code
wetmore
parents:
17916
diff
changeset
|
2 |
* Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.security.pkcs11; |
|
27 |
||
28 |
import java.math.BigInteger; |
|
29 |
import java.security.*; |
|
30 |
||
31 |
/** |
|
32 |
* Collection of static utility methods. |
|
33 |
* |
|
34 |
* @author Andreas Sterbenz |
|
35 |
* @since 1.5 |
|
36 |
*/ |
|
37 |
public final class P11Util { |
|
38 |
||
39 |
private static Object LOCK = new Object(); |
|
40 |
||
41 |
private static volatile Provider sun, sunRsaSign, sunJce; |
|
42 |
||
43 |
private P11Util() { |
|
44 |
// empty |
|
45 |
} |
|
46 |
||
47 |
static Provider getSunProvider() { |
|
48 |
Provider p = sun; |
|
49 |
if (p == null) { |
|
50 |
synchronized (LOCK) { |
|
51 |
p = getProvider |
|
52 |
(sun, "SUN", "sun.security.provider.Sun"); |
|
53 |
sun = p; |
|
54 |
} |
|
55 |
} |
|
56 |
return p; |
|
57 |
} |
|
58 |
||
59 |
static Provider getSunRsaSignProvider() { |
|
60 |
Provider p = sunRsaSign; |
|
61 |
if (p == null) { |
|
62 |
synchronized (LOCK) { |
|
63 |
p = getProvider |
|
64 |
(sunRsaSign, "SunRsaSign", "sun.security.rsa.SunRsaSign"); |
|
65 |
sunRsaSign = p; |
|
66 |
} |
|
67 |
} |
|
68 |
return p; |
|
69 |
} |
|
70 |
||
71 |
static Provider getSunJceProvider() { |
|
72 |
Provider p = sunJce; |
|
73 |
if (p == null) { |
|
74 |
synchronized (LOCK) { |
|
75 |
p = getProvider |
|
76 |
(sunJce, "SunJCE", "com.sun.crypto.provider.SunJCE"); |
|
77 |
sunJce = p; |
|
78 |
} |
|
79 |
} |
|
80 |
return p; |
|
81 |
} |
|
82 |
||
83 |
private static Provider getProvider(Provider p, String providerName, |
|
84 |
String className) { |
|
85 |
if (p != null) { |
|
86 |
return p; |
|
87 |
} |
|
88 |
p = Security.getProvider(providerName); |
|
89 |
if (p == null) { |
|
90 |
try { |
|
10336
0bb1999251f8
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents:
5506
diff
changeset
|
91 |
Class<?> clazz = Class.forName(className); |
2 | 92 |
p = (Provider)clazz.newInstance(); |
93 |
} catch (Exception e) { |
|
94 |
throw new ProviderException |
|
95 |
("Could not find provider " + providerName, e); |
|
96 |
} |
|
97 |
} |
|
98 |
return p; |
|
99 |
} |
|
100 |
||
101 |
static byte[] convert(byte[] input, int offset, int len) { |
|
102 |
if ((offset == 0) && (len == input.length)) { |
|
103 |
return input; |
|
104 |
} else { |
|
105 |
byte[] t = new byte[len]; |
|
106 |
System.arraycopy(input, offset, t, 0, len); |
|
107 |
return t; |
|
108 |
} |
|
109 |
} |
|
110 |
||
111 |
static byte[] subarray(byte[] b, int ofs, int len) { |
|
112 |
byte[] out = new byte[len]; |
|
113 |
System.arraycopy(b, ofs, out, 0, len); |
|
114 |
return out; |
|
115 |
} |
|
116 |
||
117 |
static byte[] concat(byte[] b1, byte[] b2) { |
|
118 |
byte[] b = new byte[b1.length + b2.length]; |
|
119 |
System.arraycopy(b1, 0, b, 0, b1.length); |
|
120 |
System.arraycopy(b2, 0, b, b1.length, b2.length); |
|
121 |
return b; |
|
122 |
} |
|
123 |
||
124 |
static long[] concat(long[] b1, long[] b2) { |
|
125 |
if (b1.length == 0) { |
|
126 |
return b2; |
|
127 |
} |
|
128 |
long[] b = new long[b1.length + b2.length]; |
|
129 |
System.arraycopy(b1, 0, b, 0, b1.length); |
|
130 |
System.arraycopy(b2, 0, b, b1.length, b2.length); |
|
131 |
return b; |
|
132 |
} |
|
133 |
||
134 |
public static byte[] getMagnitude(BigInteger bi) { |
|
135 |
byte[] b = bi.toByteArray(); |
|
136 |
if ((b.length > 1) && (b[0] == 0)) { |
|
137 |
int n = b.length - 1; |
|
138 |
byte[] newarray = new byte[n]; |
|
139 |
System.arraycopy(b, 1, newarray, 0, n); |
|
140 |
b = newarray; |
|
141 |
} |
|
142 |
return b; |
|
143 |
} |
|
144 |
||
145 |
static byte[] getBytesUTF8(String s) { |
|
146 |
try { |
|
147 |
return s.getBytes("UTF8"); |
|
148 |
} catch (java.io.UnsupportedEncodingException e) { |
|
149 |
throw new RuntimeException(e); |
|
150 |
} |
|
151 |
} |
|
152 |
||
153 |
static byte[] sha1(byte[] data) { |
|
154 |
try { |
|
155 |
MessageDigest md = MessageDigest.getInstance("SHA-1"); |
|
156 |
md.update(data); |
|
157 |
return md.digest(); |
|
158 |
} catch (GeneralSecurityException e) { |
|
159 |
throw new ProviderException(e); |
|
160 |
} |
|
161 |
} |
|
162 |
||
163 |
private final static char[] hexDigits = "0123456789abcdef".toCharArray(); |
|
164 |
||
165 |
static String toString(byte[] b) { |
|
166 |
if (b == null) { |
|
167 |
return "(null)"; |
|
168 |
} |
|
24578
bbf15045dfbb
8043342: Replace uses of StringBuffer with StringBuilder within crypto code
wetmore
parents:
17916
diff
changeset
|
169 |
StringBuilder sb = new StringBuilder(b.length * 3); |
2 | 170 |
for (int i = 0; i < b.length; i++) { |
171 |
int k = b[i] & 0xff; |
|
172 |
if (i != 0) { |
|
173 |
sb.append(':'); |
|
174 |
} |
|
175 |
sb.append(hexDigits[k >>> 4]); |
|
176 |
sb.append(hexDigits[k & 0xf]); |
|
177 |
} |
|
178 |
return sb.toString(); |
|
179 |
} |
|
180 |
||
181 |
} |